Class Assignment 2 Sp18-Bse-118
Class Assignment 2 Sp18-Bse-118
Write each
step of this conversion.
1. (A + B)*(C – D )
2. A ^ B * C – D + E/F
3. A/(B+C*D-E)
4. A-B*C+D/E
5. (A+B)^2 -(C-D)/2
Evaluate the postfix expression (part a) when:
ASSIGNMENT 2 DATA
A = 12 , B = 3 ,C = 7 , D = 4 ,E = 2 and F = 5
b). AB^C*D-EF/+
i. Print A
ii. Push ^ to stack
iii. Print B
iv.
v.
Mudabbar ul islam SP18-BSE-118
Pop and print ^, push * to stack
Print C
vi. Pop and print *,[COMPANY
push – to stack
NAME] [Company address]
vii. Print D
viii. Pop and print -, push + to stack
ix. Print E
x. Push / to stack
xi. Print F
xii. Pop and print /
xiii. Pop and print +
c). ABCD * + E - /
i. Print A
ii. Push / to stack
iii. Push ( to stack
iv. Print B
v. Push + to stack
vi. Print C
vii. Push * to stack
viii. Print D
ix. Pop and print *, then +, and push – to stack
x. Print E
xi. Pop and print –
xii. Pop and print /
d). ABC * - DE / +
i. Print A
ii. Push - to stack
iii. Print B
iv. Push * to stack
v. Print C
vi. Pop and print *, then pop and print -, and push + to stack
vii. Print D
viii. Push / to stack
ix. Print E
x. Pop and print / and then pop and print +
e). AB + 2 ^ CD – 2 / -
i. Push ( to stack
ii. Print A
iii. Push + to stack
iv. Print B
v. Pop and print +
vi. Push ^ to stack
vii. Print “2”
viii. Pop and print ^, and push – to stack
ix. Push ( to stack
x. Print C
xi. Push – to stack
xii. Print D
xiii. Pop and print –
xiv. Push / to stack
xv. Print “2”
xvi. Pop and print /, and then pop and print –
Solution:
Deletion:
Step 1: initialize a pointer p and point it to the front such that p=front
Step 2: check whether the queue is empty or not if it is empty print “queue is empty” else :
Step 3: now point the front pointer to its successor by front = frontnext.
Step 4: now easily delete the p pointer such as delete(p);
Display:
Step 1: initialize a pointer p and point it to the front such that p=front
Step 2: check whether the queue is empty or not if it is empty print “queue is empty” else :
Step 3: start a while loop while (p!=NULL){
Start printing the priority queue}
Step 4: traverse the queue using p=plink until you reach step 2 condition p==NULL.
Q3: A DEQUE is a data structure consisting of a list of items, on which the following
operations are possible:
PUSH ( X,D) : Insert item X on the front end of DEQUE D.
POP(D) : Remove the front item from DEQUE D and return it.
Inject(X, D) : Insert item X on the rear end of DEQUE D.
Eject(D) : Remove the rear item from DEQUE D and return it.
Write algorithms for above operations
Push(x,D):
1. If queue is full, i.e. (front == 0 && rear == size-1)||front == rear+1)
2. Print “Queue is full”
[Endif]
3. Else
4. If queue is empty (front == -1)
5. Point front and rear = 0
[Endif]
6. Else if front == 0
7. Front = size – 1
[Endelseif]
8. Else
9. Decrement front
[Endelse]
10. Array at front index = item (x)
[Endelse]
[End]
POP (): -
Remove from front
Steps: -
1. If front = -1
2. Print “Queue is empty”
[Endif]
3. Else
4. Print front (element deleted)
5. If queue has only one element (front = rear)
6. Point front and rear = -1
[Endif]
7. Else if front = size -1
8. Point front = 0
[Endelseif]
9. Else
10. Increment front once
[Endelse]
[Endelse]
[End]
Inject (x): -
Insert at rear
Steps: -
1. If queue is full, i.e. (front == 0 && rear == size-1)||front == rear+1)
2. Print “Queue is full”
[Endif]
3. Else
4. If queue is empty (front == -1)
5. Point front and rear = 0
[Endif]
6. Else if rear == size – 1
7. Rear = 0
[Endelseif]
8. Else
9. Increment rear
[Endelse]
10. Array at rear index = item(x)
[Endelse]
[End]
Eject (): -
Remove from rear
Steps: -
1. If front = -1
2. Print “Queue is empty”
[Endif]
3. Else
4. Print front (element deleted)
5. If queue has only one element (front = rear)
6. Point front and rear = -1
[Endif]
7. Else if rear = 0
8. Point rear = size - 1
[Endelseif]
9. Else
10. Decrement rear once
[Endelse]
[Endelse]
[End]