You are on page 1of 41

Department of

Computer Science and Engineering

DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Stack ADT
Faculty Name : Mrs.S.Vijitha
Date : 13.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Contents

 Stack ADT - Definition


 Stack ADT- Examples
 Stack ADT- Operations
 Stack ADT- Implementation
 Array implementation
 Linked list implementation

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Stack - Definition
Definition:

 Stack ADT is a data structure that stores data in


such a way that the last piece of data stored, is the
first one retrieved.

 Stack ADT is also called Last-In-First-Out(LIFO).

 Stack ADT allows all data operations at one end


only.
and Project
Management
 We can only access the top element of a stack.
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Stack - Examples
Examples of stacks in "real life":

 The stack of trays in a cafeteria.


 The stack of plates in a cupboard. 

07/09/2021
Department of Computer Science and Engineering
Stack - Operations
Push: Adds an item in the stack. If the stack is full, then
it is said to be an Overflow condition.

Pop: Removes an item from the stack. The items are


popped in the reversed order in which they are pushed.
If the stack is empty, then it is said to be an Underflow
condition.

Peek or Top: Returns top element of stack.


and Project
Management
isEmpty: Returns true if stack is empty, else false.
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Stack - operations

07/09/2021
Department of Computer Science and Engineering
Stack - operations

07/09/2021
Department of Computer Science and Engineering
Stack - Implementation
Implementation:

There are two ways to implement a stack:

 Using array

 Using linked list

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array Implementation of stack

Array implementation of Stack:

 In array implementation, the stack is formed by using


the array.

 All the operations regarding the stack are performed


using arrays. 

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of stack
Adding an element onto the stack (push operation):

 Adding an element into the top of the stack is


referred to as push operation.

 Push operation involves following two steps.

 Increment the variable Top so that it can now refer to


the next memory location.
and Project
 Add element at the position of incremented top. This
Management
(SEPM)
is referred to as adding new element at the top of the
stack.
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
Adding an element onto the stack (push operation):

 Stack is overflown when we try to insert an element


into a completely filled stack therefore, our main
function must always avoid stack overflow condition.

Algorithm:

begin
if top = n then stack full
top = top + 1 Management
and Project

stack (top) : = item;


(SEPM)

end
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
Implementation of push algorithm:

void push (int val,int n) //n is size of the stack


{
if (top == n )
printf("\n Overflow");
else
{
top = top +1;
stack[top] = val;
and Project
} Management
(SEPM)
}
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
Deletion of an element from a stack (Pop
operation):

 Deletion of an element from the top of the stack is


called pop operation.

 The value of the variable top will be incremented by


1 whenever an item is deleted from the stack.

 The top most element


and Project
of the stack is stored in an
another variable and then the top is decremented
Management
(SEPM)
by 1.
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
Deletion of an element from a stack (Pop
operation):

 The operation returns the deleted value that was


stored in another variable as the result.

 The underflow condition occurs when we try to


delete an element from an already empty stack.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of stack

Algorithm :

begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of stack
Implementation of POP algorithm:
int pop ()
{
if(top == -1)
{
printf("Underflow");
return 0;
}
else
{
and Project
return stack[top - - ];
Management
(SEPM)
}
}
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack

Peek operation:

 Peek operation involves returning the element which


is present at the top of the stack without deleting it.

 Underflow condition can occur if we try to return the


top element in an already empty stack.
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of stack

Algorithm :

PEEK (STACK, TOP)

Begin
if top = -1 then stack empty
item = stack[top]
return item
End
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Array implementation of stack
Implementation of Peek algorithm in C language:

int peek()
{
if (top == -1)
{
printf("Underflow");
return 0;
}
else
{ and Project
Management
return stack [top];
(SEPM)

}}
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
Sample code:

#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);Management
and Project

printf("*********Stack
(SEPM)
operations using array*********");

printf("\n----------------------------------------------\n");
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push(); Management
and Project

break; (SEPM)
}
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
case 2:
{
pop();
break;
}
case 3:
{
show();
break;
}
case 4: Management
and Project

{ (SEPM)

printf("Exiting....");
07/09/2021
break;}Department of Computer Science and Engineering
Array implementation of stack
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
if (top == n ) Management
and Project

printf("\n Overflow");
(SEPM)

else {
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop ()
{
if(top == -1)
printf("Underflow");
and Project
else Management
(SEPM)
top = top -1;
}
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
} and Project
} Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

Linked list implementation of stack:

 Instead of using array, we can also use linked list to


implement stack. Linked list allocates the
memory dynamically.

 However, time complexity in both the scenario is


same for all the operations i.e. push, pop and peek.

 In linked list implementation


and Project of stack, the nodes are
Management
maintained non-contiguously
(SEPM) in the memory.

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack
 Each node contains a pointer to its immediate
successor node in the stack.

 Stack is said to be overflown if the space left in the


memory heap is not enough to create a node.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

 The top most node in the stack always contains null


in its address field.

Adding a node to the stack (Push operation):

 Adding a node to the stack is referred to


as push operation.

 Pushing an element to a stack in linked list


and Project
implementationManagement
is different from that of an array
(SEPM)
implementation.
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of stack

In order to push an element onto the stack, the


following steps are involved.

 Create a node first and allocate memory to it.

 If the list is empty then the item is to be pushed as


the start node of the list.

 This includes assigning value to the data part of the


node and assign null to the address part of the
and Project
Management
node. (SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

 If there are some nodes in the list already, then we


have to add the new element in the beginning of the
list.

 For this purpose, assign the address of the starting


element to the address field of the new node and
make the new node, the starting node of the list.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack
void push ()
{
int val;
struct node *ptr =(struct node*)malloc(sizeof(struct
node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
and Project
{ Management
(SEPM)
printf("Enter the value");
scanf("%d",&val);
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of stack
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
and Project
head=ptr;Management
(SEPM)
}
printf("Item pushed");
07/09/2021 Department of Computer Science and Engineering
}}
Linked list implementation of stack

Deleting a node from the stack (POP operation):

 Deleting a node from the top of stack is referred to


as pop operation.

 Deleting a node from the linked list implementation


of stack is different from that in the array
implementation.

 The next nodeManagement


ofProject
and the head node now becomes the
head node. (SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
} and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack
Display the nodes (Traversing):

 Displaying all the nodes of a stack needs traversing


all the nodes of the linked list organized in the form of
stack.

 For this purpose, we need to follow the following


steps.

 Copy the head pointer into a temporary pointer.


and Project
Management
 Move the temporary pointer through all the nodes of
(SEPM)

the list and print the value field attached to every


node.
07/09/2021 Department of Computer Science and Engineering
Linked list implementation of stack

void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
} and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list implementation of stack

else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
} and Project
} Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Thank You

Department of Computer Science and Engineering

You might also like