Stack Implementation using Array

// Static Implementation of Stack

#include<stdio.h>

#include<conio.h>

#define N 5


int stack[5];

int top = -1;


void push(){

int x;

printf("Enter data: ");

scanf("%d",&x);

if(top==N-1)

printf("Overflow\n");

else{

top++;

stack[top]=x;

}

}


void pop(){

if(top==-1)

printf("Underflow\n");

else{

printf("Popped out element is %d\n",stack[top]);

top--;

}

}


void peek(){

if(top==-1)

printf("Empty\n");

else

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

}


void display(){

int i;

if(top==-1)

printf("Empty\n");

else{

printf("Current elements of stack are:\n");

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

printf("%d\n",stack[i]);

}

}


void main(){

int ch;

do{

printf("Enter your choice:\n 1 for Push 2 for Pop 3 for Peek 4 for Display\n");

scanf("%d",&ch);

switch(ch){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

default:

printf("Invalid Choice\n");

}

}while(ch!=0);

getch();

}


No comments:

Post a Comment