Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




STACKS USING ARRAYS implementation in C++

Download Attachment
/******************************************/
/*PROGRAM TO IMPLEMENT STACKS USING ARRAYS*/
/******************************************/

#include < stdio.h>
#include < conio.h>
#include < malloc.h>
#include < process.h>
#include < ctype.h>

#define SIZE 50

void menu();
void display();
int underflow();
int overflow();
void push(int);
void pop();

int stack[SIZE];
int top=-1;

void main()
{
    clrscr();
    menu();
}

void menu()
{
    int choice,item;
    printf("MENU");
    printf("\n1. Push into the stack");
    printf("\n2. Pop from stack");
    printf("\n3. Display");
    printf("\n4. Exit");
    printf("\nEnter your choice: ");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
            clrscr();
            if(overflow()==0)
            {
                printf("\nEnter the item tobe pushed: ");
                scanf("%d",&item);
                push(item);
                clrscr();
                printf("\nAfter push operation stack is:\n");
            }
            display();
            getch();
            clrscr();
            menu();
            break;
        

case 2:
            clrscr();
            if(underflow()==1)
            {
                pop();
                if(underflow()==1)
                {
                    printf("\nAfter pop operation stack is:\n");
                    display();
                }
            }
            getch();
            clrscr();
            menu();
            break;
        case 3:
            clrscr();
            if(underflow()==1)
            {
                printf("The stack is:\n");
                display();
            }
            getch();
            clrscr();
            menu();
            break;
        case 4:
            exit(1);
        default:
            clrscr();
            printf("Your choice is wrong\n\n");
            menu();
    }
}

int underflow()
{
    if(top==-1)
    {
        printf("\nStack is empty");
        return(0);
    }
    else
    {
        return(1);
    }
}

int overflow()
{
    if(top==SIZE-1)
    {
        printf("\nStack is full\n");
        return(1);
    }
    else
    {
        return(0);
    }
}

void push(int item)
{
    top=top+1;
    stack[top]=item;
}

void pop()
{
    top=top-1;
}

void display()
{
    int i;
    for(i=top;i>-1;i--)
    {
        printf("\nElement %d : %d",i+1,stack[i]);
    }
}


 
Share this article   |    Print    |    Article read by 5287 times
Author:
Rohit kakria
I am software developer
Related Articles:
Related Interview Questions: No related interview question