Professional Documents
Culture Documents
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
and Project
Management
(SEPM)
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.
07/09/2021
Department of Computer Science and Engineering
Stack - operations
07/09/2021
Department of Computer Science and Engineering
Stack - Implementation
Implementation:
Using array
and Project
Management
(SEPM)
and Project
Management
(SEPM)
Algorithm:
begin
if top = n then stack full
top = top + 1 Management
and Project
end
07/09/2021 Department of Computer Science and Engineering
Array implementation of stack
Implementation of push algorithm:
and Project
Management
(SEPM)
Algorithm :
begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;
and Project
Management
(SEPM)
Peek operation:
Algorithm :
Begin
if top = -1 then stack empty
item = stack[top]
return item
End
and Project
Management
(SEPM)
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)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
and Project
Management
(SEPM)
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
} and Project
Management
(SEPM)
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
} and Project
Management
(SEPM)
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
} and Project
} Management
(SEPM)