Stack Implementation

Code Id 35
Date Updated 3/7/2010
Title Stack implementation  
Description
This is a program to implement stack using array. 
                                  
Codes Snippet
#include
#define MAX 50

struct stack 
{
int top;
int stack_arr[MAX];
};
pop(struct stack *);
push(struct stack *, int);
overflow(struct stack *);
underflow(struct stack *);
void display(struct stack *);

main()
{
        int choice,n;
        struct stack s;
        s=(struct stack *)malloc(sizeof(struct stack));
        s->top=-1;
        while(1)
        {
                printf("1.Pushn");
                printf("2.Popn");
                printf("3.Displayn");
                printf("4.Quitn");
                printf("Enter your choice : ");
                scanf("%d",&choice);
                switch(choice)
                {
                 case 1 :
                        if(!overflow(s))
                        {
                                printf(�n Enter item to be inserted�);
                                scanf(�%d�,&n);
                        push(s,n);
                }
                else
                {
                        printf(�n Can�t push � stack overflow�);
                        exit(1);
                }
                        break;
                 case 2:
                        if(!underflow(s))
                        {
                                pop(s);
                        break;
                                }
                else
                {
                        printf(�n Can�t pop � stack underflow�);
                        exit(1);
                }
                                break;
                 case 3:
                        if(!underflow(s)
                        {
                                display(s);
                break;
        }
                else
                {
                        printf(�n Can�t display � empty stack�);
                        exit(1);
                }
                                break;
                case 4:
                        exit(1);
                 default:
                        printf("Wrong choicen");
                }/*End of switch*/
        }/*End of while*/
}/*End of main()*/
overflow(struct stack *s)
{
        if(s->top == (MAX-1))
                return 1;
        else
                return 0;
}       
underflow(struct stack *s)
{
        if(s->top == -1)
                return 1;
        else
                return 0;
}       
push(struct stack *s, int pushed_item)
{
        int pushed_item;
        s->top++;
        s->stack_arr[s->top] = pushed_item;
}/*End of push()*/
pop()
{
                return s->stack_arr[s->top];
                s->top--;
}/*End of pop()*/
void display()
{
        printf(�%d�, s->stack_arr[s->top]);
}/*End of display()*/

Comments are closed.