0% found this document useful (0 votes)
10 views12 pages

LINK LIST Full

Uploaded by

skumar45753
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

LINK LIST Full

Uploaded by

skumar45753
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

LINK LIST

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

✓ Linked List representation


Linked list can be represented as a chain of nodes where every node contains next node.

Types of linked list


There are mainly three types of linked list.
1. Singly linked list
2. Doubly linked list
3. circular linked list

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.

Insertion in linked list: -


it is used to insert a new node in linked list. in linked list insertion is take place in beginning of the
linked list and ending of the linked list.
Deletion in linked list: -
it is used to remove the nodes from the linked list

Q) what is difference between overflow and underflow


Ans: - if you try to insert new node in a linked list when the memory is full then it is said to be
overflow and if you want to delete a node when it is empty then it is said to be underflow.
STACK
Stack is a linear data structure which is based on LIFO (last in first out). Hence contain chain like
structure.
Real time example of stack: -

Basic operation performs on stack: - following operation can be perform on stack.

I) PUSH(): - pushing(storing): - data elements in stack

(ii) POP():- Removing data elements from top of the stack


(iii) PEEP:- viewing the Top data elements of the stack.

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.

Enqueue: - Add an item to the queue is called enqueue.


Dequeue: - Remove an item from the queue is called dequeue.
Q. Program of pop, push and display.
#include<iostream.h>
#include<conio.h>

const int MAX_SIZE = 5;


class Stack {
private:
int elements[MAX_SIZE];
int top;

public:
Stack() {
top = -1;
}

void push(int value) {


if (top == MAX_SIZE - 1) {
cout << "Stack Overflow. Cannot push " << value << "." << endl;
} else {
elements[++top] = value;
cout << value << " pushed into the stack." << endl;
}
}

void pop() {
if (top == -1) {
cout << "Stack Underflow. Cannot pop." << endl;
} else {
cout << elements[top--] << " popped from the stack." << endl;
}
}

void display() const {


if (top == -1) {
cout << "Stack is empty. Nothing to display." << endl;
} else {
cout << "Elements in the stack: ";
for (int i = 0; i <= top; ++i) {
cout << elements[i] << " ";
}
cout << 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.";
}

} while (choice != 4);

getch();

return 0;
}
Infix, Postfix and Prefix

Infix notation: X+Y


Operators are written in-between their operands. This is the usual way we write expressions. An
expression such as A * (B + C) / D.

Postfix notation (also known as "Reverse Polish notation"): X Y +


Operators are written after their operands. The infix expression given above is equivalent to
ABC+*D/

Prefix notation (also known as "Polish notation"): + XY


Operators are written before their operands. The expressions given above are equivalent to
/*A+BCD

Infix to Postfix and Prefix

INFIX (A * B) POSTFIX (AB*) PREFIX (*AB)


(A + (B * D))
BD* *BD
A+B*D ABD*+ +A*BD

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

Sol = A + (B – C) * D Sol = A – ((B / C) * D) * E + F Sol= ((AB+) – C) * D


A + (BC-) * D A – ((BC/) * D) * E + F
(AB+C-) * D
A – (BC/D*) * E + F
A + BC-D* AB+C-D*
A – BC/D*E* + F
ABC-D*+ ABC/D*E*- + F
ABC/D*E*-F+

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*-

Q. Transform each of the following expression to postfix. 4 MARKS (JAC 2020)


a) A + C – D * B
b) (A + B) * C + D / E – F
a) A + C – D * B b) (A + B) * C + D / E – F
Ans:-
A+C–D*B (A + B) * C + D / E – F
A + C – DB* AB+ * C + D / E – F
AC+ - DB* AB+C* + D / E – F
AC+DB*- AB+C* + DE/ - F
AB+C*DE/+ - F
AB+C*DE/+F-
Another Method to solve

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

Required postfix expression is: AB+CD*-


b) (A + B) * C + D / E – F
Symbol Stack Postfix
( (
A ( A
+ (+ A
B (+ AB
) (+) AB+
* * AB+
C * AB+C
+ *+ AB+C*
D + AB+C*D
/ +/ AB+C*D
E +/ AB+C*DE
- +/- AB+C*DE/+
F - AB+C*DE/+F

Required postfix expression is: AB+C*DE/+F-

Q. Obtain postfix notation for the following infix notation manually.


(A + B / C * (D + E) – F)
Soln: -
(A + B / C * (D + E) – F)
Symbol Stack Postfix
( (
A ( A
+ (+ A
B (+ AB
/ (+/ AB
C (+/ ABC
* (+/* ABC/
( (+*( ABC/
D (+*( ABC/D
+ (+*(+ ABC/D
E (+*(+ ABC/DE
) (+*(+) ABC/DE+
- (+*- ABC/DE+*+
F (- ABC/DE+*+F
) (-) ABC/DE+*+F-
Required postfix expression is: ABC/DE+*+F-
Traverse the Postfix expression

 Steps to evaluate a POSTFIX expression


Operand: Push to Stack Stack

Operator: second_op = Stack.Pop() //(Here op = Operand)


2nd
first_op = Stack.Pop()
1st
evaluate
first_op operator second_op
Push the result into the stack

Once the expression is finished, return Stack.Pop()

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy