-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack_Using_Linked_List.cpp
90 lines (75 loc) Β· 3.07 KB
/
Stack_Using_Linked_List.cpp
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
#include <iostream>
using namespace std;
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
class Node{
public:
int data;
Node* next;
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Display(Node* n){
if (n == NULL){
cout << "Stack is Empty";
return;
}
while (n != NULL){
cout << n->data << " ";
n = n->next;
}
cout << endl;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Push(Node** head_ref, int new_data){
// Allocate 'node'
Node* new_node = new Node();
// Input the data
new_node->data = new_data;
// Make 'next' of 'new node' as 'head'
new_node->next = (*head_ref);
// Move the 'head' to point to the 'new node'
(*head_ref) = new_node;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Pop(Node** head_ref){
// Store 'head' node
Node* temp = *head_ref;
if (temp == NULL){
cout << "Stack is Empty";
return;
}
*head_ref = temp->next; // Changed 'head'
delete temp; // free 'old head'
return;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int main(){
Node *head = NULL;
int ch, a;
cout << "1 βββββ> Push \n";
cout << "2 βββββ> Pop \n";
cout << "3 βββββ> Display \n";
cout << "4 βββββ> Exit\n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the value to be Pushed to the Stack : ";
cin >> a;
Push(&head, a);
break;
case (2):
Pop(&head);
break;
case (3):
Display(head);
break;
case (4):
break;
default:
cout << "Enter a value between 1 and 4";
}
}while(ch != 4);
return 0;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/