Wednesday, October 12, 2011

2 Write a program for generate a Stack of n number of values; perform Push, Pop, Display, Peep, Change operation for selected position. Using C languge,....


#include<stdio.h>
#include<conio.h>
#define SIZE 5
void PUSH();
void POP();
void PEEP();
void DISPLAY();
void CHANGE();
int a[SIZE],item,top=0;
void main()
{
    int ch;
    clrscr();
    do
    {
       printf("\n1.push");
       printf("\n2.pop");
       printf("\n3.display");
       printf("\n4.peep");
       printf("\n5.change");
       printf("\n6.exit\n\n");
       printf("enter the choice");
       scanf("%d",&ch);
       switch(ch)
       {
           case 1:
              PUSH();
              break;
           case 2:
              POP();
              break;
           case 3:
              DISPLAY();
              break;
           case 4:
              PEEP();
              break;
           case 5:
            CHANGE();
              break;
           case 6:
              exit(0) ;
           default:
              printf("not allowed");
         }
      } while(ch<=6);
 getch();
}

void PUSH()
    {
         if(top>=SIZE)
         {
            printf("\nstack is full");
         }
        else
         {
            top++;
            printf("\nEnter the item");
            scanf("%d",&item);
            a[top]=item;
        }
     }

void POP()
    {
     if(top<=0)
      {
      printf("\nstack is empty");
      }
        else
           {
         item=a[top];
         top--;
         printf("\nitem is deleted %d",item);
          //     count --;
           }
    }
void DISPLAY()
       {
    int i;
    if(top!=0)
    {
      for(i=top;i>0;i--)
      printf("%5d",a[i]);
    }
    else
    {
    printf("\nstack is empty");

    }
       }
void PEEP()
    {
    int loc;
     if(top==0)
      {
      printf("\nstack is empty");
      }
        else
        {
        printf("\nEnter location to search");
        scanf("%d",&loc);
         if(loc<=top)
         printf("\nelement is %d",a[loc]);
         else
         printf("\nelement is not found") ;
        }

    }
void CHANGE()
    {
    int loc,ele;
    if(top==0)
    {
    printf("\nstack is empty");
    }
     else
     {
      printf("\nEnter the location to change");
      scanf("%d",&loc);
      printf("\nEnter element");
      scanf("%d",&ele);
        a[loc]=ele;
      }
     }