Friday, March 7, 2014

C program to implement Stack operation

#include<stdio.h>
#include<windows.h>
#define MAXSIZE 5

struct stack
{
    int stk[MAXSIZE];
    int top;
}s;

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

int main ()
{
    int choice;
    char a;
    s.top =0;

    printf ("\t\tSTACK OPERATION\n");
    while (1)
    {

   printf ("\n");
        printf (" 1.PUSH\n");
        printf (" 2.POP\n");
        printf (" 3.DISPLAY\n");
        printf (" 4.EXIT\n");
        printf ("------------------------------------------\n");

        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
     exit(0);
            break;
            default:
        printf("Invalid Option");
        }
   }
    }
void push ()
{
    int num;
    if (s.top==(MAXSIZE - 1))
    {
        printf ("Stack is Full\n");
    }
    else
    {
        printf ("Enter the element to be pushed : ");
        scanf ("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
    return;
}
int pop ()
{
   int num;

    if (s.top==0)
    {
        printf ("Stack is Empty\n");
    }
    else
    {
    printf("Enter the element to be popped : ");
scanf("%d", &num);
        num = s.stk[s.top];
        printf ("poped element is = %d", s.stk[s.top]);
        s.top=s.top-1;
    }
}
int display ()
{
    int i;
    if (s.top==0)
    {
        printf("Stack is empty\n");
    }
    else
    {
   printf ("\n The status of the stack is : ");
        for (i=1;i<=s.top;i++)
        {
            printf ("%d   ", s.stk[i]);
        }
    }
}

No comments:

Post a Comment