0% found this document useful (0 votes)
282 views

Stack Operations of Linear Array: Lab Report No. 5

This document summarizes the key operations of a stack data structure using a linear array: push, pop, peek, and isEmpty. It provides code to implement these stack operations including pushing elements onto the stack, popping elements off the stack, and traversing the entire stack from top to bottom. The code includes functions for each operation, error handling for full or empty stack conditions, and sample outputs.

Uploaded by

Mian Tauseef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
282 views

Stack Operations of Linear Array: Lab Report No. 5

This document summarizes the key operations of a stack data structure using a linear array: push, pop, peek, and isEmpty. It provides code to implement these stack operations including pushing elements onto the stack, popping elements off the stack, and traversing the entire stack from top to bottom. The code includes functions for each operation, error handling for full or empty stack conditions, and sample outputs.

Uploaded by

Mian Tauseef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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