Home » Data Structure

How to create a Double Stack?

In this article, we are going to learn how to implement item code and price code of that item will store in respective item and its price stack?
Submitted by Manu Jemini, on December 21, 2017

We can manage multiple arrays to store different type of data and still manage them with a single index. The stack is a very important data structure because it can store data in a very practical way. The stack is a linear data structure. Stack array list follows the Last in First Out principle. The element gets to add up at the top and deleted from the top.

Double stack in DS

Image source: https://he-s3.s3.amazonaws.com/media/uploads/9a74c87.png

In the Code below there are four parts. First three function to implement three different operations like Insert an item, delete an item and display the Stack. The Fourth part is the main function, in that a do while loop is implemented to keep the user engaged and provide him the all the given choices, according to the choice one of the three function get called.

The Push function checks whether the stack is full or not. If the stack is full it’s not do anything but if not it takes two input from the user and push it to the top of both arrays.

The Pop function checks whether both arrays are empty, if not then it prints the topmost items and deletes them from both the arrays.

The Third function will simply print all the elements of both arrays if exist. If not, then it will say stack is Empty.

The Stack can hold only 4 items, for changing the capacity edit the second line.

C program to implement double stack

#include<stdio.h>

#define MaxSize 4

int itemStack[MaxSize],top=-1;
float priceStack[MaxSize];
void push();
void pop();
void display();

/*Begin of main*/
int main()
{
	int choice;
	do
	{
		printf("1.Push");
		printf("\n\n2.Pop");
		printf("\n\n3.Display");
		printf("\n\n4.Exit");
		printf("\n\nEnter Choice ");
		fflush(stdin);
		scanf("%d ",&choice);
		fflush(stdin);
		switch(choice)
		{
			case 1: push();break;
			case 2: pop();break;
			case 3: display();break;
			case 4: break;
		}
	}while(choice!=4);

	return 0;
}
/*End of main*/

/*Begin of push*/
void push()
{
	int item;
	float price;
	if(top>=(MaxSize-1))
	{
		printf("\n\nStack OverFlow");
		return;
	}
	else
	{
		printf("\n\nEnter The Item To Push");
		scanf("%d",&item);
		printf("\n\nEnter The Price");
		scanf("%f",&price);
		top++;
		itemStack[top]=item;
		priceStack[top]=price;
	}
}
/*End of push*/

/*Begin of pop*/
void pop()
{
	int item,price;
	if(top==-1)
	{
		printf("Stack Underflow");
		return;
	}
	else
	{
		item=itemStack[top];
		price=priceStack[top];
		printf("Poped item: Item:%d\tPrice:%d\n",item,price);
		top--;
	}
}
/*End of pop*/

/*Begin of display*/
void display()
{
	int i;
	if(top==-1)
	{
		printf("Stack Empty");
		return;
	}
	else
	{
		printf("\n\n\t\t\t\tItem \t\tPrice\n\n");
		for(i=top;i>=0;i--)
		{
			printf("\t\t\t\t%d   \t\t%.2f\n",itemStack[i],priceStack[i]);
		}
	}
}
/*End of display*/

Output

Double stack in DS



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.