Friday 7 September 2012

Stack program in c

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

void push(int sta[],int *top,int maxstk,int ele);
void pop(int sta[],int *top,int *ele);
void display(int sta[],int ele,int top,int maxstk,int opr);

int of,uf;

void main(void)
{
    int n,t;
    int opr,el,s[10] ;

    clrscr();
    gotoxy(20,1);
    textcolor(LIGHTGRAY);
    cprintf("Program on Stacks --- Push & Pop Operations");
    gotoxy(1,3);
    cprintf("Enter the Size of Stack : ");
    cscanf("%d",&n);
    t = el = opr = 0;
    do
    {
      gotoxy(1,5);
      cprintf("1. Push an element");
      gotoxy(1,6);
      cprintf("2. Pop an element");
      gotoxy(1,7);
      cprintf("3. Exit");
      display(s,el,t,n,opr);
      do
        {
        gotoxy(1,8);
        clreol();
        cprintf("Select the Operation : ");
        cscanf("%d",&opr);
        } while (opr<1 || opr>3);
      switch (opr)
        {
        case 1 : gotoxy(1,10);
             clreol();
             cprintf("Enter the element : ");
             cscanf("%d",&el);
             of = 1;
             push(s,&t,n,el);
             if (!of)
               {
                 gotoxy(1,10);
                 clreol();
                 cprintf("The element %d is pushed",el);
               }
             break;
        case 2 : uf = 1;
             pop(s,&t,&el);
             if (!uf)
               {
                 gotoxy(1,10);
                 clreol();
                 cprintf("The element %d is popped",el);
               }
             break;
        }
    } while(opr>0 && opr<3);
    gotoxy(1,10);
    cprintf("\nPress any key to end...  ");
    getch();
}

void push(int sta[10],int *top,int maxstk,int ele)
{
    if (*top == maxstk)
      {
        gotoxy(1,10);
        clreol();
        cprintf("Stack Overflow");
      }
    else
      {
         of = 0;
         *top = *top+1;
         sta[*top] = ele;
      }
}

void pop(int sta[],int *top,int *ele)
{
    if (*top == 0)
      {
        gotoxy(1,10);
        clreol();
        cprintf("Stack Underflow");
      }
    else
     {
       uf = 0;
       *ele = sta[*top];
       *top = *top-1;
     }
}

void display(int sta[],int ele,int top,int maxstk,int opr)
{
    int i,c,r;

    gotoxy(70,3);
    cprintf("Size");
    r = 5;
    for (i=maxstk;i>0;i--,r+=2)
       {
         gotoxy(70,r);
         textcolor(LIGHTGRAY);
         cprintf("%d",i);
       }
    if (top == 0)
      {
        gotoxy(62,r);
        cprintf("Stack   0  <--Top");
      }
    if (opr == 1 && !of)
      {
        textcolor(BLACK);
        gotoxy(73,r-(top-1)*2);
        cprintf("<--Top");
        textcolor(LIGHTGRAY);
        gotoxy(62,r-top*2);
        cprintf("%d",ele);
        gotoxy(73,r-top*2);
        cprintf("<--Top");
      }
    else
    if (opr == 2)
      {
        textcolor(BLACK);
        gotoxy(62,r-(top+1)*2);
        cprintf("%d",ele);
        gotoxy(73,r-(top+1)*2);
        cprintf("<--Top");
        textcolor(LIGHTGRAY);
        gotoxy(73,r-top*2);
        cprintf("<--Top");
      }
}

No comments:

Post a Comment