Professional Documents
Culture Documents
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:
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:
procedure ImplementPush(x,Q);
MAKENULL(Q1);
while(NOT Empty(Q1)) {
temp = DEQUEUE(Q1);
ENQUEUE(temp,Q2);
ENQUEUE(Q1);
while(NOT EMPTY(Q2);{
temp = DEQUEUE(Q2);
ENQUEUE(temp,Q1);
procedure ImplementPop(Q1){
return DEQUEUE(Q1);
When we implement Stack using a Queue the push operation becomes expensive.
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)
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))
MAKENULL(S);
Push(Front(Q),S);
Dequeue(Q);
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:
Solution
Maintain another max_queue, head of that queue will be the max element
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
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))
Dequeue(MAXQ1);
If( MAXQ1.kreu=Front(Q);
ii. Fshini te gjitha perseritjet 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
else
Q.kreu=Q.kreu^.pas
end;
procedure DequeueFund(Q);
begin
if (Empty(Q) then
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;
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