Stack Example in C

Advertisements

Stack POP Operations Queue in C

Example of Stack in C

Program of stack is very simple when you insert any item in stack top will be increased by 1 and when you pop any item from stack top will be decreased by 1. Both insertion and deletion operation in stack perform from top of stack.

Stack Program in C

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

#define size 5

void pop();
void push();
void display();

 struct stack
  {
   int item;
   int stack[size];
  }s;

  int top=-1;

  void push()
  {
  if(top==size-1)
  {
  printf("\n stack is full");
  }
  else
  {
  top=top+1;
  printf("\n\n Enter element in stack: ");
  scanf("%d",&s.item);
  s.stack[top]=s.item;
  }
  }

  void pop()
  {
  if(top==0)
  {
  printf("\nStack is empty: ");
  }
  else
  {
  s.item=s.stack[top];
  top=top-1;
  printf("deleted data is: %d",s.item);
  }
  }

  void display()
  {
   int i;
   if(top==0)
   {
   printf("\n Stack is empty: ");
   }
   else
   {
    for(i=top;i>0;i--)
    {
    printf("\n%d",s.stack[i]);
    }
   }
  }

  void main()
  {
   char ch,c;

   do
   {
   u:
   clrscr();
   printf("\n\n1: push");
   printf("\n2: pop");
   printf("\n3: display");
   printf("\n4: exit");
   printf("\nenter your choice: ");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
	 up:
	 push();
	 break;
    case 2:
	 pop();
	 break;
    case 3:
	 display();
	 break;
    case 4:
	 exit(0);
	 break;
    default:
	 printf("\nwrong choice");
   }
   printf("\n\n Pushed an element (Y/N)");
   c=getch();
   if(c=='y'||c=='Y')
   {
   goto up;
   }
   else
   {
   goto u;
   }
   }
   while(ch!=5);
  }
stack program in c
Stack POP Operations Queue in C

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements