0
1.9kviews
Write a function in C to maintain 2 stacks in a single array?

Mumbai University > Computer Engineering > Sem 3 > Data Structures

Marks: 10M

Years: Dec 2015

1 Answer
0
6views
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX 5
void PrintErrMsg(char *Msg)
{
    clrscr();
    printf("%s \n", Msg);
    printf("Press any key to continue\n");
    getch();
}
int Push(int Stack[], int Item, int top)
{
    if(top+1==MAX)
    {
        printErrMsg("Error:Stack is full\n");
        return top;
    }
    top++;
    Stack[top]=Item;
    return top;
}
int Pop(int Stack[], int *top)
{
    int Item
    if(*top==-1)
    {
        PrintErrMsg("Error:Stack is Empty\n");
    }
    Item=Stack[*top];
    (*top)--;
    return(Item);
}
void Display(int Stack[], int top)
{
    if(top==-1)
    {
        PrintErrMsg("The stack is empty\n");
        return;
    }
    printf("Contents of stack are :\n");
    while(top!=-1)
        printf("%d\n", Stack[top--]);
}
void ShowMenu()
{
    int Stack1[Max], Stack2[MAX], top1, top2;
    int row, col, choice, Data;
    Char Ans;
    top1=-1;
    top2=-1;
    do
    {
        clrscr();
        gotoxy(5, 3);
        pprintf("Two stack");
        row=5; col=5; gotoxy(col,row);
        row++; gotoxy(col,row);
        printf("2.Push into second stack");
        row++; gotoxy(col, row);
        printf("3.Pop from the First stack");
        row++; gotoxy(col, row);
        printf("4.Pop from the second stack");
        row++; gotoxy(col, row);
        printf("5.Display First stack");
        row++; gotoxy(col, row);
        printf("6.Display second stack");
        row++; gotoxy(col, row);
        printf("7.Quit");
        row++; gotoxy(col, row);
        printf("Enter your Choice(1-7)");
        switch(choice)
        {
            case 1:print("Enter Item to push on stack 1:");
                    scanf("%d", *Data);
                    top2=Push(Stack1, Data, top1);
                    break;
            case 1:print("Enter Item to push on stack 2:");
                    scanf("%d", *Data);
                    top2=Push(Stack2, Data, top2);
                    break;
            case 3: Data=Pop(Stack1, &top1);
                    if(Data != -9999)
                    {
                        printf("The item deleted from stack1 is %d \n", Data);
                        getch();
                    }
                    break;
            case 5:Display(Stack1, top1);
                    break;
            case 6: Display(Stack2, top2);
                    break;
            case 7: printf("Are you Sure (Y/N)?");
                    Ans=getch();
                    if(toupper(Ans)=='N')
                        Choice=0;
                    break;
            default : clrscr();
                      gotoxy(5,20);
                      printf("Invalid Choice, Try again");
                      getch();      
        }

    }while(choice!=7)
}
void main()
{
    clrscr();
    ShowMenu();
    exit(0);
}

OUTPUT:

Two stack

  1. Push into first stack
  2. Push into second stack
  3. Pop from the first stack
  4. Pop from the second stack
  5. Display first stack
  6. Display second stack
  7. Quit

Enter your choice (1-7) 1

Enter Item to push on stack 1 : 11

Two stacks

  1. Push into first stack
  2. Push into second stack
  3. Pop from the first stack
  4. Pop from the second stack
  5. Display first stack
  6. Display second stack
  7. Quit

Enter your choice (1-7) 2

Enter Item to push on stack 2 : 12

Two stacks

  1. Push into first stack
  2. Push into second stack
  3. Pop from the first stack
  4. Pop from the second stack
  5. Display first stack
  6. Display second stack
  7. Quit

Enter your choice (1-7) 1

Enter Item to push on stack 1:13

Two stacks

  1. Push into first stack
  2. Push into second stack
  3. Pop from the first stack
  4. Pop from the second stack
  5. Display first stack
  6. Display second stack
  7. Quit

Enter your choice (1-7) 5

Contents of stack are :

13

11

Two stacks

  1. Push into first stack
  2. Push into second stack
  3. Pop from the first stack
  4. Pop from the second stack
  5. Display first stack
  6. Display second stack
  7. Quit

Enter your choice (1-7) 6

Contents of stack are: 12

Please log in to add an answer.