Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

24

Lab Report no. 5

Stack Operations of Linear Array


Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Mainly the following three basic operations are performed in the stack:
 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.
 isEmpty: Returns true if stack is empty, else false.

Main Program:
#include<stdio.h>
#define size 5
int n;
int a[5];
int stack[size];
int top=0;
void stack_operation();
void pushing();
void pop();
void transverse();
int main(){
int ch=0;
while(ch<9){
printf("\t1-creation""\n\t2-insertion""\n\t3-deletion""\n\t4-searching""\n\t5-sorting""\n\t6-
stack Operation""\n\t7-transverse""\n\t8-exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1: break;
case 2: break;
Roll no.16-ELE-03
25

Lab Report no. 5

case 3: break;
case 4: break;
case 5: break;
case 6:stack_operation();break;
case 7:transverse();break;
case 8:printf("Program is Terminated");break;
default:return 0;break;}}}
Stack Operation Code:
void stack_operation(){
int op;
do{
printf("\n\t1-Pushing""\n\t2-Popup""\n\t3-Transverse""\n\t4-Exit\n");
printf("enter you option:");
scanf("%d",&op);
switch(op){
case 1:pushing();break;
case 2:pop();break;
case 3:transverse();break;
default:break;}} while(op<4);
}
Code of Push Operation: output of Push Operation:

void pushing(){
int x;
if(top>=size)
printf("stack is full.");
else{
printf("enter the data for
pushing.");
scanf("%d",&x);
stack[top]=x;
top++;}}
Code of Pop Operation:
void pop(){
if(top==0){
printf("\n\t Stack is under flow");}
else{
printf("\n\t The popped elements
is %d",stack[top]);
top--;}}
Transverse Code:
void transverse(){
int i;
Roll no.16-ELE-03
26

Lab Report no. 5

if(top==0)
printf("stack is empty.");
else{
for(i=top-1;i>=0;i--){
printf("\nstack %d=%d",i,stack[i]);}}}
Otput of Enque Operation:

Roll no.16-ELE-03

You might also like