Two Stacks in an Array

10. Program for implementing two Stacks in an array?

Algorithm

Globally,Define MAX as 5;Declare stack[MAX],top1=-1,top2=MAX,item.
Step 1:Start
Step 2:Declare variable ch.
Step3:while(ch==’1’) repeat steps 3.1 to 3.8
                   3.1)Print the Menu 1.Push1  2.Push2  3.Pop1  4.Pop2  5.Display
                   3.2)Input and read the choice,ch.
                   3.3)If ch==1 then call the function push1().
                   3.4)Else if ch==2 then call the function push2().
                   3.5)Else if ch==3 then call the function pop1().
                   3.6)Else if ch==4 then call the function pop2().
                   3.7)Else if ch==5 then call the function display().
                   3.8)Else print “invalid key pressed”.
Step 4:End while.
Step 5:Stop.
Function push1()
Step 1:If(top1=top2-1) then
                   1.1)print STACK OVERFLOW.
Step 2:Else
                   2.1)Input and read the element to be inserted,item.
                   2.2)Increment ‘top1’ by 1.
                   2.3)Assign stack[top1]=item.
Step 3:End if
Step 4:Return.
Function push1()
Step 1:If(top1=top2-1) then
                   1.1)print STACK OVERFLOW.
Step 2:Else
                   2.1)Input and read the element to be inserted,item.
                   2.2)Decrement ‘top2’ by 1.
                   2.3)Assign stack[top2]=item.
Step 3:End if
Step 4:Return.
Function pop1()
Step 1:If (top1=-1) then
                   1.1)Print STACK UNDERFLOW.
Step 2:Else
                   2.1)Print the popped element,stack[top1].
                   2.2)Decrement ‘top1’ by 1.
Step 3:End if.
Step 4:Return.
Function pop2()
Step 1:If (top2=MAX-1) then
                   1.1)Print STACK UNDERFLOW.
Step 2:Else
                   2.1)Print the popped element,stack[top2].
                   2.2)Increment ‘top2’ by 1.
Step 3:End if.
Step 4:Return.
Function display()
Step 1:Declare variable i.
Step 2:Initialize i as 0.
Step 3:while(i<=top1) repeat steps 3.1 and 3.2
                   3.1)Print the elements of stack1,stack[i].
                   3.2)Increment i by 1.
Step 4:End while.
Step 5:Initialize i as MAX-1.
Step 6:while(i>=top2) repeat steps 6.1 and 6.2
                   6.1)Print the elements of stack2,stack[i].
                   6.2)Decrement i by 1.
Step 7:End while.
Step 8:Return.

Program

#include<stdio.h>
#include<conio.h>
#define MAX 5
char stack[MAX];
int top1=-1,top2=MAX,item;
void push1()
          {
          if(top1==top2-1)
                   printf("\nOverflow");
          else
                   {
                   printf("\nEnter item\t");
                   scanf("%d",&item);
                   top1++;
                   stack[top1]=item;
               }
          }
void push2()
          {
          if(top1==top2-1)
           printf("\nOverflow");
          else  
                   {
                   printf("\nEnter the item\t");
                   scanf("%d",&item);
                   top2--;
                   stack[top2]=item;
                   }
          }
void pop1()
          {
          if(top1==-1)
                   printf("\nUnderflow");
          else
            {
                   printf(“The popped element is %d\n”,stack[top1]);
                   top1--;
               }
          }
void pop2()
          {
          if(top2==MAX-1)
                   printf("\nUnderflow\n");
          else
                   {
                   printf(“The popped element is %d\n”,stack[top2]);
                   top2++;
                   }
          }
void display()
          {
                   int i;
                   printf("\nElements of stack1 are\n ");
          for(i=0;i<=top1;i++)
                   {
                   printf("%d \t",stack[i]);
                   }
                   printf("\nElements of stack2 are\n ");
          for(i=MAX-1;i>=top2;i--)
                   {
                   printf("%d\t ",stack[i]);
                   }
       }
void main()
          {
          int ch;
          clrscr();
          do{
          printf("1.Push1\t2.Push2\t3.Pop1\t4.Pop2\t5.Display\n");
          scanf("%d",&ch);
          switch(ch)
                   {
                   case 1:
                             push1();
                             break;
                   case 2:
                             push2();
                             break;
                   case 3:
                             pop1();
                             break;
                   case 4:
                             pop2();
                             break;
                   case 5:
                             display();
                             break;
                   default:
                             printf(“Invalid Key Pressed\n”);
                   }
                   printf("\nPress 1 to continue\n");
                   scanf("%d",&ch);
                   }while(ch==1);
          getch();
          }
Output

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
1
Enter item   10
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
1
Enter item   11
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
1
Enter item   12
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
2
Enter the item       14
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
2
Enter the item       13
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
5
Elements of stack1 are
10      11      12
Elements of stack2 are
14      13
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
1
Overflow
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
2
Overflow
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
3
The popped element is 12
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
3
The popped element is 11
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
3
The popped element is 10
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
3
Underflow
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
4
The popped element is 13
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
4
The popped element is 14
Press 1 to continue
1

1.Push1                 2.Push2                 3.Pop1                   4.Pop2                   5.Display
4
Underflow
Press 1 to continue
2

No comments:

Post a Comment