Implementation of stacks using Arrays lab program

#include<stdio.h>

#include<stdlib.h>

void push();

void pop();

void peek();

void display();

# define size 5

int arr[size];

int top = -1;

int main()

{

int n;

printf("Stack Menu:-\n\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n\n");

while(1)

{

printf("Enter your choice\t");

scanf("%d",&n);

switch(n)

{

case 1: push(); break;

case 2: pop(); break;

case 3: peek(); break;

case 4: display(); break;

case 5: exit(1);

default: printf("Please enter a valid choice\n");

};

}

return 0;

}

void push()

{

if(top == size-1)

printf("Overflow Occured\n");

else

{

int n;

printf("Enter value\t");

scanf("%d",&n);

top++;

arr[top] = n;

}

}

void pop()

{

if(top == -1)

printf("Underflow Occured\n");

else

{

printf("Element %d is deleted\n",arr[top]);

top--;

}

}

void peek()

{

if(top == -1)

printf("Stack is Empty\n");

else

printf("Top element is %d\n",arr[top]);

}

void display()

{

if(top == -1)

printf("Empty stack - No elements to display\n");

else

{

int i;

for(i = top;i>=0;i--)

printf("%d  ",arr[i]);

printf("\n");

}

}

 

Comments

Popular posts from this blog

Data Structures UNIT-1 Chapter-1

Unit-2 chapter-1 STACKS AND ITS APPLICTIONS

Conversion of Infix to Postfix Notation lab program