Showing posts with label Program - Static stack. Show all posts
Showing posts with label Program - Static stack. Show all posts

Saturday 31 March 2012

Program - Static stack


Program-  Create stack using Array 

// Stack
#include<stdio.h>
#include<conio.h>
#define size 10

int stack[size];
int tos=-1,flag=0;

void push(int x);
void pop();
int peep();
int update();
void display();

void main()
{
int choice=1,data;
while(choice>=1 && choice<6)
 {
  do
    {
      clrscr();
      printf("\n\t==================");
      printf("\n\t\t Menu");
      printf("\n\t==================");
      printf("\n\t\t1. Push");
      printf("\n\t\t2. Pop");
      printf("\n\t\t3. Peep");
      printf("\n\t\t4. Update");
      printf("\n\t\t5. Display");
      printf("\n\t\t6. Exit");
      printf("\nEnter Your Choise : ");
      scanf("%d",&choice);
    }while(choice<1 || choice>6);
  switch(choice)
    {
       case 1:
       {
        printf("\n\tPush Operation Selected\n\tEnter Value To be Pushed");
        scanf("%d",&data);
        push(data);
        if(flag==0)
          {
           printf("\nStack Is Full");
          }
        else
          {
            display();
            if(tos==size-1)
            {
              printf("\n Stack Overflow");
            }
          }
          getch();
          break;
       }
       case 2:
       {
         printf("\n\t POP Operation Selected");
         pop();
         if(flag==0)
         {
           printf("\nStack Empty");
         }
         else
          {
            if(tos==-1)
            {
              printf("\n\t Stack Underflow");
              display();
            }
          }
          getch();
          break;
       }
       case 3:
       {
         printf("\n\tPeep Operation Selected");
         data=peep();
         if(flag)
         {
           printf("\n\t Peeped Element %d",data);
           printf("\n\t Stack is as under");
           display();
         }
         else
         {
           printf("\n Stack Underflow");
         }
         getch();
         break;
       }
       case 4:
       {
         printf("\n\t Update Operation Selected");
         data=update();
         if(flag)
         {
           printf("\n\t Updated Information %d",data);
           printf("\n\t Stack ia as Under");
           display();
         }
         else
         {
           printf("\n Stack Underflow");
         }
         getch();
         break;
       }
       case 5:
       {
         printf("\n\t Display Selected");
         display();
         getch();
         break;
       }
    }
  }
  printf("\n End Of Program");
  getch();
  }


void push(int x)
{
if(tos==size-1)
 {
 flag=0;
 }
else
 {
 ++tos;
 stack[tos]=x;
 flag=1;
 }
}

void pop()
{
if(tos==-1)
 {
 flag=0;
 }
else
{
  printf("\n Popped Element %d",stack[tos]);
  tos--;
  flag=1;
}
}

int peep()
{
  int i, peeped_element;
  printf("\n\t Enter the location of value which you want to peep");
  scanf("%d",&i);
  if(tos-i+1<0)
  {
    flag=0;
    peeped_element=0;
  }
  else
  {
    flag=1;
    peeped_element = stack[tos-i+1];
  }
  return(peeped_element);
}
int update()
{
  int i,updated_element;
  printf("\n\t Enter the location of value which you want to update");
  scanf("%d",&i);
  if(tos-i+1<0)
  {
    flag=0;
    updated_element=0;
  }
  else
  {
    flag=1;
    printf("\n Possible");
    printf("\n Enter Value to Be Updated");
    scanf("%d",&updated_element);
    stack[tos-i+1]=updated_element;
  }
  return(updated_element);
  }

  void display()
  {
    int i;
    printf("\n Stack Value");
    for(i=tos;i>=0;i--)
    {
      printf("\n\t\t %d",stack[i]);
    }
  }

Posted By : Ruchita Pandya