STACK USING PUSH POP PROGRAMME IN C++
INTRODUCTION:
A stack is a collection of components that is an abstract data structure. The stack uses the LIFO technique, which means that the element at the end gets popped out first. Some of the stack's primary operations are as follows:
Push-Pushing a data value to the top of the stack.
Pop - Removes the data value at the top of the stack.
Peek - This method retrieves the stack's top data value.
FEATURES:
- A stack is an ordered set of data types that are comparable.
- LIFO or FILO: Stack is a LIFO (Last in First Out) or FILO (First in Last Out) structure.
- PUSH(): This method is used to add new elements to the Stack
- pop(): function is used to remove pieces from the stack. Only one end of the Stack, named Top, allows both insertion and removal. A stack is said to be in an overflow state when it is entirely full, and an underflow state when it is entirely empty.
Syntax:
1. stackname.push(value)
Parameters: The value of the element to be placed is the parameter.
Results: Adds an element to the stack at the top with the same value as the provided argument.
2.stackname.pop()
Parameters: There are no parameters passed.
Result: The newest member in the stack, or the top element, is removed.
PROGRAMME:
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Output:
![STACK USING PUSH POP PROGRAMME IN C++](https://static.tutorialandexample.com/cpp/stack-using-push-pop-programme-in-cpp1.png)
EXPLANATION:
The value to be added to the stack is the parameter that the push() function in the programming above takes. There is no room in a stack if a top is more than or equal to n and overflow is noted. If not, value is moved to the top of the stack.
If there is a value on the stack, the pop() method returns it. Underflow is reported if the stack is empty.
The display() method displays all of the stack's items. It accomplishes this by using a for loop. The phrase "Stack is empty" is printed if the stack is empty.
Applications of Stack Data Structure:
- Recursion and function calls: The state of the programme at the time a function is called is added to the stack. The state is popped off the stack after the function completes in order to resume the previous function's execution.
- Undo/Redo procedures: Stacks are used by numerous apps to maintain track of prior activities when using the undo-redo function. When an action is executed, it is added to the stack. To undo the action, pop the top element of the stack and do the reverse operation.
- Backtracking Algorithms: The backtracking method uses stacks to keep track of the stages of the solution process. The previous state is removed from the stack and replaced with the current state when the algorithm goes backward.
- Balanced Parentheses: A stack data structure is utilised to determine whether or not parentheses are balanced. A closing parenthesis is taken out of the stack while an opening parenthesis is introduced. If the stack is empty when the expression is finished, the brackets are balanced.
- Expression evaluation: Stack data structures are used to evaluate expressions in infix, postfix, and prefix notations. Operators and operands are placed into the stack, and operations are carried out depending on the top components of the stack.
Advantages of Stack:
Stack data structures are straightforward to create using arrays or linked lists, and their operations are simple to comprehend and implement.
Memory utilisation efficiency: Because the stack utilises a continuous block of memory, it is more memory efficient than other data structures.
rapid access time: Because items are added and withdrawn from the top of the stack, the stack data structure enables rapid access time for adding and deleting elements.
Stack data structure is used to hold function calls and associated states, allowing for more efficient implementation of recursive function calls.
Disadvantages of Stack:
Stack overflow and underflow: The stack data structure can cause stack overflow if too many elements are placed onto it, and stack underflow if too many components are popped off it.
Limitations of recursive function calls: While the stack data structure permits recursive function calls, too many recursive function calls might cause stack overflow, leading in programme termination.