Stack Linked List using classes instead of struct?

OK - So this doesn't work at all. I am out of time to figure out how to write this program where the class node and class list have to be used. But, for the sake of my brain can someone please explain or provide an answer? I have searched far and wide and cannot find ANY information that references using a list class or node class to implement a stacked linked list.

This has been hurting my brain for a week straight.

Thanks


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

class node {
    void *info;
    node *next;
public:
    node (void *v) {info = v; next = 0; }
    void put_next (node *n) {next = n;}
    node *get_next ( ) {return next;}
    void *get_info ( ) {return info;}
};

class list {
    node *head;
    int node_num;
public:
    list ( ) { node_num = 0; head = 0;}
    void remove (int);
    void insert (void *, int);
    void append (void * v) {insert (v, node_num + 1); }
    void *find (int);
    void display ( );
};
class stack
{
	node *top;
	public:
	stack() // construct
	{
		top=NULL;
	}
	void push();
	void pop();
	void show();
};

void stack::push ()
{ 
	int value;
	node *ptr;
	cout <<"\nPush Operation\n";
	cout <<"Enter a number to insert: ";
	cin >> value;
	ptr=new node;
	ptr->node_num=value;
	ptr->put_next=NULL;
	if(top!=NULL)
		ptr->&put_next=top;
	top=ptr;
	cout << "\nNew item is entered into the stack!";
}

void stack::pop()
{
	node *temp;
	if(top==NULL)
	{
		temp=top;
		top=top->put_next;
		cout << "\nPop Operation....\nPopped value is " << temp->node_num;
		delete temp;
	}
}

void stack::show()
{
	node *ptr1=top;
	cout << "\nThe Stack is\n";
	while(ptr1!=NULL)
	{
		cout << ptr1->node_num << " ->";
		ptr1=ptr1->put_next;
	}
	cout << "NULL\n";

}

int main()
{
stack s;
int choice;
while (1)
{
	cout <<"\n---------------------------------------------------";
	cout <<"\nStack Using a Linked List\n\n";
	cout <<"1:Push\n2:POP\n3:Show Stack\n4:Exit";
	cout <<"\nEnter your selection(1-4): ";
	cin >> choice;
	switch(choice)
	{
		case 1:
			s.push();
			break;
		case 2:
			s.pop();
			break;
		case 3:
			s.show();
			break;
		case 4:
			return 0;
			break;
		default:
			cout<<"\nPlease enter correct selection(1-4)!";
			break;
	}
}
return 0;
}
I'm still no expert in coding but the first error I see is that you put (line 45)
ptr = new node;
where there isn't any default constructor for node. You could put
prt = new node(&value);
instead if you want to stick with the constructor you've defined. Also, there seems to be attempts to directly access the node members:
1
2
ptr->node_num=value;
ptr->put_next=NULL;

In this case you should use the public member functions defined.

in stack::pop()
it seems you're trying to dereference a null pointer.

When using a linked list, you want the class (in this case stack) containing the node struct/class to have a pointer to the first node in the list, because there is no way to access the nodes behind, only the next nodes.
Then when adding something to a linked list, you allocate new memory at node* next. There would be another node* next that would hold the next element in the list.

Hopefully this sketch would help a bit on the understanding!
Node
****
-Node* next---->Node
-void* info	****
		-Node* next---->Node
		-void* info	****
				-Node* next
				-void* info
Last edited on
Topic archived. No new replies allowed.