Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
C++
#include <iostream>
using namespace std;
template<class T>class LinkedList
{
    public: T* current;
    public: LinkedList* next = NULL;
    public: LinkedList* prev = NULL;
    private: long long CountFirst()
    {
        if (prev == NULL)
        {
            return 0;
        }
        long long answer = (prev == NULL)?0:prev->prev->CountFirst();
        return answer;
    }

    public: long long Count()
    {
        if (next == NULL)
        {
            return 0;
        }
        long long answer = CountFirst();
        answer += (current == NULL)?0:1;
        answer += (next == NULL)?0:next->Count();
        return answer;
    }
    public: T* Find()
    {

    }
    public: T& operator[](int index)
    {
        if (next == NULL and current != NULL)
        {
            return *current;
        }
        if (prev->Count() - 1 > index)
        {
            return *prev[index].current;
        }
        if (prev->Count() == index)
        {
            return *current;
        }
        return *next[index - prev->Count()].current;
    }
    public: void Add(T ForAdd, int pos)
    {
        cout << "ADD";
        ((*this)[pos - 1]) = (*this)[pos];
        cout << "ADD";
        ((*this)[pos]) = (*this)[pos - 1];
        cout << "ADD";
        ((*this)[pos]) = (*this)[pos + 1];
        cout << "ADD";
        ((*this)[pos + 1]) = (*this)[pos];
        cout << "ADD";
        ((*this)[pos]) = &ForAdd;//crash
        cout << "ADD";
    }
    public: void Delete(int pos)
    {
        (*this)[pos - 1] = (*this)[pos + 1];
        (*this)[pos + 1] = (*this)[pos - 1];
    }
};

it crashes on function Add
Posted
Comments
OriginalGriff 28-Aug-13 11:41am    
And?
What have you done to find the problem? Used the debugger? Examined your list before teh call?
Does it crash every time you call it? Or just under some circumstances?
Radoslav Dimitrov 28-Aug-13 11:44am    
Everytime.
OriginalGriff 28-Aug-13 11:50am    
So if it crashes everytime, then it crashes on the first time. So on the first time, is there an element at
((*this)[pos - 1])
because if there isn't...
Look in the debugger. breakpoint the function, and single step through. It should be fairly obvious where the problem is.
[no name] 28-Aug-13 11:44am    
Not sure what your question is.
Usually there is a '?' following it, but if I put it at the end of the 5 words you actually wrote it doesn't make sense. Are you asking us
"it crashes on function Add?"
Hmmmm. Maybe. I wonder if anyone has run the code before that could provide more details.....
Radoslav Dimitrov 28-Aug-13 11:46am    
This is a library.
Sorse:
#include <iostream>
#include "baba.h"
#include "linked list.h"

using namespace std;

int main()
{
LinkedList<string> babi;
char command;
while (true)
{
cin >> command;
string name;
long long ebn;
switch (command)
{
case 'a':
cin >> name;
babi.Add(name, babi.Count() - 1);
break;
case 'r':
babi.Delete(0);
break;
case 'd':
cin >> ebn;
babi.Delete(0);
break;
case 'e':
return 0;
}
}
}

1 solution

This is indeed a very exotic definition of a linked list. And as far as I can see it contains many errors. The one you are probably stumbling about is:
C++
public: void Add(T ForAdd, int pos)
{
    ...
    ((*this)[pos]) = &ForAdd;//crash
}

You are taking the address of the parameter ForAdd, which is transferred by value and hence just a temporary variable that will go out of scope as soon as you return from the function. And you are assing this address to the current pointer of you linked list.

Besides that I see many other strange things.

- the members next and prev are nowhere assigned to, so your list will only contain a single element
- the constructor is missing so all the members are not initialized
- you don't distiguish between the list and a node of the list and are trying to use the same class for both
- the CountFirst function is defined recursively (an absolute no go for longer lists) and as it stands will access prev->prev-> which is one step too much. You forgot to add 1 and hence CountFist will always return 0 if doesn't crash first.

I could continue for a while here. Why don't you take a look at the implementation of other linked lists and learn from them. That will make your life a lot easier and your learning progress will be a lot faster.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900