Professional Documents
Culture Documents
Ena Lab Viva 2
Ena Lab Viva 2
IMPLEMENTATION:
PASS 1:
12 23 56 38 19 45
PASS 2:
ABDUL WASAY
PASS 3:
PASS 4:
Question 2) Let C denotes the number of times LOC is updated to find the largest element
in array A with N elements. Write a procedure COUNT(A, N, C)which finds C.
ANSWER:
ABDUL WASAY
ALGORITHM:
STEP 1: START [Initialize] Set LOC=1 LARGEST=A[1]
STEP 2: Repeat STEP 3 and 4 for C=1 to C<=N, then:
STEP 3: If LARGEST< A[C] then :
LOC = C , LARGEST = A[C]
[ end of if structure ]
STEP 4: Set C++
[ end of Step 2 ]
STEP 5: Write C , LARGEST
STEP 6: END
Question 3) Write a procedure INSERT (P, T, LOC, R, S)that inserts pattern P in string T
having lengths R and S respectively, at location LOC.
ANSWER:
ALGORITHM:
STEP 1: [Initialize] Set T: =text, LOC: =position and P: =pattern
STEP 2: Set R:= SUBSTRING (T, 1, LOC-1)
STEP 3: Set S:= SUBSTRING (T, K, LENGTH (T)-LOC+1)
STEP 4: [Concatenate] Set P2:=R||P||S
STEP 5: Write: P2
STEP 6: Exit
Question 4) Can stacks be used to implement queues? If yes, show how to implement a
queue using stacks and describe the working of your algorithms (enqueue and dequeue)
using appropriate diagrams.
ANSWER: Yes, stack can be used to implement queues. We uses two stacks to implement
queues first we added element in first stack (stack_1) then then pop (removing) the element and
pushed (added) in second stack (stack_2) thus in result it reverses the arrangement (order) of
element and forms a Queue. We will discuss and describe with the help of diagram to understand
enqueue and dequeue method.
ALGORITHM [ENQUEUE(ITEM)]:
ABDUL WASAY
4
Rear Front
3
Stack_1 Stack_2
ENQUEUE
ALGORITHM [DEQUEUE]:
STEP 1: START [initialize S1 and S2]
STEP 2: [Check] if S1 = empty
Write “queue is empty” and return
STEP 3: Then;
ITEM= S1 TOP
STEP 4: pop S1 and Return ITEM
STEP 5: END
Push pop
ABDUL WASAY
pop
1
Rear Front
2
Stack_1 Stack_2
DEQUEUE
Question 5) Consider the following Tower of Hanoi with 4 discs:
Describe the working of the recursive method of moving the entire stack of discs from
SOURCE to DEST using appropriate diagram.
ANSWER)