0% found this document useful (0 votes)
8 views

Prog1_Stack Implementation

.,.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Prog1_Stack Implementation

.,.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ Program to Implement Stack

C++ Program to Implement Stack using array


#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;
C++ Program to Implement Stack
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

The function main() provides a choice to the user if they want to push, pop or
display the stack. According to the user response, the appropriate function is called
using switch. If the user enters an invalid response, then that is printed.
The code snippet for this is given below:

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: {
C++ Program to Implement Stack
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

Output
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit

Enter choice: 1
Enter value to be pushed: 2

Enter choice: 1
Enter value to be pushed: 6
Enter choice: 1
Enter value to be pushed: 8
Enter choice: 1
Enter value to be pushed: 7
Enter choice: 2
The popped element is 7
Enter choice: 3
Stack elements are: 8 6 2
Enter choice: 5
Invalid Choice
C++ Program to Implement Stack
Enter choice: 4
Exit
C++ Program to Implement Stack
C++ Program to Implement Stack using linked list
Given that we utilize a head pointer to keep track of the beginning
of our linked list, we can simply refer to the head pointer as top
when creating a stack using a linked list to make it more related to a
stack.

 Push (insert element in stack)


1. Similar to putting a node at the beginning of the linked list, the
push operation would be.
Therefore, the top pointer will initially be NULL when the Stack
(Linked List) is empty. Let's say we need to add the numbers 1,
2, and 3 to the stack.

2. So, using the new operator, we will first build a new Node and
return its address as a temporary pointer ptr.

3. Then, we will make the link section of the node equal to the top
by setting ptr->link = top and inserting the value 1 in the data
part of the node using ptr->data = value.

4. Finally, we will set top = ptr to refer to the newly generated


node, which will serve as both the top of our stack and the
beginning of the linked list.
C++ Program to Implement Stack
5. Similarly, if we put the values 2 and 3 into the stack, a linked
list of three nodes will result, with the top pointer referring to
the node containing the value 3.

 Pop (delete element from stack)


1. The pop operation is comparable to removing a beginning node
from a linked list. Therefore, we shall compare a temporary
pointer (ptr) to the top pointer.
2. The top pointer will then be moved to the subsequent node, top
= top->link.
3. Using the delete operator and pointer ptr, or delete, we will
finally delete the node (ptr)
4. Our stack will appear something like this after we pop once:

 isEmpty (stack empty or no checker)


C++ Program to Implement Stack
Simply checking whether top == NULL, which denotes that the stack
is empty, will tell us whether the stack is empty or not.

A program that implements a stack using linked list is given as follows.

#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;
void push(int val) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = val;
newnode->next = top;
top = newnode;
}
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< top->data <<endl;
top = top->next;
}
}
void display() {
struct Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
}
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;
C++ Program to Implement Stack
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;
}

You might also like