Lecture 6 Stack DataStructure in C++
Lecture 6 Stack DataStructure in C++
Introduction to Stacks
• The stack is one of the most important data structures in computer
science. To understand how a stack works, think of a deck of playing
cards that is face down. We can only easily access the card that is on top.
When we want to look at the top card, there are two things we can do:
we can peek at it, but leave it on the stack, or we can pop it off. When we
pop off the top object, we are taking it off the stack. If we want to add
another card to the top of the stack, we push.
4. isEmpty( ) return true if stack is empty and false if stack is not empty
• To use an array to implement a stack, you need both the array itself and an integer.
The integer tells you either:
• Which location is currently the top of the stack, or
• How many elements are in the stack
• The top of the stack is the index of the last element added to the stack
Array Implementation of a stack
The Push Operation
• Initially when the stack is empty, TOP can have any integer value OTHER THAN any
valid index number of the array. Let top = -1;
• The first element in the empty stack goes to the 0th position of the array and the
top is initialized to value 0.
• After this, every push operation increments the top variable by 1 and inserts new
data at that particular position.
• As arrays are fixed in length, elements CAN NOT be inserted beyond the maximum
size of the array.
• Pushing data beyond the maximum size of the stack results in “data overflow”.
Array Implementation of a stack
The Push Operation (2)
Array Implementation of a stack
The Pop Operation
• The pop operation deletes the most recently entered item from the
stack.
• The variable top contains the index number of the current top position
of the stack.
#include<iostream>
using namespace std;
int stack[100],choice,n,top,x,i;
void push(void); //function declarations
void pop(void);
void display(void);
Array Implementation of a Stack| Code (cont..)
int main(){ //main menu
top=-1;
cout<<"\n Enter the size of STACK[MAX=100]:";
cin>>n;
cout<<"\n\t STACK OPERATIONS USING ARRAY"<<endl;
cout<<"\n\t--------------------------------";
cout<<"\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT";
cout<<"\n\t--------------------------------";
do{
cout<<"\n Enter the Choice:";
cin>>choice;
Array Implementation of a Stack| Code (cont..)
switch(choice) {
case 1:push(); break;
case 2:pop();break;
case 3:display();break;
case 4:cout<<"\n\t EXIT POINT ";break;
default:cout<<"\n\t Please Enter a Valid Choice(1/2/3/4)";
}
}
while(choice!=4);
return 0;
}
Array Implementation of a Stack| Code (cont..)
void push(){ //push function
if(top>=n-1){
cout<<"\n\tSTACK is over flow"<<endl;
}
else {
cout<<" Enter a value to be pushed: ";
cin>>x;
top++;
stack[top]=x;
}
}
Array Implementation of a Stack| Code (cont..)
void pop(){ //pop function
if(top<=-1){
cout<<"\n\t Stack is under flow"<<endl;
}
else{
cout<<"\n The popped elements is "<<stack[top]<<endl;
top--;
}
}
Array Implementation of a Stack| Code (cont..)
void display(){ //display function
if(top>=0){
cout<<"\n The elements in STACK \n";
for(i=top; i>=0; i--)
cout<<stack[i]<<endl;
cout<<"\n Press Next Choice"<<endl;
}
else{
cout<<"\n The STACK is empty"<<endl;
}
}
Array Implementation of a Stack| Output
(2) Linked List Implementation of a Stack
• 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 (node
*next;)
• . The data part stores the data and link part stores the address of the
next node of the linked list.
Linked List Implementation of a Stack
• NB: The top is at the HEAD AND NOT AT THE TAIL of a linked list.
Can you imagine what would happen if top was at the tail? What
would be the implication of push and pop operations? Push
would be fine if a tail pointer were provided...but Pop would be
disastrous, requiring traversal!! But because the top is at the
“head”, push and pop are essentially “direct access”, no
traversals needed!
• In implementing Stack as a linked list the top of the stack is
represented by the first item in the linked list. To implement
push: create a new node and attach it as the new first node. To
implement pop: advance the top of stack to the second item in
the list (if there is one).
Linked List Implementation of a Stack
The Push Operation
Linked List
Implementation of a Stack
The Pop Operation
Linked List Implementation of a Stack
#include<iostream> • Recall that Self Referential
Structures: are special
using namespace std; structures which contains
pointers to themselves (node
*next;).
struct node{
int reg_num;
• Here, next is a pointer of type
char name[20]; node itself.
float mark;
}; • Self Referential structures are
needed to create Linked Lists.
Linked List Implementation of a Stack (cont..)
class stack{ • class stack: It contains pointer TOP
which will point to the top of the
node *top; stack.
public :
stack(){ • A Constructor (stack())has same
top=NULL;} name as the class itself.
A constructor is automatically called
void push(); when an object is created
void pop();
void display(); • The constructor function initializes
TOP to NULL.
};
Linked List Implementation of a Stack (cont..)
void stack::push(){ • Scope resolution operator (::) in C++ is used to define a
function outside a class
node *temp;
temp=new node; • node *temp; // pointer of type node
cout<<"Enter reg_num : ";
cin>>temp->reg_num; • new operator will create a new node and address of new
node is stored in temp
cout<<"\n Enter name : ";
cin>>temp->name; • temp->next=top;
cout<<"\n Enter mark : "; // address stored in TOP gets stored in next of newly created
node.
cin>>temp->mark;
temp->next=top; • top=temp;
top=temp; // Top will contain address of newly created node
}
Linked List Implementation of a Stack (cont..)
void stack::pop(){
• if(top!=NULL) // if stack is not empty
if(top!=NULL){
node *temp=top;
• node *temp=top; // temp is a pointer
top=top->next; containing address of first node
cout<<temp->reg_num<<temp-
>name<<temp->mark
• top=top->next; // top will now contain address
<<"deleted"; of second node
delete temp;
} • delete temp; // delete operator will delete the
else node stored at temp
cout<<"Stack empty";
}
do{
cout<<endl<<"------------------"<<endl;
cout<<"\nSTACK OPTION \nP PUSH \n
void stack::display(){ O POP \nD DISPLAY \nQ QUIT\n";
cout<<endl<<"------------------"<<endl;
node *temp=top;
cin>>ch;
while(temp!=NULL){
switch(ch){
cout<<temp->reg_num<<temp>name<<temp->mark<<" ";
case 'P':
temp=temp->next;
st.push();break;
} case 'O':
} st.pop();break;
case 'D':
st.display();break;
int main(){ }
stack st; }while(ch!='Q');
char ch;
return 0;}
Arrays vs Linked-List Implementations
Array Implementation Linked List Implementation
• https://
www.youtube.com/watch?v=sFVxsglODoo&list=PL2_aWCzGMAwI3
W_JlcBbtYTwiQSsOTa6P&index=15
• https://www.youtube.com/watch?v=MuwxQ2IB8lQ&list=PL2_aWCz
GMAwI3W_JlcBbtYTwiQSsOTa6P&index=16