0% found this document useful (0 votes)
70 views7 pages

Class Assignment 2 Sp18-Bse-118

The document discusses algorithms for implementing priority queues and dequeues using different data structures. For priority queues, insertion, deletion and display algorithms are provided using an array of structures, 2D array, and linked list. For dequeues, algorithms are given for push, pop, inject and eject operations using an array. The priority queue algorithms insert elements in order of priority, delete the highest priority element, and traverse elements in priority order for display. The dequeue algorithms add/remove elements from either end of the array-based deque structure.

Uploaded by

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

Class Assignment 2 Sp18-Bse-118

The document discusses algorithms for implementing priority queues and dequeues using different data structures. For priority queues, insertion, deletion and display algorithms are provided using an array of structures, 2D array, and linked list. For dequeues, algorithms are given for push, pop, inject and eject operations using an array. The priority queue algorithms insert elements in order of priority, delete the highest priority element, and traverse elements in priority order for display. The dequeue algorithms add/remove elements from either end of the array-based deque structure.

Uploaded by

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

a) Convert the following infix expression into its equivalent postfix expression.

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

STRUCTURE AND ALGORITHEM


Solution:
a). AB+CD-*
i. push left bracket in stack.
SUBMITTED TO: MR TANVEER
ii.
iii.
print value(A).
push + sign in stack.
iv. Print B.
v. Push right bracket.
vi. Pop all element between bracket and print +.
vii. Push * in stack.
viii. Push ( in stack .
ix. Print C.
x. Push – in stack .
xi. Print D.
xii. Push ) in stack.
xiii. Pop all element between bracket and print -.
xiv. Pop and print *.
Evaluation:
By putting the values, the postfix expression will become
12 3 +7 4 -*
Push 12 into stack .
Push 3 into stack.
Pop top two elements and perform operation and push 15 into stack.
Push 7 and 4 into stack .
Operator reached, pop top two numbers ,perform operation and push 3 into stack.
Pop the last two element and perform operation and print 45 .

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 –

Q2. A priority Queue can be implement using following methods:


1. Using Array of Structure
Where array elements of priority queue can have the following structure:
struct data
{
int item;
int priority;
int order;
};
2. Using 2D Array
3. Linked List
Write algorithms for insertion, deletion and display for above implementation methods

Solution:

Using Array of Structure


Insertion: -
Steps: -
enqueue(item, priority, order)
1. If order is greater than length of array
2. Print “Queue is full”
[Endif]
3. Else
4. If it is the first entry, i.e, order == 1
5. Store item, priority and order at index 0 of array
6. Point pointers p1 and p2 at index 0 of array
[Endif]
7. Else
8. Initialize int i=0, and point pointer q=p1
9. While q->order <= p2->order+1
10. If priority<q->priority
11. Shift data from the end one step to the front until the data at q is now at q->order+1
12. Store data at array[i].data
[Endif]
13. Else
14. Increment i
15. Point pointer q = &array[i]
[Endelse]
[End loop]
[Endelse]
[End]
Deletion: -
Steps: -
dequeue()
1. If p1->(order+1) is greater than p2->order
2. Print “Queue is full”
[Endif]
3. Else
4. Set p1 = &array[p1->order] //Since order is one int greater than the index
[Endelse]
[End]
Display: -
Steps: -
display()
1. Point pointer q = p1
2. While q->order is less than or equal to p2->order
3. Print data as q->data for item, priority and order
4. Shift q forward like q=array[q->order]
[End loop]
[End]
Using linked list:
Insertion:
PUSH(HEAD, DATA, PRIORITY)
Step 1: Create new node with DATA and PRIORITY
Step 2: Check if HEAD has lower priority. If true follow Steps 3-4 and end. Else goto Step 5.
Step 3: NEW -> NEXT = HEAD
Step 4: HEAD = NEW
Step 5: Set TEMP to head of the list
Step 6: While TEMP -> NEXT != NULL and TEMP -> NEXT -> PRIORITY > PRIORITY
Step 7: TEMP = TEMP -> NEXT
[END OF LOOP]
Step 8: NEW -> NEXT = TEMP -> NEXT
Step 9: TEMP -> NEXT = NEW
Step 10: End
POP(HEAD)
Step 2: Set the head of the list to the next node in the list. HEAD = HEAD -> NEXT.
Step 3: Free the node at the head of the list
Step 4: End
PEEK(HEAD):
Step 1: Return HEAD -> DATA
Step 2: End

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 = frontnext.
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=plink 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]

You might also like