linked list and stack

Hi there,

I am creating a linked list and a stack.

My linked list contains getters and setters. But once the program reaches the try exception handler to pop the values from the stack. I receive the following violation: -

Exception thrown: read access violation.
**this** was nullptr. occurred.

my methods

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
106
107
108
109
110
111
112
class Node
{
public:
	Node(int value, Node* nextptr = NULL, Node* prevptr = NULL, int currentpriority = 0)
	{
		this->value = value;
		next = nextptr;
		prev = prevptr;
		priority = currentpriority;
	}

	int getVal(void)
	{
		return value; // Exception thrown violations occured here.
	}

	Node* getNext(void)
	{
		return next;	
	}

	Node* getPrev(void)
	{
		return prev;	
	}

	void setVal(int value)
	{
		
		this->value = value;	//???
	}

	void setPrev(Node* prevptr)
	{
		prev = prevptr;
	}

	void setNext(Node* nextptr)
	{
		next = nextptr;
	}

	int getPriority(void)
	{
		return priority;	//returns the priority.
	}

	void setPriority(int priority)
	{
		this->priority = priority; //???
	}

private:
	Node* next;
	Node* prev;
	int priority;
	int value;
};

class Stack
{
public:
	Stack(void) { top = 0; }

	~Stack(void)
	{
		while (top != nullptr)
		{
			delete NodePop();
		}
	}

	void Push(int value)
	{
	Node* tmp = new Node(value, top);	//Creates a new node.
	top = tmp;				
	//tmp->setVal(value);
	//tmp->setNext(nullptr);
		

	}

	Node* NodePop(void)
	{
		Node* tmp = top;
		if (top != nullptr) top = top->getNext();

		return tmp;
	}

	int Pop(void)
	{

		Node* tmp = NodePop();
		int ret = 0;
		if (tmp != nullptr)
		{
			ret = top->getVal();
			Node* tmp = top;
			top = top->getNext();
			delete tmp;
		}
		else
			throw "Stack Empty";

		return ret;
	}

private:

	Node* top;
};


my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	Stack s;
	s.Push(10);

	try
	{
		cout << s.Pop() << endl;
	}
	catch (const char* msg)
	{
		cout << msg << endl;
	}

	getchar();


	return 0;
}



note: I cannot make any changes to the classes or add arrays/variables. I also have to make use of Object-oriented programming
Okay Pop() is throwing an exception, what does that tell you?

Did you try running the program with your debugger? The debugger will be able to tell you exactly where the problem occurs and allow you to view the variables at the time of the exception.

note: I cannot make any changes to the classes or add arrays/variables.

Well if you can't make any changes to the classes, what are you allowed to do, especially since the problem is in one of the class functions that you say you can't modify?


Also I suggest you see about increasing your compiler warning levels and fix all warnings that should be issued by your compiler, such as:
In member function ‘int Stack::Pop()’:|
warning: declaration of ‘tmp’ shadows a previous local [-Wshadow]|
I'm not sure if I am allowed to modify such classes or add arrays/variables.
But, let's just say I was allowed to.

I can modify the member functions/methods the actual class implementation i don't think I can.

I don't quite fully understand how the debugger works. however, I do know that the exception is thrown once you step into line 99.
Hello, I didn't actually run your code, but you seem to be re-assigning top to top->getNext() in both NodePop() and in Pop(). Is this intentional? If I'm correct, this makes top become top->getNext()->getNext(). And if you only have 1 element, this will fail.

If I'm correct, you need to decide which place you want to actually modify the data structure; in NodePop() or Pop()? Your naming convention is a bit ambiguous... maybe you should change Pop()'s name to Top() [returns the top-most node without popping], and then change NodePop()'s name to just Pop().
Last edited on
Topic archived. No new replies allowed.