Translate

Labels

Monday 25 February 2013

STACK



  # include <stdio.h>
  # include <conio.h>
  void push();
  void pop();
  void list();
  int top=1,stack[10],z,o;
  main()
  {
int n,m,i,a,s;
    clrscr();
    printf("\t\t\t\tSTACK\n");
printf("\nEnter the size of the stack:\n");
    scanf("%d",&z);
    for(;;)
    {
      clrscr();
      printf("\t\t\t\tSTACK\n");
      printf("\n1.push");
      printf("\n2.pop");
      printf("\n3.list");
      printf("\n4.exit\n");
      printf("\nEnter your choice:");
      scanf("%d",&n);
      switch(n)
      {
case 1: if((top-1)>=z)
{
printf("The stack is full.\n");
getch();
break;
}
printf("Enter the no. of elements:\n");
scanf("%d",&m);
while(z-(top-1)<m)
{
if((top-1)>m)
break;
printf("%d elements not to be added.\n",m);
printf("Only %d elements to be added.\n\n",z-top+1);
printf("Enter the no. of elements:\n");
scanf("%d",&m);
}
printf("Enter the elements:\n");
for(i=1;i<=m;i++)
{
if (top>z)
{
printf("The stack is full.\n");
break;
}
scanf("\t%d",&a);
push(a);
}
break;
case 2: if((top-1)==0)
{
printf("The stack is empty.\n");
getch();
break;
}
printf("Enter the no. of elements to be deleted:\n");
scanf("%d",&o);
s=o;
while(s>=top)
{
if((top-1)==0)
break;
printf("%d elements not to be deleted.\n",o);
printf("Only %d elements to be deleted.\n",top-1);
printf("\nEnter the no. of elements to be deleted:\n");
scanf("%d",&s);
o=top-1;
}
for(i=0;i<s;i++)
{
if((top-1)<=0)
break;
pop();
}
getch();
break;
case 3: list();
break;
case 4: exit();
break;
default: printf("\nYour choice is wrong");
break;
      }
    }
  }
  void push(int a)
  {
      stack[top]=a;
      top++;
  }
  void pop()
  {
    printf("The popped element is: %d\n",stack[top-1]);
    top--;
  }
  void list()
  {
    int i;
    printf("stack:");
    for(i=1;i<=top-1;i++)
    printf("  --->  %d  ",stack[i]);
    printf("\n");
    getch();
  }






No comments: