Sponsor Area

Linked List, Stack and Queue

Question
CBSEENCO12011527

Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents for each step of conversion.
P + ( Q - R ) * S / T

Solution

Conversion of  the given Infix expression to its equivalent Postfix expression
(P+(((Q-R)*S)/T))

INFIX STACK POSTFIX
( (  
P ( P
+ (+ P
( (+( P
Q (+( PQ
- (+(- PQ
R (+(- PQR
) (+ PQR-
* (+* PQR-
S (+* PQR-S
/ (+/ PQR-S*
T (+/ PQR-S*T
)   PQR-S*T/+

Question
CBSEENCO12011573

Write the definition of a member function PUSHGIFT() for a class STACK in C++,to add a GIFT in a dynamically allocated stack of GIFTs considering the following code is already written as a part of the program:

struct GIFT
{
	int GCODE;       //Gift Code
	char GDESC[20]; //Gift Description
	GIFT *Link;
};
class STACK
{
	Gift *TOP;
public:
	STACK(){
		TOP=NULL;
	}
	void PUSHGIFT();
	void POPGIFT();
	~STACK();
};

Solution

void PUSHGIFT( )
{
	GIFT *G = new GIFT;
	cout<<'Enter gift code and description';
	cin>>G–>GCODE;
	gets(G–>GDESC);
	if(TOP == NULL)
	{
		TOP = G;
	}
	else
	{
		G–>Link = TOP
		TOP = G;
	}
}

Question
CBSEENCO12011574

Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents for each step of conversion:
X - ( Y + Z ) / U * V

Solution

ELEMENT STACK POSTFIX
X   X
- - X
( -( X
Y -( XY
+ - ( + XY
Z - ( + XYZ
) - XYZ+
/ -/ XYZ+
U -/ XYZ + U
* -* XYZ + U/
V -* XYZ +U/V
    XYZ+U/V*-

Question
CBSEENCO12011622

Convert the following Infix expression to its equivalent Postfix expression, showing the stack contents for each step of conversion.
A/(B+C)*DE

Solution

A/(B+C)*DE
= (A / (B+C) * D E)

Element Stack of Operators Postfix Expression
( (  
A ( A
/ (/ A
( (/( A
B (/( AB
+ (/(+ AB
C (/(+ ABC
) (/ ABC+
* (* ABC+/
D (* ABC+/D
- (- ABC+/D*
E (- ABC+/D*E
)   ABC+/D*E-

ABC+/D*E