Completely Solved C, C++ Programs Assignment.




C program to implement a Di-stack.

Filed Under:

PROBLEM STATEMENT: 
Write a C program to implement a Di-stack.

THEORY:
 In computer science, a stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds an item to the top of the stack, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.
When two stacks are implemented with the help of a single array such that the user can perform both push and pop operations on both the stacks considering the stack base of the first and second stack to be the first and last index location of the array respectively, it is known to be a Di-Stack.

ALGORITHM:
Begin
Initialize globally variables top1,top2 & s
Begin function main()
Read s //Size of di-stack
Allocate size s into array a
Initialize stack top pointers top1-1,top2s
do
Print ‘1. PUSH 2. POP 3. DISPLAY 4. EXIT ‘
Read ch //Users choice
switch ch
case 1:push(a)
break
case 2:pop()
break
case 3:display(a)
break
case 4:Print’Terminating’
break
If ch does not match any of the cases mentioned Print’Wrong choice entered, Enter again !!’
while ch is not equal to 4
End main
Begin function push //Function to implement push operation
Read ch //Stack no.
if (top1+1) is equal to (top2)
Print ‘Stack overflow, please pop elements before you push
else
switch ch
case 1: Increment top1 by 1
Read e //Accepting element
a[top1]e
break

case 2: Decrement top2 by 1
Read e //Accepting element
a[top2]e
break
If ch does not match any of the cases mentioned
Print ‘Wrong choice entered, enter again’
End function push

Begin function pop //Function to implement pop operation
Read ch //Accept stack no from user
Switch ch
case 1: if top1 is less than equal to -1
Print ‘There is no element in the stack to be popped’
else
Decrement top1 by 1
break

case 2: if top2 is less than equal to s
Print ‘There is no element in the stack to be popped’
else
Increment top2 by 1
break

If ch does not match any of the cases mentioned
Print ‘Wrong choice entered, enter again’
End function pop

Begin function display
Print ‘Stack 1 consists of ‘
for itop1 is greater than equal to 0, Decrement i
Print a [i]
Print ‘Stack 2 consists of ‘
for itop2 is less than s, Increment i
Print[i])
End function display
End

PROGRAM LISTING:
//Program to implement a push, pop and display operations for a DI-STACK
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

//Global Variables
int top1,top2,s;

void main()
{
int *a;
int ch;
void push(int *);
void pop();
void display(int *);
clrscr();
//Accepting size of di-stack
printf("Enter size of di-stack n");
scanf("%d",&s);
a=(int *)malloc(s*sizeof(int));

top1=-1,top2=s;//Initializing stack pointers
do
{
printf("n");
printf(" 1. PUSH 2. POP 3.DISPLAY 4. EXIT n");
printf("n");
printf("Enter choice n");
scanf("%d",&ch);

//Switch through different operations according to users choice
switch(ch)
{
case 1:push(a);
break;
case 2:pop();
break;
case 3:display(a);
break;
case 4:printf("Terminating n");
break;
default:printf("Wrong choice entered,Enter again !! n");
}//end of switch
}//end of do
while(ch!=4);
}//end of main

//Function to implement push operation
void push(int *a)
{
int ch,e;
printf("Enter Stack No. n");
scanf("%d",&ch);

if((top1+1)==(top2))
printf("Stack overflow,please pop elements before you push n");
else
{
//switch operation to switch between the two stacks
switch(ch)
{
case 1: top1++;
printf("Enter element n");
scanf("%d",&e);
a[top1]=e;
break;

case 2: top2--;
printf("Enter element n");
scanf("%d",&e);
a[top2]=e;
break;

default:printf("Wrong choice entered,enter againn");
}//end of switch
}//end of else
}//end of push

//Function to implement pop operation
void pop()
{
int ch;
printf("Enter Stack No. n");
scanf("%d",&ch);

//switch operation to switch between the two stacks
switch(ch)
{
case 1: if(top1<=-1)
printf("There is no element in the stack to be popped n");
else
top1--;
break;

case 2: if(top2>=s)
printf("There is no element in the stack to be popped n");
else
top2++;
break;

default:printf("Wrong choice entered,enter againn");
}//end of switch
}//end of pop

//Function to implement display operation
void display(int *a)
{
int i;
clrscr();
//Printing stack 1
printf("Stack 1 consists of :n");
for(i=top1;i>=0;i--)
printf("%d n",a[i]);

//Printing stack 2
printf("Stack 2 consists of :n");
for(i=top2;i<s;i++)
printf("%d n",a[i]);
}//end of display

OUTPUT:
Enter size of di-stack
3

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
1
Enter Stack No.
1
Enter element
1

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
1
Enter Stack No.
1
Enter element
1

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
1
Enter Stack No.
1
Enter element
1

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
2
Enter Stack No.
1

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
2
Enter Stack No.
1

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
1
Enter Stack No.

2
Enter element
2

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
1
Enter Stack No.
2
Enter element
3

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
3

Stack 1 consists of :
1
Stack 2 consists of :
3
2

1. PUSH 2. POP 3.DISPLAY 4. EXIT

Enter choice
4
Terminating

DISCUSSION:
Keep a check for overflow and underflow conditions.
Keep a check for choice entered not in the list of options in the menu.
Define function prototypes before writing down the function itself.




Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments