C program to implement stack data structure

Simple C program to implement stack data structure with following operations:

  • Push
  • Pop
  • Peek
  • Change
  • Delete

Code:

#include<stdio.h>
#include<conio.h> 

int stack[100],top=-1,n;

void display(){

int i;

for(i=0;i<n;i++)

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

}

void push(){

int element;

if(top==n-1)

printf("The stack is Full");



else

{ printf("Enter the Element: ");

scanf("%d",&element);

top++;

stack[top]=element;

}

}

void pop(){

if(top==-1)

printf("The stack is Underflow");

else

printf("%d is popped",stack[top]);

top--;

}





main(){

int option;

clrscr();

printf("\nEnter the number of elements in Stack: ");

scanf("%d",&n);

do{

printf("\n*****Stack menu*****");

printf("\n1 Insert value to stack");

printf("\n2 Remove value from stack");

printf("\n3 Display the stack");

printf("\n4 Exit");

printf("\nEnter your choice: ");

scanf("%d",&option);



switch(option)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

}

}

while(option!=4);

printf("\nThe End");

getch();

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization