Seminar 7-Radha Zgjidhje

You might also like

Download as pdf or txt
Download as pdf or txt
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.

Procedure Push(x , Q1, Q2)


begin
MAKENULL(Q2)
ENQUEUE(x,Q2)
while !EMPTY(Q1) do
begin
y=FRONT(Q1)
DEQUEUE(Q1)
ENQUEUE(y,Q2)
end
while !EMPTY(Q2) do
begin
y=FRONT(Q2)
DEQUEUE(Q2)
ENQUEUE(y,Q1)
end
end

Procedure POP(Q1, Q2)


Begin
DEQUEUE (Q1);
end
2. Duke perdorur si variabla hyres ne funksion nje stive dhe nje radhe, te behet renditja e anasjellte e
elementeve te radhes.
procedure rradha(Q,S)
begin
MAKENULL(S);
while(Not EMPTY(Q) )do
begin
x=FRONT(Q);
DEQUEUE(Q);
PUSH (x,S);
end;
while (Not EMPTY(S)) do
begin
x= TOP(S);
POP(S);
ENQUEUE(x, Q);
End;
end;
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
Function GjejMaxQueue(Q1)
Begin
MAKENULL(Q2)
max=FRONT(Q1);
while !EMPTY(Q1) do
begin
x=FRONT(Q1)
if (x>=max)
max=x
DEQUEUE(Q1)
ENQUEUE(x, Q2)
end
while !EMPTY(Q2) DO
begin
x= FRONT(Q2)
if (x!=max)
ENQUEUE(x,Q1)
DEQUEUE(Q2)
end
end

ii. Fshini te gjitha perseritjet ne radhe (Detyre Shtepie)


iii. Shtoni nje element x ne nje pozicion te deshiruar ne radhe
Procedure ShtoElPoz (k , Q1, x)
Begin
i=0;
MAKENULL(Q2);
While Not Empty (Q1) do
Begin
If i <> k then
Begin
Y = FRONT(Q1);
ENQUEUE (y, Q2);
DEQUEUE (Q1);
i =i +1;
end
else begin
ENQUEUE (x, Q2);
i =i +1;
end

While Not Empty (Q2) do


Begin
Y = FRONT(Q2);
ENQUEUE (y, Q1);
DEQUEUE (Q2);
end
4. Si mund të implementohet struktura radha duke përdorur dy stiva? Krijoni dhe:
Enqueue(x,S1, S2), e cila shton elementin x në fund të radhës Q.
Dequeue(S1, S2), e cila heq elementin x nga kreu i radhes Q.
Te perdoren veprimet e gatshme te Stives.

Procedure Enqueue(x,S1, S2)


Begin
PUSH (x, S1);
End

Procedure Dequeue (S1, S2)


Begin
Makenull (S2);
While not Empty(S1) do
Begin
x= TOP(S1);
POP(S1)
Push(x, S2);
end

POP(S2);

While not Empty(S2) do


Begin
x= TOP(S2);
POP(S2)
Push(x, S1);
End;

End;
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 DEQUEUE_BegLast(Q)
Begin
If Empty (Q) then error (“Radha bosh”);
Else
If Q.kreu^. element = x then Q.kreu =Q.kreu^.pas;

Else
Begin
p =Q.kreu;
While p ^.pas <> Q.bishti do
p = p^.pas;

Q.bishti = p;

Q.bishti^.pas =nil;
End;
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=7;
Y=6;
Clear(Q) ose Makenull(Q)
ENQ(5,Q)
EnQ(6,Q)
ENQ(7,Q)
ENQ(8,Q) 7 7 7 7 7
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

You might also like