Professional Documents
Culture Documents
Stacks
Basic Operations of Stack
Memory Representation
Traversal
Stacks
Stacks
A Stack is a list of elements in which an element may
be inserted or deleted only at one end, called the top of
the stack. This means that the elements are removed
from a stack in the reverse order of that in which they
were inserted into the stack.
Two operations on Stack:
1. Push is the term used to insert an item on to the
stack
2. Pop is the term used to delete an element from a
stack
Array representation of stack
Array Representation of Stack
Stacks may be represented by means of one-way list
or linear arrays. A pointer variable TOP contains the
location of the top element of the stack, and a variable
MAXSTK which gives the maximum number of
elements that can be held by the stack. The condition
TOP=0 or TOP=NULL will indicate that the stack is
empty.
XXX YYY ZZZ
1 2 3 4 5 6 7 8
TOP 3 MAXSTK 3
Linked Representation of Stack
Linked Representation of Stack
The liked representation of stack is a stack that is
implemented using a singly linked list. The INFO
fields of the nodes hold the elements of the Stack and
the LINK fields hold pointers to the neighboring
element in the stack. The START pointer of the linked
list behaves as the TOP pointer variable of the stack
and the null pointer of the last node in the list signals
the bottom of stack.
Top (START)
44,33,11,55,77,90,40,60,99,22,88,66
1. Use the first element (44) . Beginning with last number 66 scan the list from
right to left comparing each number with 44 and stopping at the first number
less than 44. the number is 22 interchange 44 with 22.
22,33,11,55,77,90,40,60,99,44,88,66
2. Beginning with 22 next scan the list in the opposite direction from left to right
comparing each number with 44 and stopping at the first number greater than
44. the number is 55 interchange 55 with 44.
22,33,11,44,77,90,40,60,99,55,88,66
Algorithm
Quick Sort this algorithm sorts
an array A with N elements.
6. [Push left Sublist on to stacks
1. [Initialize] TOP=NULL when it has 2 or more Elements]
2. [Push boundary values of A on If Beg<LOC-1 then
to stacks where A has 2 or Top=Top+1,
more elements] Lower[Top]=Beg
upper[Top]=LOC-1
if N>1 Then Top=Top+1,
Lower[1]=1, Upper[1]= N.
7. [Push Right Sublist on to stacks
3. Repeat Steps 4 to 7 while Top! when it has 2 or more Elements]
=NULL. if LOC+1<End then
4. [Pop Sublist From Stacks] Top=Top+1,
Set Beg=Lower[Top], End= Lower[Top]=LOC+1
Upper[Top] and Top=Top-1 upper[Top]=End
[End of If]
5. Call [End of step 3 loop]
Quick(A,N,BEG,END,LOC)
8. Exit
QUICK() 3) [Scan from Left to Right]
a) Repeat while
1)[Initialize] Set Left=Beg ,Right
A[LEFT]<=A[LOC] and
=End and Loc= Beg
LEFT!=LOC:
2) [Scan from Right to Left]
LEFT=LEFT+1
a) Repeat while [End of Loop]
A[LOC]<=A[Right] and
LOC !=Right: b) If LOC=LEFT then Return:
Right=Right-1 c) If A[LEFT] > A [LOC] then
[End of Loop] i) Interchange A[left] and
b) If LOC=Right then Return: A[loc]
Temp=A[LOC],
c) If A[LOC]>A[Right] then
A[LOC]=A[Left],
i) Interchange A[loc] and A[Left]=Temp.
A[Right] ii) Set LOC=Left
Temp=A[LOC], iii) GOTO Step 2.
A[LOC]=A[Right],
A[Right]=Temp.
ii) Set LOC=Right
iii) GOTO Step 3.
Example
44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66
Queues
Queues
A Queue is a linear list of elements in which deletions
can take place only at one end, called the front, and
insertions can take place only at the other end, called
the rear.
Queues are also called FIFO lists, since the first
element in a queue will be the first element out of the
queue.
Array Representation of Queues
Array Representation of Queues
FRONT contains the location of the front element of
the queue, and REAR contains the location of the rear
element of the queue. The condition FRONT=NULL
will indicate that the queue is empty.
Whenever an element is deleted from the queue, the
value of FRONT is increased by 1
FRONT=FRONT + 1
Whenever an element is added to the queue, the value
of REAR is increased by 1
REAR=REAR + 1
If REAR=N, then to insert a new ITEM in queue we reset
REAR=1 instead of N+1.
Array Representation of Queue
If FRONT=N and an element of QUEUE is deleted,
we reset FRONT=1 instead of increasing FRONT to
N+1.
Suppose that the queue contains only one element, i.e.
suppose that
FRONT=REAR != NULL
and suppose that the element is deleted. Then we
assign
FRONT:=NULL and
REAR:=NULL
to indicate that the queue is empty
Array Representation of Queue
FRONT: 1 AAA BBB CCC DDD …
REAR: 4
1 2 3 4 5 6 7 … N
Queue Q:
Front Rear
LINKQ_INSERT(INFO, LINK, FRONT,
REAR, ITEM)
1. Set INFO[NEW]=ITEM and LINK[NEW]=NULL
2. If (FRONT==NULL)
then FRONT=REAR=NEW
else
LINK[REAR]=NEW and REAR=NEW
3. Exit
LINKQ_DELETE(INFO, LINK, FRONT,
REAR, ITEM)
1. If FRONT=NULL then write: UNDERFLOW and
Exit
2. Set TEMP=FRONT
3. ITEM=INFO(TEMP)
4. FRONT=LINK(TEMP)
Deques
A deque is a linear list in which elements can be added
or removed at either end but not in the middle. The
term deque is a contraction of the name double-ended
queue.
There are two types of deques:
1. input-restricted deque: It is a deque which allows
insertions at only one end of the list but allows
deletions at both ends of the list.
2. output-restricted deque: It is a deque which allows
deletions at only one end of the list but allows
insertions at both ends of the list.
Priority Queues
A Priority Queue is a collection of elements such that
each element has been assigned a priority and such that
the order in which elements are deleted and processed
comes from the following rules:
1. An element of higher priority is processed before any
element of lower priority
2. Two elements with same priority are processed
according to the order in which they were added to
the queue
Towers of Hanoi
Tower(N,BEG,AUX,END)
1. If N=1, then
(a) write BEG->END
(b) Return
2. [Move N-1 disks from peg BEG to peg AUX]
Call TOWER(N-1, BEG, END, AUX]
3. Wrtie: BEG->END
4. [Move N-1 disks from peg AUX to peg END]
Call TOWER(N-1, AUX, BEG, END]
5. Return