Stacks through Pointers

I want to make a code and it must be a pointer.
I need push, pop, reverse, and display.
I don't know how but i've finished the display.
In the push part, my problem is it errors at
1
2
3
4
5
 case 1:
			cout << "enter elements\n";
			cin >> num;
			push(&top, num);
			break;


I have my pop code but not in pointer and i don't know how to reverse.
I can't use vector and class since we haven't tackled it at school.
Can anyone help me? Thank you!


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
  #include <iostream>
using namespace std;
//void pop();
//void reverse();
void display();

struct Node{
	int info;
	struct Node *next;
};

struct Node *initNode(int data){
	struct Node *top = (struct Node*)malloc(sizeof(struct Node));
	top->info = data;
	top->next = NULL;
	return top;
}

typedef struct Node *NODE;

void push(struct Node **top, int n)
{
	struct Node *NewNode = (Node*)malloc(sizeof(Node));
	if (top == 0)
	{
		*top = NewNode;
	}
	else
	{
		NewNode->next = *top;
		*top = NewNode;
	}
	system("pause");
}

int main()
{
	while (1)
	{
		int choice, num, data;
		system("cls");
		cout << " enter [1]: push" << endl;
		cout << " enter [2]: pop" << endl;
		cout << " enter [3]: reverse" << endl;
		cout << " enter [4]: display" << endl;
		cout << "enter your choice\n";
		cin >> choice;
		system("cls");
		switch (choice)
		{
		case 1:
			cout << "enter elements\n";
			cin >> num;
			push(&top, num);
			break;
		case 2:
			/*pop();*/
			break;
		case 5:
			/*reverse();*/
			break;
		case 4:
			display();
			system("pause");
			break;
		default: break;
		}
		cout << endl;
	}
	system("pause>0");
	return 0;
}

//void push(int n)
//{
//	if (top == SIZE - 1)
//		cout << "stack is full\n";
//	else
//	{
//		array_stacks[++top] = n;
//	}
//	system("pause");
//}

//void display()
//{
//	for (int i = top; i >= 0; i--)
//		cout << array_stacks[i] << endl;
//}

void display(struct Node**top)
{
	struct Node *pointer = (Node*)malloc(sizeof(Node));
	for (pointer = *top; pointer != 0; pointer = pointer->next)
		cout << pointer << endl;
}

//void pop()
//{
//	if (top < 0)
//		cout << "Stack is empty!";
//	else
//	{
//		array_stacks[--top];
//	}
//}

//void reverse()
//{
//	for (int i = top; i >= 0; i--)
//		cout << array_stacks[i - 1] << endl;
//} 
UPDATE:
I change this part and it does not errors like case 1 does
1
2
3
4
case 4:
			display(&top);
			system("pause");
			break;
On line 54: The variable top does not exists in that scope. Maybe like so:
1
2
3
4
5
int main()
{
	struct Node *top = nullptr;
	while (1)
	{


Furthermore I think that you misunderstand the use of the second parameter of push(...). Set NewNode->info = n; in push(...). I.e. line 52: cout << "enter info\n";
I don't fully understand the concept of it but i'm really trying.
I changed this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void push(struct Node **top, int n)
{
	struct Node *NewNode = (Node*)malloc(sizeof(Node));
	NewNode->info = n;
	if (top == 0)
	{
		*top = NewNode;
	}
	else
	{
		NewNode->next = *top;
		*top = NewNode;
	}
	system("pause");
}

and this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Node *top = nullptr;
	while (1)
	{
		int choice, num, data;
		system("cls");
		cout << " enter [1]: push" << endl;
		cout << " enter [2]: pop" << endl;
		cout << " enter [3]: reverse" << endl;
		cout << " enter [4]: display" << endl;
		cout << "enter your choice\n";
		cin >> choice;
		system("cls");
		switch (choice)
		{
		case 1:
			cout << "enter elements\n";
			cin >> NODE->info//the NODE errors;
			push(&top,info)//the info errors;
			break;
I changed this part and it displays the address... I might close to the push part and i need a little changing.
1
2
3
4
5
case 1:
			cout << "enter elements\n";
			cin >>num//i changed it to num
			push(&top,num)//this one too;
			break;
I changed this part and it displays the address...
You mean this:
cout << pointer << endl;
Yes: It shows the pointer which is the address. To show the info change it to:
cout << pointer->info << endl;
I think it's pretty straightforward.

In display(...) it doesn't make sense to create a node. It may look like this:
1
2
3
4
5
void display(const struct Node *top)
{
	for (pointer = *top; pointer != 0; pointer = pointer->next)
		cout << pointer->info << endl;
}

I created a node because pointer->next and pointer->info errors
This one now inputs the number
1
2
3
4
5
6
void display(struct Node**top)
{
	struct Node *pointer = (Node*)malloc(sizeof(Node));
	for (pointer = *top; pointer != 0; pointer = pointer->next)
		cout << pointer->info << endl;
}

BUT
1
2
3
4
5
6
when i input for example: 1, 2, 3
it displays:
3
2
1
3


I'm so close hahaha
This is my
case 1:
1
2
3
4
5
case 1:
			cout << "enter elements\n";
			cin >> cur->info;
			push(&top, cur->info);
			break;


I called
1
2
struct Node *top = initNode(5);
	struct Node *cur = top;

at the start of main
This is my whole code so far
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
#include <iostream>
using namespace std;
//void pop();
//void reverse();
//void display();

struct Node{
	int info;
	struct Node *next;
};

struct Node *initNode(int data){
	struct Node *top = (struct Node*)malloc(sizeof(struct Node));
	top->info = data;
	top->next = NULL;
	return top;
}

typedef struct Node *NODE;

void push(struct Node **top, int n)
{
	struct Node *NewNode = (Node*)malloc(sizeof(Node));
	NewNode->info = n;
	if (top == 0)
	{
		*top = NewNode;
	}
	else
	{
		NewNode->next = *top;
		*top = NewNode;
	}
	system("pause");
}

void display(struct Node**top)
{
	struct Node *pointer = (Node*)malloc(sizeof(Node));
	for (pointer = *top; pointer != 0; pointer = pointer->next)
		cout << pointer->info << endl;
}

int main()
{
	struct Node *top = initNode(5);
	struct Node *cur = top;
	while (1)
	{
		int choice, num, data;
		system("cls");
		cout << " enter [1]: push" << endl;
		cout << " enter [2]: pop" << endl;
		cout << " enter [3]: reverse" << endl;
		cout << " enter [4]: display" << endl;
		cout << "enter your choice\n";
		cin >> choice;
		system("cls");
		switch (choice)
		{
		case 1:
			cout << "enter elements\n";
			cin >> cur->info;
			push(&top, cur->info);
			break;
		//case 2:
		//	/*pop();*/
		//	break;
		//case 5:
		//	/*reverse();*/
		//	break;
		case 4:
			display(&top);
			system("pause");
			break;
		default: break;
		}
		cout << endl;
	}
	system("pause>0");
	return 0;
}
Last edited on
Well, I modified you code so that it works:
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
#include <iostream>
using namespace std;
//void pop();
//void reverse();
//void display();

struct Node{
	int info;
	struct Node *next;
};

struct Node *initNode(int data){
	struct Node *top = (struct Node*)malloc(sizeof(struct Node));
	top->info = data;
	top->next = NULL;
	return top;
}

typedef struct Node *NODE;

void push(struct Node **top, int n)
{
	struct Node *NewNode = initNode(n); // Note
	if (top == 0)
	{
		*top = NewNode;
	}
	else
	{
		NewNode->next = *top;
		*top = NewNode;
	}
	system("pause");
}

void display(struct Node**top)
{
	for (Node *pointer = *top; pointer != 0; pointer = pointer->next) // Note
		cout << pointer->info << endl;
}

int main()
{
	struct Node *top = nullptr;  // Note
	while (1)
	{
		int choice, num, data;
		system("cls");
		cout << " enter [1]: push" << endl;
		cout << " enter [2]: pop" << endl;
		cout << " enter [3]: reverse" << endl;
		cout << " enter [4]: display" << endl;
		cout << "enter your choice\n";
		cin >> choice;
		system("cls");
		switch (choice)
		{
		case 1:
			cout << "enter elements\n";
			cin >> data; // Note
			push(&top, data); // Note
			break;
		//case 2:
		//	/*pop();*/
		//	break;
		//case 5:
		//	/*reverse();*/
		//	break;
		case 4:
			display(&top); // Note
			system("pause");
			break;
		default: break;
		}
		cout << endl;
	}
	system("pause>0");
	return 0;
}
Thank you! It now works!
Should I make another topic for pop or should i just ask here?
If yes, i don't know how will it works but i made a void pop() and it does not work but it might be correct, a little.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void pop(struct Node**top)
{
	struct Node *NewNode = (Node*)malloc(sizeof(Node));
	NewNode->info = NULL;
	if (top < 0)
	{
		cout << "Stack is empty!";
	}
	else
	{
		*top = NewNode->next;
		delete(*top);
	}
}
I finished the whole code!
It's now a fully functional code with push, pop, display, and reverse!
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
113
114
115
116
117
118
119
120
121
#include <iostream>
using namespace std;

struct Node{
	int info;
	struct Node *next;
};

struct Node *initNode(int data){
	struct Node *top = (struct Node*)malloc(sizeof(struct Node));
	top->info = data;
	top->next = NULL;
	return top;
}

typedef struct Node *NODE;
struct Node *top;

void push(struct Node **top, int n)
{
	struct Node *NewNode = initNode(n); 
	if (top == 0)
	{
		*top = NewNode;
	}
	else
	{
		NewNode->next = *top;
		*top = NewNode;
	}
	system("pause");
}

void pop(struct Node**top)
{
	Node*NewNode;
	NewNode = (Node*)malloc(sizeof(Node));
	if (top < 0)
	{
		cout << "Stack is empty!";
	}
	else
	{
		NewNode = *top;
		*top = NewNode->next;
		free(NewNode);
	}
}

void reverse(struct Node **top)
{
	struct Node *first;
	struct Node *rest;

	if (*top == NULL)
		return;

	first = *top;
	rest = first->next;

	if (rest == NULL)
		return;

	reverse(&rest);
	first->next->next = first;
	first->next = NULL;
	*top = rest;
}

void display(struct Node**top)
{
	cout << "=====================" << endl;
	for (Node *pointer = *top; pointer != 0; pointer = pointer->next)
		cout << pointer->info << endl;
		cout << "=====================" << endl;
}

int main()
{
	struct Node *top = nullptr;  
	while (1)
	{
		int choice, num, data;
		system("cls");
		cout << "==============================" << endl;
		cout << "Enter [1]: Push" << endl;
		cout << "Enter [2]: Pop" << endl;
		cout << "Enter [3]: Reverse" << endl;
		cout << "Enter [4]: Display" << endl;
		cout << "Enter [5]: Exit" << endl;
		cout << "==============================" << endl;
		cout << "Enter your choice\: ";
		cin >> choice;
		system("cls");
		switch (choice)
		{
		case 1:
			cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
			cout << "Enter an element: ";
			cin >> data; 
			push(&top, data);
			break;
		case 2:
			pop(&top);
			break;
		case 3:
			reverse(&top);
			break;
		case 4:
			display(&top); 
			system("pause");
			break;
		case 5:
			exit(1);
		default: break;
		}
		cout << endl;
	}
	system("pause>0");
	return 0;
}
Ok, now remove line 37 and you should be done
ok thank you!
Wait: line 38 doesn't make sense. It should be if (top == 0)
Topic archived. No new replies allowed.