forked from abhishekdoifode1/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack Using Linked list.cpp
181 lines (169 loc) · 4.72 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// This program illustrates the concept of Stack Data Structure using linked list.
// Methods for push, pop, displaying the contents of the stack, and searching have been programmed.
// No global variables are used in the implementation of the program.
#include<iostream>
#define MAX 5
using namespace std;
class node
{
public:
int data;
node* next;
};
bool isEmpty(node** head) //Checks if the stack is empty or not. Here head refers to tos (top of stack)
{
if(*head == NULL)
{
return true;
}
return false;
}
bool isFull(int n)//Checks if the stack is Full or not. Here, n is the current size of the stack
{
if(n == MAX)
{
return true;
}
return false;
}
void push(node** head, node* new_node, int n)// head refers to tos(top of stack).
{
// Double pointer is the pointer to another pointer.
// So it stores the address of another pointer which,
// in this case is tos because we need insert into the stack and update the tos.
if(isFull(n))
{
cout << "Overflow!!\n";
}
else
{
new_node->next = *head;
*head = new_node;
}
}
int pop(node** head)
{
if(isEmpty(head))
{
cout << "Underflow!!\n";
return -1;
}
else
{
node* temp = *head;
int item = temp->data;
*head = temp->next;
delete temp;
return item;
}
}
void display(node** head)
{
if(isEmpty(head))
{
cout << "Underflow!!\n";
}
else
{
node* temp = *head;
while(temp != NULL) //Traversing through the stack and printing each element/data stored at respective index.
{
cout << temp->data << endl;
temp = temp->next;
}
}
}
void search(node** head, int pos, int n)//Search method: head refers to tos(top of stack), pos is the position where the search is to be performed. n is the current size of the stack.
{
if(isEmpty(head)) // Checking if the stack is empty or not.
{
cout << "Underflow!!\n";
return;
}
node* tempTos = NULL; // Creating a temporary stack in which we would push the elements after popping from the original one until position pos is not found.
int temp_size = 0;
int p = 0;
if(pos >= n) // If the input index is out of range where no value exists, print "Not found!!"
{
cout << "Not Found!!\n";
}
else
{
while(p <= pos) // until p<=pos we need to perform following operations
{
int popped = pop(head); // pop from the original stack
n--;
node* temp_node = new node(); //creating a new node to be pushed into the temporary stack
temp_node->data = popped;
push(&tempTos, temp_node, temp_size); // push into the temporary stack
temp_size++; // increment the size of temporary stack
p++; // increment p
}
cout << "Data at the required index: " << tempTos->data << endl; // at this point, we have popped and pushed all the elements of stack into the temporary stack.
int i=0;
while(i<temp_size) // RECONSTRUCTION OF ORIGINAL STACK
{
int popped = pop(&tempTos); // Pop from the temporary stack
node* new_node = new node();
new_node->data = popped;
push(head, new_node, n);//Push into the original stack.
i++;
}
}
}
//Driver method.
int main()
{
node* tos = NULL;
int n=0,choice;
while(1) // MAIN MENU
{
cout << "\n1.Push\n";
cout << "\n2.Pop\n";
cout << "\n3.Display\n";
cout << "\n4.Search\n";
cout << "\n5.Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
{
node* new_node = new node();
int item;
cout << "Enter the element you want to push into the stack: ";
cin >> item;
new_node->data = item;
push(&tos, new_node, n);
n++;
break;
}
case 2:
{
int popped = pop(&tos);
cout << popped << endl;
n--;
break;
}
case 3:
{
display(&tos);
break;
}
case 4:
{
int pos;
cout << "Enter the position at which you want to search: ";
cin >> pos;
search(&tos, pos, n);
break;
}
case 5:
{
return 0;
}
default:
cout << "Enter the valid choice!!\n";
}
}
}