Tuesday, December 30, 2014

Implement a Stack in c program



Implement a Stack

#include 
#include 
#define maxsize 5
 
struct stack
{
    int stk[maxsize];
    int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int  pop(void);
void display(void);
 
void main ()
{
    int choice;
    int option = 1;
    s.top = -1;
 
    printf ("Stack Operation\n");
    while (option)
    {
        printf ("------------------------------------------\n");
        printf ("      1    -->    push               \n");
        printf ("      2    -->    pop               \n");
        printf ("      3    -->    display               \n");
        printf ("      4    -->    exit           \n");
        printf ("------------------------------------------\n");
        printf ("Enter your choice\n");
        scanf    ("%d", &choice);
        switch (choice)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
            return;
        }
        fflush (stdin);
        printf ("Do you want to continue(Type 0 or 1)?\n");
        scanf    ("%d", &option);
    }
}
void push ()
{
    int num;
    if (s.top == (maxsize - 1))
    {
        printf ("Stack is Full\n");
        return;
    }
    else
    {
        printf ("Enter the element to be pushed\n");
        scanf ("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
    return;
}
int pop ()
{
    int num;
    if (s.top == - 1)
    {
        printf ("Stack is Empty\n");
        return (s.top);
    }
    else
    {
        num = s.stk[s.top];
        printf ("poped element is = %dn", s.stk[s.top]);
        s.top = s.top - 1;
    }
    return(num);
}
void display ()
{
    int i;
    if (s.top == -1)
    {
        printf ("Stack is empty\n");
        return;
    }
    else
    {
        printf ("\n The status of the stack is \n");
        for (i = s.top; i >= 0; i--)
        {
            printf ("%d\n", s.stk[i]);
        }
    }
    printf ("\n");
    getch();
}
 
Output
 
Stack Operation
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
0
Stack Operation
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
2
poped element is = 34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3
Stack is empty
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
50
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
60
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3
 
The status of the stack is
60
50
 
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
4

 

 

 

No comments:

Post a Comment