using a template for my Double linked node list(logic problem)

[
template <typename T>
class LinkedList
{
public:
LinkedList();
LinkedList(const LinkedList& rhs);
LinkedList& operator=(const LinkedList& rhs);
bool isEmpty();
LinkedNode<T>* getFirst();
LinkedNode<T>* getLast();
void printNode();
void setData();
void insertFirst(T data);
void insertLast(T data);
void insertAfter(T tKey, T tData);
void removeFirst();
void removeLast();
void remove(T removalCandidate);
void destroy();
private:
LinkedNode<T>* mFirst;
LinkedNode<T>* mLast;
};

template <typename T>
void LinkedList<T>::insertFirst(T data)
{
//Get a linkednode pointer to the first node
LinkedNode<T>* N = 0;
N = new LinkedNode<T>;
N->data = data;
//Get a pointer to this linked lists first linked node
LinkedNode<T>* C = this->mFirst;
//get the new node to point to the first one in the list
if(C->next==0)
{
N->next = 0;
N->prev = 0;
mFirst = N;
mLast = N;
}
else
{
N->next = C;
N->next->prev = N;
N->prev = 0;
mFirst = N; //Only mFirst changes, mLast does not change
}

}

template <typename T>
void LinkedList<T>::printNode()
{
LinkedNode<T>* C = mFirst;
int cnt = 0;
while(C!=0)
{
std::cout << "Data[" << cnt << "]=" << C->data << std::endl;
cnt++;
C = C->next;
}
}

template <typename T>
void LinkedList<T>::setData()
{
bool quit = false;

while(quit!=true)
{
int selection = 0;
T data;
std::cout << "1)Enter Data, 2)Quit: ";
std::cin >> selection;
switch(selection)
{
case 1:
{
std::cout << "Enter your telephone number: ";
std::cin >> data;
this->insertFirst(data);
break;
}
case 2:
{
quit = true;
break;
}

}

}
}
]
[
#include <iostream>
#include "LinkedList.h"
#include "LinkedNode.h"

using namespace std;

int main()
{
LinkedList<float> apple;
apple.setData();
apple.printNode();

return 0;
}
]





The code is running but there is some logical problem. It stops after you enter your first data. I am not sure what is wrong if anyone could help that would be greatly appreciated.
Last edited on
You need to repaste your code in code tags to preserve indentation:
[code]
your code here
[/code]
Okay thank you. I will repost
"code" is not the actual code,

[code]insert code here[/code]

Is a markup command similar in html. Use the <> button on the side of your edit box.
results in:
1
2
3
4
5
6
7
8
int main()
{
  LinkedList<float> apple;
  apple.setData();
  apple.printNode();

  return 0;
}


But you much repaste it for the spaces to be preserved.
Sounds like it's crashing. When dealing with pointers, this most likely means you're dereferencing a null or invalid pointer.

I cannot stress this enough: Use a proper debugger. A debugger allows you to step through your code, line by line, and examine the current state of every value in your program. You'll be so happy once you figure out how to do this.

Links, depending on your IDE:

(Visual Studio) https://msdn.microsoft.com/en-us/library/sc65sadd.aspx
(Code::Blocks) http://wiki.codeblocks.org/index.php/Debugging_with_Code::Blocks
(Eclipse) https://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Fgetting_started%2Fcdt_w_debug.htm
(command-line debugging) https://www.cs.fsu.edu/~myers/cop3330/debug/debugger.html

I've used all three IDEs above. Personally I recommend Visual Studio for debugging, they have free versions available...

LinkedNode<T>* C = this->mFirst;
Where do you initialize mFirst?

if(C->next==0)
Where do you initialize next?
Last edited on
Below is a version of your code that compiles.

Line 44: if the list is empty then C is nullptr and your program will crash.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>

template < typename T > class LinkedNode {
public:
    LinkedNode *prev, *next;
    T data;
};

template < typename T > class LinkedList {
  public:
    LinkedList() :
	mFirst(nullptr),
	mLast(nullptr)
    {}
    
    LinkedList(const LinkedList & rhs);
    LinkedList & operator=(const LinkedList & rhs);
    bool isEmpty();
    LinkedNode < T > *getFirst();
    LinkedNode < T > *getLast();
    void printNode();
    void setData();
    void insertFirst(T data);
    void insertLast(T data);
    void insertAfter(T tKey, T tData);
    void removeFirst();
    void removeLast();
    void remove(T removalCandidate);
    void destroy();
  private:
    LinkedNode < T > *mFirst;
    LinkedNode < T > *mLast;
};

template < typename T > void LinkedList < T >::insertFirst(T data)
{
    //Get a linkednode pointer to the first node
    LinkedNode < T > *N = 0;
    N = new LinkedNode < T >;
    N->data = data;
    //Get a pointer to this linked lists first linked node
    LinkedNode < T > *C = this->mFirst;
    //get the new node to point to the first one in the list
    if (C->next == 0) {
	N->next = 0;
	N->prev = 0;
	mFirst = N;
	mLast = N;
    } else {
	N->next = C;
	N->next->prev = N;
	N->prev = 0;
	mFirst = N;	  //Only mFirst changes, mLast does not change
    }
}

template < typename T > void LinkedList < T >::printNode()
{
    LinkedNode < T > *C = mFirst;
    int cnt = 0;
    while (C != 0) {
	std::cout << "Data[" << cnt << "]=" << C->data << std::endl;
	cnt++;
	C = C->next;
    }
}

template < typename T > void LinkedList < T >::setData()
{
    bool quit = false;

    while (quit != true) {
	int selection = 0;
	T data;
	std::cout << "1)Enter Data, 2)Quit: ";
	std::cin >> selection;
	switch (selection) {
	case 1:
	    {
		std::cout << "Enter your telephone number: ";
		std::cin >> data;
		this->insertFirst(data);
		break;
	    }
	case 2:
	    {
		quit = true;
		break;
	    }

	}

    }
}

// #include "LinkedList.h"
// #include "LinkedNode.h"
using namespace std;
int main()
{
    LinkedList < float >apple;
    apple.setData();
    apple.printNode();
    return 0;
}

Thank you all for helping me :D
Topic archived. No new replies allowed.