Having trouble with linked lists

Hi, I'm fairly new to C++ and wanted to know if you all could help me with something here. I have this bit of code that I'm certain works but there is a problem with where I put the variables or what not. I'm simply trying to make a linked list and so far this is what I have but it is giving me error LNK2019 unresolved external symbol.

Here is the code that i have so far. Thank you all for your time.
For the source
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
#include <iostream>
#include "stackLL.h"
//#include "queueLL.h"
//#include "priorityQueueLL.h"
using namespace std;

int main()
{
	/////////////Test code for stack ///////////////
	stackLL stk;

	stk.push(5);
	stk.push(13);
	stk.push(7);
	stk.push(3);
	stk.push(2);
	stk.push(11);

//	cout << "Popping: " << stk.pop() << endl;
//	cout << "Popping: " << stk.pop() << endl;
//
//	stk.push(17);
//	stk.push(19);
//	stk.push(23);
//
//	while( ! stk.empty() )
//	{
//		cout << "Popping: " << stk.pop() << endl;
//	}
//	
//	// output order: 11,2,23,19,17,3,7,13,5
//
//	///////////////////////////////////////
//
//	//////////Test code for queue ///////////
//
//	/////////////////////////////////////////
//
//
//
//	//////////Test code for priority queue/////
//
//	///////////////////////////////////////////
//
//
	return 0;
}


and for the header file stackLL.h
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
#include <iostream>
using namespace std;

class stackLL
{
private:
	class node
	{
	public:
		int data; // for holding data in the node
		node *next; // for pointing to the next.
	};

	node *top;

public:

	stackLL();

	~stackLL();

	//return true if empty, false if not
	bool empty()
	{}

	//add item to top of stack
	void push(int x)
	{}

	//remove and return top item from stack
	int pop()
	{}

	//additional "weird" methods: TBA

};

stackLL::stackLL()
{
	top = NULL;
}
Hmm what compiler are you using? I didn't get that error when compiling with g++ but it gives an error about undefined reference to `stackLL::~stackLL()'. if you simply add the basic outline for the destructor the program compiles with no errors.
also as far as it working you would have to implement the unimplemented functions for this to really work. right now it wont actually create a linked list since push pop and empty don't do anything.
Last edited on
I know what you mean since so far its a work in progress. I'm using visual studio 2012 ultimate. I wanted to at least make sure that it is working before I continue cause I wanted to make sure I can fix the errors one at a time other than writing everything and fixing alot of errors.
Well defining the destructor method should clear it up. I dont have 2012 but in vs2010 it seems to work.
Last edited on
You know that is one thing I never really truly understood. I needed to know what everyone is talking about. They say you need a deconstructor to prevent memory leaks and such and I always wondered, what exactly are they talking about? And another question about this is, if I were to make a constructor for ~stackLL like this stackLL::~stackLL() then I should clear it up?
1
2
3
4
stackLL::~stackLL()
{

}

should clear your error but you still need to add functionality to it. As far as memory leaks you can find more info on them at
http://en.wikipedia.org/wiki/Memory_leak

here's an example on how you might implement it
1
2
3
4
5
6
7
8
9
10
11
12
stackLL::~stackLL()
{
    node* nextNode;
    node* current = top;
    while(!empty())
    {
        nextNode = current->next;
        delete current;
        current = nextNode;
    }
   top = NULL;
}
Last edited on
a memory 'leak' is kind of a misnomer, memory isnt leaking, its just that if you initialize an object, and then stop using that object but dont destruct or free up the memory it resided in, then you lose that chunk of memory. Using a destructor frees up the memory space the object took up. It may seem harmless and with small test functions it kind of is, but get a big app with lots of objects being initialized and then discarded but not properly cleaned up, and you run out of memory fast, like its leaking out of your system and you run the risk of it drying up. :)

.. at least this is my understanding.
you can also have a memory leak by losing a reference to the data. ie you have a node of data but dont have the pointer to it so there is data but no way to access it.
@K0T4K0t4
I just tried initializing ~stackLL and all I added was stackLL::~stackLL() {} and it stopped complaining about the error.

@gtm I see what you mean about that but the thing I wonder for the ~stackLL space I have, do I have to put anything in there for me to get it to free up the memory or do I just have to have a class constructor for it to get it to free up the memory?
look at the above example i edited it and added whats needed to loop through your list and free the memory by deleting the nodes.
Forgive me if this is a stupid question but how do I call upon the ~stackLL constructor and the stackLL constructor?
stackLL stk; calls the constructor implicitly

to explicitly call the destructor its just like any method call
stk.~stackLL();
or
stk->~stackLL();

also ~stackLL(); is a destructor since it destructs not a constructor.
Last edited on
Cool cause as you can see I have to implement it in three ways. Stack, last in first out, queue, first in first out, and priority but I'm not so sure how to do that. At least not yet.
constructor/destructor are sometimes written as ctor/dtor.

Constructors are called when you create an object (instance of a class) on the stack or on the free store (heap). Which ctor depends on the arguments in the call. Learn what default ctor and copy ctor are and how arguments are used to overload ctors and rules for matching arguments to parameter types.

Destructors are called when an object goes out of scope if it is on the stack (local variable) or is explicitly deleted from the free store or heap. There can be only one dtor. When inheritence is used, learn order of dtor calls.

With this code, how do I create a new node?
I would assume it's something like

1
2
int temp;
temp = new node;
I have updated my header code to this

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
#include <iostream>
using namespace std;

class stackLL
{
private:
	class node
	{
	public:
		int data; // for holding data in the node
		node *next; // for pointing to the next.
	};

	node *top;

public:

	stackLL();

	~stackLL();

	//return true if empty, false if not
	bool empty()
	{}

	//add item to top of stack
	void push(int x)
	{
		node *tmp;
		tmp = new node;
		cout<<top << endl;
		data = x;
		cout<<data<<endl;
	}

	//remove and return top item from stack
	int pop()
	{}

	//additional "weird" methods: TBA

};

stackLL::stackLL()
{
	top = NULL;
}

stackLL::~stackLL()
{

}


but it tells me that data is undeclared. Why?
data is not a member of stackLL. data is a member of node.

tmp->data = x ;
Cool, I have actually made progress since then but now I have a new problem. For some reason, my code gets stuck in an infinite loop here on pop which I temp made as a transverse function to ensure my stack is working correctly. Here is what I have.

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
#include <iostream>
#include "stackLL.h"
//#include "queueLL.h"
//#include "priorityQueueLL.h"
using namespace std;

int main()
{
	/////////////Test code for stack ///////////////
	stackLL stk;

	stk.push(5);
	stk.push(13);
	stk.push(7);
	stk.push(3);
	stk.push(2);
	stk.push(11);

	stk.pop();

//	cout << "Popping: " << stk.pop() << endl;
//	cout << "Popping: " << stk.pop() << endl;
//
//	stk.push(17);
//	stk.push(19);
//	stk.push(23);
//
//	while( ! stk.empty() )
//	{
//		cout << "Popping: " << stk.pop() << endl;
//	}
//	
//	// output order: 11,2,23,19,17,3,7,13,5
//
//	///////////////////////////////////////
//
//	//////////Test code for queue ///////////
//
//	/////////////////////////////////////////
//
//
//
//	//////////Test code for priority queue/////
//
//	///////////////////////////////////////////
//
//

	stk.~stackLL();
	return 0;
}



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
#include <iostream>
using namespace std;

class stackLL
{
private:
	class node
	{
	public:
		int data; // for holding data in the node
		node *next; // for pointing to the next.
	};

	node *top;
	node *nxt;

public:

	stackLL();

	~stackLL();

	//return true if empty, false if not
	bool empty()
	{}

	//add item to top of stack
	void push(int x)
	{
		node *tmp;
		tmp = new node;
		if (top == NULL)
		{
			tmp->data = x;
			top = tmp;
			tmp->next = NULL;
		}
		tmp->data = x;
		tmp->next = top;
		top = tmp;
	}

	//remove and return top item from stack
	int pop()
	{
		nxt = top;
		while (nxt != NULL)
		{
			cout<<nxt->data<<endl;
			nxt = nxt->next;
		}
		return 0;
	}

};

stackLL::stackLL()
{
	top = NULL;
}

stackLL::~stackLL()
{

}
Topic archived. No new replies allowed.