Stacks C++
Stacks C++
INTRODUCTION TO STACK
DEFINITION:
EXAMPLE:
In real life we can think of stack as a stack of copies, stack of plates etc. The
first copy put in the stack is the last one to be removed. Similarly last copy to
put in the stack is the first one to be removed.
OPERATIONS ON STACK
A stack is a linear data structure.
It is controlled by two operations: push and pop.
Both the operations take place from one end of the stack,
usually called top. Top points to the current top position of the
stack.
Push operation adds an element to the top of the stack.
Pop operation removes an element from the top of the stack.
These two operations implements the LIFO method .
IMPLEMENTATION OF STACK
Stack can be implemented in two ways:
As an Array
As a Linked List
STACK AS AN ARRAY
Array implementation of stack uses 4
3
an array to store data
an integer type variable usually called the top, which 30
2
points to the top of the stack.
2 20 1
TOP
NOTE: The variable top contains the index number of 10 0
NOTE: Pushing one more element into the stack will result in overflow.
POP OPERATION
The pop operation deletes the most recently entered item from
the stack.
Any attempt to delete an element from the empty stack results
in “data underflow” condition.
The variable TOP contains the index number of the current top
position of the stack.
Each time the pop operation is performed, the TOP variable is
decremented by 1.
POP OPERATION EXPLAINED
top = 2
PROGRAM TO ILLUSTRATE OPERATIONS ON
STACK AS AN INTEGER ARRAY
#include<iostream.h> void pop() cout<<”1.push”<<”\n
#include<process.h> { if (top== -1) 2.pop”<<”\n3.display”<<”\n
const int size=5 cout<<”Stack is empty “; 4.exit”;
class stack else cout<<”Enter choice :”;
{ int arr[size]; { cout<<arr[top]; cin>>ch;
int top; //top will point to the last item top - -; } if (ch==1)
//pushed onto the stack } { cout<<”enter item to
public: void display() be pushed in the stack”;
stack() { top=-1; } //constructor to create an { cin>>data;
//empty stack, top=-1 indicate for(int i=top;i>=0;i--) s1.push(data);
//that no item is present in the array cout<<arr[top]; }
void push(int item) } else if (ch == 2)
{ if(top==size-1) void main() { s1.pop(); }
cout<<”stack is full, given item cannot { stack s1; else if (ch==3)
be added”; int ch,data; { s1.display(); }
else while(1) else
{ top++; { exit(-1);
arr[top]=item;} }}
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
AS AN ARRAY OF OBJECTS
#include<iostream.h> else void main()
#include<process.h> { top++; { stack s1;
const int size=5 arr[top].roll=r; int ch,data;
struct stu strcpy(arr[top].name,n); while(1)
{ arr[top].total=tot; } { cout<<”1.push”<<”\n
int roll; void pop() 2.pop”<<”\n3.display”<<”\n
char name[20]; { if (top== -1) 4.exit”;
float total; cout<<”Stack is empty “; cout<<”Enter choice :”;
}; else cin>>ch;
class stack { cout<<arr[top].roll<< if (ch==1)
{ stu arr[size]; arr[top].name<<arr[top].tot; { cin>>data;
int top; top - -; } s1.push(data); }
public: void display() else if (ch == 2)
stack() { top=-1; } { { s1.pop(); }
void push(int r, char for(int i=top;i>=0;i--) else if (ch==3)
n[20],float tot) cout<<arr[top].roll<< { s1.display(); }
{ if (top==size-1) arr[top].name<<arr[top].tot<<”\n”; else
cout<<”overflow”; } exit(-1); } }
APPLICATIONS OF STACK
A stack is an appropriate data structure on which information is stored and
then later retrieved in reverse order. The application of stack are as follows:
When a program executes, stack is used to store the return address at time
of function call. After the execution of the function is over, return address is
popped from stack and control is returned back to the calling function.
Step 1. Scan the postfix expression P from left to right and repeat the steps 2, 3
until the STACK is empty.
Step 2. If the scanned element = operand, push it into the STACK.
Step 3. If the scanned element = operator, then
a) Pop the two operands viz operand A and operand B from the STACK.
b) Evaluate ((operand B) operator (operand A))
c) Push the result of evaluation into the STACK.
Step 4. Set result=top most element of the STACK.
EVALUATING POSTFIX EXPRESSION
Q Evaluate the following Postfix expression:
NOTE: Each node of a stack as a linked list has two parts : data part and link part and
is created with the help of self referential structure.
The data part stores the data and link part stores the address of the next node of the
linked list.
NODE
TOP
DATA LINK
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
AS A LINKED LIST
#include<iostream.h> void stack::push() node *temp=top; void main()
#include<conio.h> { top=top->next; { stack st;
struct node node *temp; cout<<temp->roll<<temp- char ch;
{ temp=new node; >name<<temp->total do
int roll; cout<<"Enter roll :"; <<"deleted"; { cout<<"stack
char name[20]; cin>>temp->roll; delete temp; options\nP push
float total; cout<<"Enter name :"; } \nO Pop \nD
node *next; cin>>temp->name; else Display \nQ quit";
}; cout<<"Enter total :"; cout<<"Stack empty"; cin>>ch;
class stack cin>>temp->total; } switch(ch)
{ temp->next=top; void stack::display() { case 'P':
node *top; top=temp; { st.push();break;
public : } node *temp=top; case 'O':
stack() void stack::pop() while(temp!=NULL) st.pop();break;
{ top=NULL;} { { case 'D':
void push(); if(top!=NULL) cout<<temp->roll<<temp- st.display();break;
void pop(); { >name<<temp->total<<" "; }
void display(); temp=temp->next; }while(ch!='Q');
}; } } }
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
AS A LINKED LIST : Explanation
struct node
{ Self Referential Structure: These are special structures which
int roll; contains pointers to themselves.
char name[20]; Here, next is a pointer of type node itself.
float total; Self Referential structures are needed to create Linked Lists.
node *next;
};
class stack
{
node *top;
public : class stack: It contains pointer TOP which will point to the top
stack() of the stack.
{ top=NULL;} The constructor function initializes TOP to NULL.
void push();
void pop();
void display();
};
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
AS A LINKED LIST : Explanation
void stack::push() void stack::pop()
{ {
node *temp; // pointer of type node if(top!=NULL) // if stack is not empty
temp=new node; // new operator will create a new {
//node and address of new node node *temp=top; // temp is a pointer
// is stored in temp //containing address of first node
cout<<"Enter roll :"; top=top->next; // top will now contain address of
cin>>temp->roll; These lines will input //second node
cout<<"Enter name :"; values into roll , name
cin>>temp->name; and total of newly cout<<temp->roll<< // This line will display
cout<<"Enter total :"; created node. temp->name<<temp->total // data stored in
cin>>temp->total; <<"deleted"; // deleted node
temp->next=top; // address stored in TOP gets
//stored in next of newly delete temp; // delete operator will delete the node
//created node. // stored at temp
top=temp; // Top will contain address of }
// newly created node else
} cout<<"Stack empty";}