You are on page 1of 5

Seminar 7

1. Implementoni veprimet POP dhe PUSH te stives kur ne dispozicione keni vetem dy radha. Hint:
Perdorni veprimet e gatshme te radhes.

2.1. Stack

In a stack, we add elements in LIFO (Last In, First Out) order. This means that the last element inserted in
the stack will be the first one removed. The basic operations of a stack are:

push — insert an element at the top


pop — remove an element from the top

2.2. Queue

In a queue, we add elements in FIFO (First In, First Out) order, meaning that the first element inserted is
the first one to be removed. The basic operations of the queue are:

enqueue — insert an element at the rear


dequeue — remove an element from the front

procedure ImplementPush(x,Q);

MAKENULL(Q1);

MAKENULL(Q2); // move all elements in Q1 to Q2

while(NOT Empty(Q1)) {

temp = DEQUEUE(Q1);

ENQUEUE(temp,Q2);

// add the element which is pushed into Stack

ENQUEUE(Q1);

// move back all elements back to Q1 from Q2

while(NOT EMPTY(Q2);{

temp = DEQUEUE(Q2);

ENQUEUE(temp,Q1);

procedure ImplementPop(Q1){
return DEQUEUE(Q1);

Time Complexity Analysis

When we implement Stack using a Queue the push operation becomes expensive.

Push operation: O(n)

Pop operation: O(1)

2. Duke perdorur si variabla hyres ne funksion nje stive dhe nje radhe, te behet renditja e anasjellte e
elementeve te radhes.

1.Pop the elements from the queue and insert into the stack. (Topmost element of the stack is the last
element of the queue)

2.Pop the elements of the stack to insert back into the queue. (The last element is the first one to be
inserted into the queue)

queue=9 8 7 6 5 4 3 2 1( 1 being the first element to be inserted)

stack= 1 2 3 4 5 6 7 8 9( 9 being the topmost element)

pop the elements of the stack,we will start from 9 and so on,until the stack is empty.

Enqueue to queue2 all the elements poped. Q2= 1 2 3 4 5 6 7 8 9( 9 being the first element).

procedure RenditjaAnasjellte(S,Q){

if (Empty(Q))

return error (‘bosh’);

while (NOT Empty(Q)){

MAKENULL(S);

Push(Front(Q),S);

Dequeue(Q);

while (NOT Empty(S)){

MAKENULL(Q2);

Enqueue(Top(S),Q2);

Pop(); // Pop(Top(S));
if ( Empty(S)){

return Front(Q2)

Dequeue(Front(Q2));}

3. Duke perdorur radhat kryeni veprimet e meposhtme, kini parasysh qe nuk duhet ndryshuar renditja e
elementeve ne radhe:

i. Fshini elementin me te madh te radhes

Solution

Maintain another max_queue, head of that queue will be the max element

When an element is enqueued

Check tail of max_queue, if tail is greater than element, then insert at tail

If tail is less than element, then remove from tail iteratively until max_queue is empty or tail element is
greater than or equal to the element

When an element is dequeued

Check if its same as max_queue's head, if it is then remove, else no action.

Concept here is: max queue must contain elements in decreasing order, any element that will get
dequeued before it gets a chance to become max must be removed from max queue

procedure MaxValue(Q){

MAKENULL(MAXQ1);

max=FRONT(MAXQ1);

if (MAXQ1.bishti>Front(Q))

Enqueue(Front(Q),MAXQ1);

if (MAXQ1.bishti<Front(Q))

while(NOT Empty(MAXQ1) || (MAXQ1.bishti>= Front(Q))do

Dequeue(MAXQ1);

If( MAXQ1.kreu=Front(Q);
ii. Fshini te gjitha perseritjet ne radhe

iii. Shtoni nje element x ne nje pozicion te deshiruar ne radhe

4. Si mund të implementohet struktura radha duke përdorur dy stiva? Krijoni dhe: Enqueue(x,Q), e cila
shton elementin x në fund të radhës Q. Dequeue(Q), e cila heq elementin x nga kreu i radhes Q. Te
perdoren veprimet e gatshme te Stives.

5. Si mund të shkruhet procedura Dequeue(Q), kur duam të heqim një element x që ndodhet në fillim
ose në fund të rradhës? Mund të shfrytëzoni paraqitjen me shënjues.

procedure DequeueFillim(Q);

begin

if (Empty(Q) then

error(“rradha eshte bosh”)

else

Q.kreu=Q.kreu^.pas

end;

procedure DequeueFund(Q);

begin

if (Empty(Q) then

error(“rradha eshte bosh”)

else

while(Q.kreu <>Q.bishti)

Q.kreu=Q.kreu^.pas // Q.bishti=Q.bishti^.pas

end;
6. Trego se cfarë është shkruar në segmentet e mëposhtme të kodit. (Q është një rradhë me numra të
plotë: x, y, z janë int).

X=3;

Y=5;

Clear(Q) ose Makenull(Q)

ENQ(5,Q)

EnQ(6,Q)

ENQ(7,Q)

ENQ(8,Q)

DEQ(x,Q)

DEQ(y,Q)

ENQ(x,Q)

ENQ(y+1,Q)

DEQ(x,Q)

ENQ(y,Q)

While notEmpty(Q) do

Begin

DEQ(Q)

Writeln(x)

End OUTPUT: 6 7 8 3 6 5

You might also like