LINK LIST Full
LINK LIST Full
Linked list is a very commonly linear data structure which is consist group of nodes. Each nodes
contain two parts first part contain data part and second part contain address part
1) Singly linked list: - Singly linked list contain two part first is data part and second is address part.
The operation perform on singly linked list is insertion, deletion and traversal.
X
2) Doubly linked list: - It is containing collection of nodes; each node contains three parts. first part
contains the address of previous node second part contain data value and third part contain
address of next node.
3) Circular linked list: - It is a collection of nodes which contain three-part first part contains data
part and second part contain address but the address part of last node contains the address of first
node.
QUEUE
Queue is a linear data structure which is based on FIFO (first in first out) insertion in queue is done
from Rear and deletion is done from front.
Real time example of queue: -
1. Railways reservation counter.
2. Passenger waiting for bus.
public:
Stack() {
top = -1;
}
void pop() {
if (top == -1) {
cout << "Stack Underflow. Cannot pop." << endl;
} else {
cout << elements[top--] << " popped from the stack." << endl;
}
}
int main() {
clrscr();
Stack myStack;
int choice, value;
do {
cout << "1. Push\n2. Pop\n3. Display\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to push: ";
cin >> value;
myStack.push(value);
break;
case 2:
myStack.pop();
break;
case 3:
myStack.display();
break;
case 4:
cout << "Exiting the program.";
break;
default:
cout << "Invalid choice. Try again.";
}
getch();
return 0;
}
Infix, Postfix and Prefix
OR OR
(A + (B * D)) (A+(B*D))
(A + BD*) (A + *BD)
ABD*+ +A*BD
((A + B) * C) +AB
(A + B) * C AB+ *+ABC
AB+C* or
or ((A + B) * C)
((A + B) * C) ((+AB) * C)
((AB+) * C) *+ABC
AB+C*
((X * Y) - (Z / P))
XY* ((*XY) - (Z / P))
X*Y–Z/P (XY* - ZP/) (*XY) - (/ZP)
XY*ZP/- -*XY/ZP
or or
((X * Y) - (Z / P)) ((X * Y) - (Z / P))
(XY* - (Z / P)) (*XY - (Z / P))
(XY* - ZP/) (*XY - /ZP)
XY*ZP/- -*XY/ZP
More Examples of postfix: -
Q. A + (B – C) * D Q. A – B / C * D * E + F Q. ((A + B) – C) * D
Q. Transform each of the following expression to prefix and postfix. 3 MARKS (JAC 2019)
a) (A – B * (C + D)) / E * F
b) (A + B) – C * D
Ans:- a) (A – B * (C + D)) / E * F
Prefix Postfix
(A – B * (C + D)) / E * F (A – B * (C + D)) / E * F
(A – B * +CD) / E * F (A – B * CD+) / E * F
(A - *B+CD) / E * F (A – BCD+*) / E * F
ABCD+*- / E * F
-A*B+CD / E * F
ABCD+*-E/ * F
/-A*B+CDE * F ABCD+*-E/F*
*/-A*B+CDEF
b) (A + B) – C * D
Prefix Postfix
(A + B) – C * D (A + B) – C * D
+AB - C * D AB+ - C * D
+AB - *CD AB+ - CD*
-+AB*CD AB+CD*-
OPERANDS: - A, B, C, X, Y, P, Q, ……
OPERATORS: - +, -, *, /, (, ), ………
Rule:-
Priorities of operators: - ^ → highest priorities
*, / → next priorities
+, - → lowest priorities
No two operators of same priority can stay together in stack column.
Lowest priority cannot be placed before highest priority.
if (+) then pop it → + //if any other operators in under bracket, then pop it.
Ex-
Symbol Stack Postfix
A A
+ + A
B + AB
* +* AB
C +* ABC
+ + ABC*+
D + ABC*+D
ABC*+D+
Q. Obtain postfix notation for the following infix notation manually. 4 MARKS (JAC 2020)
a) A + C – D * B
b) (A + B) * C + D / E – F
Ans: -
a) A + C – D * B
Symbol Stack Postfix
A A
+ + A
C + AC
- +- AC+
D - AC+D
* -* AC+D
B -* AC+DB
Q. Evaluate the following postfix expression using a stack and show the contents of stock
after execution of each operation. 3 MARKS (JAC 2019)
300, 10, 30, +, 20, *, +
Ans: -
Symbol Operation Stack
300 Push 300 300
10 Push 10 300, 10
30 Push 30 300, 10, 30
+ Pop 10, 30 300
10+30=40, Push 40 300, 40
20 Push 20 300, 40, 20
* Pop 40, 20 300
40*20=800, Push 800 300, 800
+ Pop 300, 800 Empty
300+800=1100, Push 1100 1100
No more elements Pop 1100
Q. Evaluate the following postfix expression using a stack and show the contents of stock
after execution of each operation.
150, 40, 10, +, 15, 10, -, +, *
Ans: -
5 MARKS (JAC 2023)
Symbol Operation Stack
150 Push 150 150
40 Push 40 150, 40
10 Push 10 150, 40, 10
+ Pop 40, 10 150
40+10=50, push 50 150, 50
15 Push 15 150, 50, 15
10 Push 10 150, 50, 15, 10
- Pop 15, 10 150, 50
15-10=5, push 5 150, 50, 5
+ Pop 50, 5 150
50+5=55, push 55 150, 55
* Pop 150, 55 Empty
150*55=8250, push 8250 8250
No more elements Pop 8250
Q. Evaluate the following postfix expression using a stack and show the contents of stock
after execution of each operation.
5, 4, 6, +, *, 4, 9, 3, /, +, *
Ans: -
Symbol Operation Stack
5 Push 5 5
4 Push 4 5, 4
6 Push 6 5, 4, 6
+ Pop 4, 6 5
4+6=10, Push 10 5, 10
* Pop 5, 10 Empty
5*10=50, Push 50 50
4 Push 4 50, 4
9 Push 9 50, 4, 9
3 Push 3 50, 4, 9, 3
/ Pop 9, 3 50, 4
9/3=3, push 3 50, 4, 3
+ Pop 4, 3 50
4+3=7, push 7 50, 7
* Pop 50, 7 Empty
50*7=350, push 350 350
No more elements Pop 350
Q. Evaluate the following postfix expression using a stack and show the contents of stock
after execution of each operation.
1) 10, 5, +, 60, 6, /, *, 8, - Ans = 142
2) 250, 100, -, 25, /, 11, *, 9, - Ans = 57