You are on page 1of 36

UNIT III

CHAPTER – 5
QUEUES
5.1 DEFINITION:
A queue is an ordered collection of homogeneous data elements, in which deletion can take
place only at one end called the FRONT and insertion can take place only at the other end called
REAR.
A data in a queue is processed in the same order as it entered, that is, on a First-In-First Out
basis this is why a queue is also termed first-in-first out (FIFO).

A queue is also a linear data structure like a stack. The only difference between a stack and a
queue is that in the case of stack insertion and deletion (Push & Pop) operations are at one end (top)
only, but in a queue insertion(called ENQUEUE) and deletion (called DEQUEUE) operations take
place at two ends called the REAR and FRONT of the queue.
An element in a queue is termed ITEM; the numbers of elements that a queue can
accommodate is termed as LENGTH.
ENQUEUE: To insert an element to queue.
DEQUEUE: To delete an element from the queue.
5.1.1 EXAMPLES OF QUEUE:
1. Queue in front of a counter:
There are a number of customers in front of a counter to get service (to collect ticket or to
withdraw/deposit money in a bank).
In this case the customers are forming a queue and they will be served in the order they
arrived that is a customer who comes first will be served first.
2. Traffic control at a turning point:
Suppose there is a turning point in the highway where the traffic has to turn, all the traffic will
have wait in the line till it gets the signal moving on getting the "GO" signal the vehicle will turn on the
first come, first turn basis.
3. Process synchronization in multi-user environment:
In the multi-user environment, more than one process is handled, in the order to synchronize
the execution of processes, it maintain queue where process which entered first will be scheduled in
CPU first.
4. Resource sharing in a computer centre:
In the computer centre, Where resources are limited compared to the demand, users must sign
a waiting register. The user who has been waiting for a terminal for the longest period of time gets
hold of the resource first, then second and so on.
5.2 REPRESENTATION OF QUEUES
There are the ways to represent a queue in memory.
Using an array
Using a linked list
The first kind of representation users a one-dimensional array and second representation uses
a double linked list.
5.2.1 REPRESENTATION OF A QUEUE USING AN ARRAY:
A one-dimensional array a Q[1......N] can be used to represent a queue.

With this representation, two pointers namely, FRONT and REAR are used to indicate the two
ends of queue.
Three states of a queue with the representation of the given below:
Queue is empty
FRONT = 0
REAR = 0
Queue is full
REAR = N
FRONT = 1
Queue contains elements ≤ 1
FRONT ≤ REAR
Numbers of elements = REAR - FRONT + 1
OPERATIONS ON QUEUE USING ARRAY:
Suppose the current state of the queue is FRONT=2, REAR=5.

Inserting:
Deleting:

ALGORITHM FOR ENQUEUE:


STEPS:
1. If(REAR=N)then
2. Print "Queue is full"
3. Exit
4. Else
5. If (REAR=0)and(FRONT=0)then
6. FRONT=1
7. End if
8. REAR=REAR+1
9. Q[REAR]=ITEM
10. Endif
11. Stop
ALGORITHM FOR DEQUEUE:
STEPS:
1. If (FRONT=0)then
2. Print "Queue is empty"
3. Exit
4. Else
5. ITEM=Q[FRONT]
6. If(FRONT=REAR)
7. REAR=0
8. FRONT=0
9. Else
10. FRONT=FRONT+1
11. Endif
12. Endif
13. Stop
5.2.2 REPRESENTATION OF A QUEUE USING A LINKED LIST:
Dynamic we create a node whenever it is required hence representation of queues using
linked list is more efficient than array representation of queues.
Here, We select a double linked which allows moving both ways

The pointers FRONT and REAR point the first node and the last node in the list.
Two states of the queue is,
Queue is empty
FRONT=REAR=HEADER
HEADER→RLINK=NULL
Queue contains at least one element
Header→RLINK≠NULL
ALGORITHM FOR ENQUEUE USING DLL:
STEPS:
1. REAR=HEADER
2. While(REAR→RLINK≠NULL)do
3. REAR=REAR→RLILNK
4. End while
5. new=GetNode(NODE)
6. If (new≠NULL)then
7. new→LLINK=REAR
8. REAR→RLINK=new
9. new→RLINK=NULL
10. new→Data=X
11. REAR=New
12. Else
13. Print "Insertion is not possible"
14. Endif
15. Stop
ALGORITHM FOR DEQUEUE USING DLL
STEPS:
1. FRONT=HEADER→RLINK
2. If (FRONT=NULL)then
3. Print "Queue is empty"
4. Exit
5. Else
6. FRONT1=FRONT→RLINK
7. HEADER→RLINK=FRONT1
8. FRONT1→LLINK=HEADER
9. Endif
10. Return Node(FRONT)
11. Endif
12. Stop
5.3 VARIOUS QUEUE STRUCTURES
5.3.1 CIRCULAR QUEUE:
In ordinary queue, when the REAR pointer reaches the end insertion will be avoid even if
space is available at the front, So the advantages of ordinary queue is wastage of memory. One way to
avoid this is use a circular array.
An circular array is the same as an ordinary array i.e., A[1...N],but logically it implies that A[1]
comes after A[N] or after A[N],A[1]appears.

Both pointers will move in the clockwise direction. This is controlled by the MOD operation.
For example,
If the current pointer is at i then shift to next location will be i MOD LENGTH+1.
With this principle the two states of queue regarding the empty or full.
Circular queue is empty
FRONT = REAR = 0
Circular queue is full
FRONT = (REAR MOD LENGTH) + 1
(i.e.) FRONT = REAR – 1, REAR = FRONT + 1
ALGORITHM FOR ENQUEUE-CQ
STEPS:
1. If(FRONT=0)then
2. FRONT=1
3. REAR=1
4. CQ[REAR]=ITEM
5. Else
6. next=(REAR MOD LENGTH) + 1
7. If (next ≠ FRONT)then
8. REAR=next
9. CQ[REAR]=ITEM
10. Else
11. Print "Queue is full"
12. Endif
13. Endif
14. Stop
ALGORITHM FOR DEQUEUE-CQ
STEPS:
1. If (FRONT=0)then
2. Print "Queue is empty"
3. Exit
4. Else
5. ITEM=CQ[FRONT]
6. If(FRONT=REAR)then
7. FRONT=0
8. REAR=0
9. Else
10. FRONT=(FRONT MOD LENGTH) + 1
11. Endif
12. Endif
13. Stop
5.3.2 DEQUE:
A deque is a linear list, Where both insertion and deletion operations can be made at either
end of the structure.
The term deque has originated from double ended queue.
A Deque Structure:

A deque can be used as a stack as well as a queue.


One simpler way to represent it is by using a double linked list. Another popular
representation is using a circular array.
The following four operations are possible on a deque,
1. Push_DQ(ITEM): TO insert ITEM at the FRONT end of a deque
2. Pop_DQ(): To remove the FRONT item from a deque
3. Inject(ITEM): To insert ITEM at the REAR end of a deque
4. Eject(): To remove the REAR ITEM from a deque
There operations are described for a deque based on the circular array of length LENGTH.
Let the array be DQ [1....LENGTH]
ALGORITHM PUSH-DQ:-
STEPS:
1. If (FRONT=1)then
2. ahead=LENGTH
3. Else
4. If (FRONT=LENGTH) or (FRONT=0)then
5. ahead=1
6. Else
7. ahead = FRONT - 1
8. Endif
9. If(ahead=REAR)then
10. Print "Deque is full"
11. Exit
12. Else
13. FRONT=ahead
14. DQ[FRONT]=ITEM
15. Endif
16. Endif
17. Stop
ALGORITHM POP-DQ
STEPS:
1. If (FRONT=0)then
2. Print "queue is empty"
3. Exit
4. Else
5. ITEM=DQ[FRONT]
6. If(FRONT=REAR)then
7. FRONT=0
8. REAR=0
9. Else
10. FRONT=(FRONT MOD LENGTH)+1
11. Endif
12. Endif
13. Stop
ALGORITHM INJECT
STEPS:
1. If (FRONT=0)then
2. FRONT=1
3. REAR=1
4. DQ[FRONT]=ITEM
5. Else
6. next =(REAR MOD LENGTH)+1
7. If(next≠FRONT)then
8. REAR=next
9. DQ[REAR]=ITEM
10. Else
11. Print "Queue is full"
12. Endif
13. Endif
14. Stop
ALGORITHM EJECT-DQ
STEPS:
1. If (FRONT=0)then
2. Print "Deque is empty"
3. Exit
4. Else
5. If (FRONT=REAR)then
6. ITEM=DQ[REAR]
7. FRONT=REAR=0
8. Else
9. If (REAR=1)then
10. ITEM=DQ[REAR]
11. REAR=LENGTH
12. Else
13. If (REAR=LENGTH)then
14. ITEM=DQ[REAR]
15. REAR=1
16. Else
17. ITEM=DQ[REAR]
18. REAR=REAR-1
19. End if
20. End if
21. End if
22. End if
23. Stop
There are two variations of deque
Input-restricted deque
Output-restricted deque
Input-Restricted Deque:
In this case, deque allows insertion at one end only, but allows deletions at both ends.

Output Restricted Deque:


In output restricted deque, deletions take place at one end only, but allows insertions at both
ends.

5.3.3 PRIORITY QUEUES:


A priority queue is a collection of elements such that each element has been assigned a priority
and an element can be inserted or deleted not only at the ends but at any position on the queue.
With this structure an elements, an elements of priority pi may be deleted before an element
which is at FRONT
Similarly, insertion of an element is based on its priority, instead of adding it after the rear.
A priority queue doesn't strictly follow the first-in-first out (FIFO), in which elements are
deleted or processed comes from following rules.
1. An element of higher priority is processed before any elements of lower priority.
2. Two elements with the same priority are processed according to the order in which they were
added to the queue.
There are various ways of implementing the structure of a priority queue, in memory, one of
them is representation of a priority queue is linked list.
Linked List Representation of a Priority Queue:
Node structure for priority queue,

LLINK and RLINK are two usual link fields, DATA to store the actual content and PRIORITY
is to store priority value of the item.

With this structure, to delete an item having priority P, the list will be searched starting from
the node under pointer REAR and the first occurring node with PRIORITY=P will be deleted.
Similarly, to insert a node containing an item with priority P, the search will begin from the
node under the pointer FRONT and the node will be inserted before a node found first with priority
value p or if not found then before a node with the next priority value.
ALGORITHM INSERT-PQ
STEPS:
1. ptr=HEADER
2. new=GetNode(NODE)
3. new→DATA=ITEM
4. new→PRIORITY=P
5. while(ptr→RLINK=NULL)and(ptr→PRIORITY<P)do
6. ptr=ptr→RLINK
7. Endwhile
8. If (ptr→RLINK=NULL)then
9. ptr→RLINK=new
10. new→LLINK=ptr
11. new→RLINK=NULL
12. REAR=new
13. Else
14. If(ptr→priority≥P)then
15. ptr1=ptr→LLINK
16. ptr1→RLINK=new
17. new→RLINK=ptr
18. ptr→LLINK=new
19. new→LLINK=ptr1
20. Endif
21. Endif
22. FRONT=HEADER→RLINK
23. STOP
ALGORITHM DELETE-DQ:-
STEPS:
1. If (REAR=NULL)then
2. Print "Queue is empty"
3. Exit
4. Else
5. ptr=REAR
6. While(ptr→PRIORITY>P)or(ptr≠HEADER)do
7. ptr=ptr→LLINK
8. Endwhile
9. If (ptr=HEADER)or(ptr→PRIORITY<P)
10. Print "No item with priority", P
11. Exit
12. Else
13. If (ptr→priority=p)then
14. ptr1=ptr→LLINK
15. ptr2=ptr→RLINK
16. If(ptr=REAR)
17. REAR=ptr1
18. ptr1→RLINK=NULL
19. Else
20. ptr1→RLINK=ptr1
21. ptr2→LLINK=ptr2
22. Endif
23. Endif
24. Endif
25. item=ptr→DATA
26. ReturnNODE(item)
27. Endif
28. Stop
5.4 APPLICATIONS OF QUEUES
Simulation is modeling of a real-life problem (or) it is the model of a real-life situation in the
form of a computer program.
CPU scheduling in a multiprogramming environment
Round Robin Algorithm:
The Round Robin (RR) algorithm is a well-known scheduling algorithm and is designed
especially for time sharing systems.
CHAPTER - 6
LINKED LIST
6.1 DEFINITION:
A linked list is an ordered collection of finite homogeneous data elements called nodes,
where each node divided into two parts.
 DATA FIELD
 LINK FIELD
DATA field used to store the actual information; LINK field used to point next node.

The linked list can be classified into three major groups,


 Single Linked List
 Double Linked List
 Circular Linked List
6.2 SINGLE LINKED LIST:
In a single linked list each node contains only one link which points to the subsequent node
in the list.

Here N1, N2, N3, N4 are the nodes in the list, HEADER is an empty node and only used to
store a pointer to the first node N1, thus if one knows the address of the HEADER node from the link
field of this node, the next node can be traced and so on.
The single linked list can move left to right only, that is why single linked list is also called
one way list.
6.2.1 Representation of a Linked List in memory:
There are two ways to represent a linked list in memory.
 Static representation using Array
 Dynamic representation using free pool of storage
Static representation:
In static representation of a single linked list, two arrays are maintained, one array for data
and the other for links.
Two parallel arrays of equal size are allocated which should be sufficient to store the entire
linked list.
Dynamic representation:
In this method, there is a memory bank (collection of free memory spaces) and memory
manager (program).
During the creation of a linked list, when a node is required the request is placed to the
memory manager, free memory manager will search the memory bank, if found, grants the desired
block to the caller.
There is also another program called the garbage collector, it plays whenever a node is no
more in use it returns the unused node to the memory bank. Such memory management is known as
dynamic memory management.

A list of available memory spaces are stored in AVAIL, for a request of a node, the list AVAIL
is searched for the block of right size.
If AVAIL is null or if the block of desired size is not found, the memory manager will return a
message accordingly.
Suppose the block is found and let it be XY then the memory manager will return the pointer
of XY to the caller in a temporary buffer i.e., NEW.
The newly availed node XY then can be inserted at any position in the linked list by changing
the pointers of the concerned nodes.
The pointers which are required to be manipulated while returning a node are shown with
dotted arrows.
6.2.2 OPERATIONS ON A SINGLE LINKED LIST:
The operations on a single linked list are,
 Traversing the list
 Inserting a node into the list
 Deleting a node from the list
 Copying a list to make a duplicate of it
 Merging the linked list with another one to make a larger list
 Searching for an element in the list
Traversing a Single Linked List:
In traversing a single linked list, we visit every node in the list starting from the first node to
the last node.
Algorithm:
Step 1: ptr = HEADERLINK
Step 2: While (ptr ≠ NULL) do
Step 3: Process (ptr)
Step 4: ptr = ptrLINK
Step 5: End while
Step 6: Stop
Inserting a node into a Single Linked List:
There are various positions where a node can be inserted.
1. Inserting at the front (as a first element)
2. Inserting at the end (as a last element)
3. Inserting at any other position.
Inserting a node at the front of a Single Linked List:

Algorithm:
Step 1: new = GetNode(NODE)
Step 2: If(new = NULL) then
Step 3: Print “Memory underflow: No insertion”
Step 4: Exit
Step 5: Else
Step 6: newLINK = HEADERLINK
Step 7: newDATA = X
Step 8: HEADERLINK = new
Step 9: End if
Step 10: Stop
The above algorithm is used to insert a node at the front of a single linked list.
Inserting a node at the end of the Single Linked List:
In this case, a node will be inserted at the end of a linked list.

Algorithm:
Step 1: new = GetNode (NODE)
Step 2: If (new = NULL) then
Step 3: Print “Memory is insufficient: Insertion is not possible”
Step 4: Exit
Step 5: Else
Step 6: ptr = HEADER
Step 7: While (ptrLINK ≠ NULL) do
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: ptrLINK = new
Step 11: newDATA = X
Step 12: End if
Step 13: Stop
Inserting a node into a Single Linked List at any position in the list:

Algorithm:
Step 1: new = GetNode (NODE)
Step 2: If (new = NULL) do
Step 3: Print “Memory is insufficient: No insertion is possible”
Step 4: Exit
Step 5: Else
Step 6: ptr = HEADER
Step 7: While (ptrDATA ≠ KEY) and (ptrLINK ≠ NULL) do
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: If (ptrLINK = NULL) then
Step 11: Print “KEY is not available in the list”
Step 12: Exit
Step 13: Else
Step 14: newLINK = ptrLINK
Step 15: newDATA = X
Step 16: ptrLINK = new
Step 17: End if
Step 18: End if
Step 19: Stop
Deleting a node from a Single Linked List:
There are three cases of deletion,
1. Deleting from the front of the list
2. Deleting from the end of the list
3. Deleting from any position in the list
Deleting the node from the front of a Single Linked List:

Algorithm:
Step 1: ptr = HEADERLINK
Step 2: If (ptr = NULL) then
Step 3: Print “The list is empty: No deletion”
Step 4: Exit
Step 5: Else
Step 6: ptr1 = ptrLINK
Step 7: HEADERLINK = ptr1
Step 8: ReturnNode(ptr)
Step 9: End if
Step 10: Stop
Deleting the node at the end of a Single Linked List:

Algorithm:
Step 1: ptr = HEADER
Step 2: If (ptrLINK = NULL) then
Step 3: Print “The list is empty: No deletion”
Step 4: Exit
Step 5: Else
Step 6: While (ptrLINK ≠ NULL) do
Step 7: ptr1 = ptr
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: ptr1LINK = NULL
Step 11: ReturnNode(ptr)
Step 12: End if
Step 13: Stop
Deleting the node at any position of a Single Linked List:

Algorithm:
Step 1: ptr1 = HEADER
Step 2: ptr = ptr1LINK
Step 3: While (ptr ≠ NULL) do
Step 4: If (ptrDATA ≠ KEY) then
Step 5: ptr1 = ptr
Step 6: ptr = ptrLINK
Step 7: Else
Step 8: ptr1LINK = ptrLINK
Step 9: ReturnNode(ptr)
Step 10: Exit
Step 11: End if
Step 12: End while
Step 13: If (ptr = NULL) then
Step 14: Print “Node with KEY does not exist: No deletion”
Step 15: End if
Step 16: Stop
Copying a Single Linked List:
This operation is used to make a duplicate the content of each node.
Algorithm:
Step 1: ptr = HEADER
Step 2: HEADER1 = GetNode(NODE)
Step 3: ptr1 = HEADER1
Step 4: ptr1DATA = NULL
Step 5: While (ptr ≠ NULL) do
Step 6: new = GetNode(NODE)
Step 7: newDATA = ptrDATA
Step 8: ptr1LINK = new
Step 9: newLINK = NULL
Step 10: ptr1 = new
Step 11: ptr = ptrLINK
Step 12: End while
Step 13: Stop
Merging two Single Linked Lists into one list:
Two single linked lists, namely L1 and L2 are available and we want to merge the list L2 after
L1.
Assume, HEADER1 and HEADER2 are the header node of the lists L1 and L2.
Merging can be done by setting the pointer of the link field of the last node in the list L1 with
the pointer of the first node in L2.
Algorithm:
Step 1: ptr = HEADER1
Step 2: While (ptrLINK ≠ NULL) do
Step 3: ptr = ptrLINK
Step 4: End while
Step 5: ptrLINK = HEADER2LINK
Step 6: ReturnNode(HEADER)
Step 7: HEADER = HEADER1
Step 8: Stop
Searching for an element in a Single Linked List:
It is used to search an item in a single linked list.
Algorithm:
Step 1: ptr = HEADERLINK
Step 2: flag = 0, LOCATION = NULL
Step 3: While (ptr ≠ NULL) and (flag = 0) do
Step 4: If (ptrDATA = KEY) then
Step 5: flag = 1
Step 6: LOCATION = ptr
Step 7: Print “Search is successful”
Step 8: Return(LOCATION)
Step 9: Else
Step 10: ptr = ptrLINK
Step 11: End if
Step 12: End while
Step 13: If (ptr = NULL) then
Step 14: Print “Search is unsuccessful”
Step 15: End if
Step 16: Stop
6.3 CIRCULAR LINKED LIST
In a single linked list, the link field of the last node is null, but a number of advantages can be
gained if we use this link fields to store the pointer of the header node.
Definition:
A linked list where the last node points to the header node is called the circular linked list.
Circular link lists have certain advantages over ordinary linked list, they are
Accessibility of a member node in the list
In an ordinary list, a member node is accessible from a particular node, that is, from the
header node only. But in a circular linked list, every member node is accessible from any node.
Null link problem
The null value in the link field may create some problem during the execution of programs,
this is explained by two algorithms to perform search on ordinary linked list and circular linked list.
Algorithm for search single linked list:
Step 1: ptr = HEADERLINK
Step 2: While (ptr ≠ NULL) do
Step 3: If (ptrDATA ≠ KEY) then
Step 4: ptr = ptrLINK
Step 5: Else
Step 6: Print “Search is successful”
Step 7: Return (ptr)
Step 8: End if
Step 9: End while
Step 10: If (ptr = NULL) then
Step 11: Print “The entire list has traversed but KEY is not found”
Step 12: End if
Step 13: Stop
Algorithm for search circular linked list:
Step 1: ptr = HEADERLINK
Step 2: While (ptrDATA ≠ KEY) and (ptr ≠ HEADER) do
Step 3: ptr = ptrLINK
Step 4: End while
Step 5: If (ptrDATA = KEY) then
Step 6: Return (ptr)
Step 7: Else
Step 8: Print “Entire list is searched: KEY node is not found”
Step 9: End if
Step 10: Stop

In the search single linked list algorithm, two tests in step 2 cannot be placed together as
while (ptr ≠ NULL) AND (ptrDATA ≠ KEY) do because in that case there will be an execution error
for ptrDATA is not defined when ptr = NULL but with a circular linked list very simple
implementation is possible without any special care for the NULL pointer.
Easy–to–implementation operations:
Some operations like merging, splitting, deleting of an entire list can easily be implemented
on circular linked list than with an ordinary linked list.
Algorithm for merging two circular linked lists:
Step 1: ptr1 = HEADER1LINK
Step 2: ptr2 = HEADER2LINK
Step 3: HEADER1LINK = ptr2
Step 4: While (ptr2LINK ≠ HEADER2) do
Step 5: ptr2 = ptr2LINK
Step 6: End while
Step 7: ptr2LINK = ptr1
Step 8: ReturnNode (HEADER2)
Step 9: Stop
In the algorithm merge single linked list, the entire list is needed to be traversed in order to
locate the last node, which is not required in the merge circular linked list algorithm, so merge
circular linked list algorithm works faster than merge single linked list.
 The linked list can be traversed either in forward or backward direction.
 All the nodes link address will have valid addresses, instead of NULL pointer.
 A node can be inserted or deleted from either front or rear of a linked list.
 One can start at any node in list and traverse the whole list.
Disadvantage:
One main disadvantage is that without adequate care in processing, it is possible to get
trapped into an infinite loop. This problem occurs when we are unable to detect the end of the list
while moving from one node to the next.

6.4 DOUBLE LINKED LIST


In a doubly linked list, each node is not only pointed to the next node but also pointed to the
previous nodes. A double linked list is a two way list because one can move either direction, ie, either
from left to right or from right to left. This is accomplished by maintaining two link fields.

Each node of doubly linked list is divided into three parts:


 BACK (or) LLINK: A pointer field, which contains the location of previous node in
the list.
 INFO (or) DATA: An info field, which contains data.
 FORW (or) RLINK: A pointer field which contains location of the next node in the
list.
Except the header node and the last node, points to its immediate predecessor and immediate
successor.
6.4.1 Operations on a Double Linked List:
1. Inserting a node into a double linked list:
i. Inserting a node at the front of the list
ii. Inserting a node at the end of the list
iii. Inserting a node at any position of the list
2. Deleting a node from a double linked list
i. Deleting a node from at the front of the list
ii. Deleting a node from at the end of the list
iii. Deleting a node from any position of the list
Inserting a node into a Double Linked List:
Inserting a node at the front of a Double Linked List:

Algorithm:
Step 1: ptr = HEADERRLINK
Step 2: new = GetNode(NODE)
Step 3: If (new ≠ NULL) then
Step 4: newLLINK = HEADER
Step 5: HEADERRLINK = new
Step 6: newRLINK = ptr
Step 7: ptrLLINK = new
Step 8: newDATA = X
Step 9: Else
Step 10: Print “Unable to allocate memory: No insertion”
Step 11: End if
Step 12: Stop
Inserting a node at the end of a Double Linked List:

Algorithm:
Step 1: ptr = HEADER
Step 2: While (ptrRLINK ≠ NULL) do
Step 3: ptr = ptrRLINK
Step 4: End while
Step 5: new = GetNode(NODE)
Step 6: If (new ≠ NULL) then
Step 7: newLLINK = ptr
Step 8: ptrRLINK = new
Step 9: newRLINK = NULL
Step 10: newDATA = X
Step 11: Else
Step 12: Print “Insertion is not possible”
Step 13: End if
Step 14: Stop
Inserting a node at any position of a Double Linked List:

Algorithm:
Step 1: ptr = HEADER
Step 2: While (ptrDATA ≠ KEY) and (ptrRLINK ≠ NULL) do
Step 3: ptr = ptrRLINK
Step 4: End while
Step 5: new = GetNode(NODE)
Step 6: If (new = NULL) then
Step 7: Print “Memory is not available”
Step 8: Exit
Step 9: Else
Step 10: If (ptrRLINK = NULL) then
Step 11: newLLINK = ptr
Step 12: ptrRLINK = new
Step 13: newRLINK = NULL
Step 14: newDATA = x
Step 15: Else
Step 16: ptr1 = ptrRLINK
Step 17: newLLINK = ptr
Step 18: newRLINK = ptr1
Step 19: ptrRLINK = new
Step 20: ptr1LLINK = new
Step 21: ptr = new
Step 22: newDATA = X
Step 23: End if
Step 24: Stop
Deleting a node from a Double Linked List:
Deleting a node from the front of a Double Linked List:

Algorithm:
Step 1: ptr = HEADERRLINK
Step 2: If (ptr = NULL) then
Step 3: Print “List is empty: No deletion is possible”
Step 4: Exit
Step 5: Else
Step 6: ptr1 = ptrRLINK
Step 7: HEADERRLINK = ptr1
Step 8: If (ptr1 ≠ NULL) then
Step 9: ptr1LLINK = HEADER
Step 10: End if
Step 11: ReturnNode (ptr)
Step 12: End if
Step 13: Stop
Deleting a node from the end of a Double Linked List:

Algorithm:
Step 1: ptr = HEADER
Step 2: While (ptrRLINK ≠ NULL) do
Step 3: ptr = ptrRLINK
Step 4: End while
Step 5: If (ptr = HEADER) then
Step 6: Print “List is empty: No deletion”
Step 7: Exit
Step 8: Else
Step 9: ptr1 = ptrLLINK
Step 10: ptr1RLINK = NULL
Step 11: ReturnNode (NODE)
Step 12: End if
Step 13: Stop
Deleting a node from any position of a Double Linked List:

Algorithm:

Step 1: ptr = HEADERRLINK


Step 2: If (ptr = NULL) then
Step 3: Print “List is empty: No deleteion”
Step 4: Exit
Step 5: End if
Step 6: While (ptrDATA ≠ KEY) and (ptrRLINK ≠ NULL) do
Step 7: ptr = ptrRLINK
Step 8: End while
Step 9: If (ptrDATA = KEY) then
Step 10: ptr1 = ptrLLINK
Step 11: ptr2 = ptrRLINK
Step 12: ptr1RLINK = ptr2
Step 13: If (ptr2 ≠ NULL) then
Step 14: ptr2LLINK = ptr1
Step 15: End if
Step 16: ReturnNode (ptr)
Step 17: Else
Step 18: Print “The node does not exist in the given list”
Step 19: End if
Step 20: Stop
6.5 CIRCULAR DOUBLE LINKED LIST
The advantages of both double linked list and circular linked list are implemented into
another type of list structure called circular double linked list.
The RLINK of the rightmost node and LLINK of the leftmost contain the address of the
header node; again the RLINK and LLINK of the header node contain the address of the rightmost
node and the leftmost node respectively.

6.5.1 Operations on Circular Double Linked List:


Traversing:
Algorithm:
Step 1: ptr = HEADERRLINK
Step 2: While (ptr ≠ HEADER) do
Step 3: Process (ptr)
Step 4: ptr = ptrRLINK
Step 5: End while
Step 6: Stop
Sorting:

Algorithm:
Step 1: ptrBeg = HEADERLLINK
Step 2: PtrEnd = HEADERRLINK
Step 3: While (ptrBeg ≠ ptrEnd) do
Step 4: ptr1 = ptrBeg
Step 5: ptr2 = ptr1RLINK
Step 6: While (ptr2 ≠ ptrEnd) do
Step 7: If order (ptr1DATA, ptr2DATA) = FALSE then
Step 8: Swap (ptr1, ptr2)
Step 9: End if
Step 10: ptr1 = ptr1RLINK
Step 11: ptr2 = ptr2RLINK
Step 12: End while
Step 13: ptrEnd = ptrEndLLINK
Step 14: End while
Step 15: Stop

6.6 APPLICATIONS OF LINKED LIST:


 Sparse Matrix Manipulation
 Polynomial Representation
 Dynamic Storage Management
6.6.1 SPARSE MATRIX MANIPULATION:
A sparse matrix is a two-dimensional array, where the majority of the elements have the
value NULL. Structure of a node to represent sparse matrices,
The fields i and j store the row and column numbers for a matrix element.
DATA field stores the matrix element at the ith row and jth column.
The ROWLINK points the next node in the same row and COLLINK points the next node in
the same column.
To illustrate the sparse matrix of order 6 x 5 is assumed.

* denotes the NULL value


Linked representation of Sparse Matrix,

Here CH1, CH2, CH3, CH4, CH5 are the 5 headers, heading 5 columns. RH1, RH2, RH3, RH4,
RH5 and RH6 are the 6 header heading 6 rows.
HEADER is one additional header node keep the starting address of the sparse matrix.
Algorithm for create Sparse Matrix_LL:
Step 1: Read m, n
Step 2: HEADER = GetNode (NODE)
Step 3: If (HEADER = NULL) then
Step 4: Print “Non availability of storage space: Quit”
Step 5: Exit
Step 6: Else
Step 7: HEADERi = m
Step 8: HEADERj = n
Step 9: HEADERROWLINK = HEADER
Step 10: HEADERCOLLINK = HEADER
Step 11: HEADERDATA = NULL
Step 12: ptr = HEADER
Step 13: For col = 1 to n do
Step 14: new = GetNode (NODE)
Step 15: newi = 0, newj = col, newDATA = NULL
Step 16: ptrROWLINK = new
Step 17: newROWLINK = HEADER, newCOLLINK = new
Step 18: ptr = new
Step 19: End for
Step 20: ptr = HEADER
Step 21: For row = 1 to m do
Step 22: new = GetNode (NODE)
Step 23: newi = row, newj = 0, newDATA = NULL
Step 24: ptrCOLLINK = new
Step 25: newCOLLINK = HEADER, newROWLINK = new
Step 26: ptr = new
Step 27: End for
Step 28: Read (data, row, col)
Step 29: rowHEADER = HEADERCOLLINK,
colHEADER = HEADERROWLINK
Step 30: While (row < rowHEADERi)
Step 31: rowHEADER = HEADERCOLLINK
Step 32: End while
Step 33: While (col < colHEADERj)
Step 34: colHEADER = HEADERROWLINK
Step 35: End while
Step 36: rowptr = rowHEADER
Step 37: While (rowptrj < col) do
Step 38: ptr1 = rowptr
Step 39: rowptr = rowptrROWLINK
Step 40: If (rowptr = rowHEADER) then
Step 41: Break
Step 42: End if
Step 43: End while
Step 44: colptr = colHEADER
Step 45: While (colptri < row)
Step 46: ptr2 = colptr
Step 47: colptr = colptrCOLLINK
Step 48: If (colptr = colHEADER) then
Step 49: Break
Step 50: End if
Step 51: End while
Step 52: new = GetNode (NODE)
Step 53: If (new = NULL) then
Step 54: Print “Non availability of storage space”
Step 55: Exit
Step 56: Else
Step 57: ptr1ROWLINK = new
Step 58: ptr2COLLINK = new
Step 59: newROWLINK = rowptr
Step 60: newCOLLINK = colptr
Step 61: newDATA = KEY
Step 62: End if
Step 63: If more insert then
Step 64: Go to Step 28
Step 65: End if
Step 66: End if
Step 67: Stop
6.6.2 POLYNOMIAL REPRESENTATION:
An important application of linked lists is to represent polynomials and their manipulations.
The main advantage of a linked list for polynomial representation is that it can accommodate
a number of polynomials of growing sizes so that their combined size does not exceed the total
memory available.
The general form of a polynomial having a single variable.
P(x) = anxen + an-1xen-1 + ……… + a1xe1
Where, aixei is a term in the polynomial so that ai is a non-zero coefficient and ei is the
exponent.
The structure of a node in order to represent a term.

COEFF EXP LINK

In the single linked list representation, a node should have three fields: COEFF, EXP and a
LINK.
Single linked list representation of the polynomial P(x) = 3x8 – 7x6 + 14x3 +10x – 5 would be
stored as,

POLYNOMIAL ADDITION:
In order to add two polynomials say P and Q to get a resultant polynomial R.
There may arise three cases during the comparison between the terms of two polynomials.
Case 1: The exponents of two terms are equal. In this case the coefficients in the two
nodes are added and a new term is created.
RptrCoeff = PptrCoeff + QptrCoeff and
RptrExp = PptrExp

Case 2: PptrExp > QptrExp ie, the exponent of the current in P is greater than the
exponent of the current term in Q. In this case, a duplicate of the current term in P
is created and inserted in the polynomial R.
Case 3: PptrExp < QptrExp ie, the exponent of the current in P is less than the
exponent of the current term in Q. In this case, a duplicate of the current term in
Q is created and inserted in the polynomial R.
Algorithm for adding two polynomials:
Step 1: Pptr = PHEADERLINK, Qptr = QHEADERLINK
Step 2: RHEADER = GetNode(NODE)
Step 3: RHEADERLINK = NULL, RHEADEREXP = NULL,
RHEADERCOEFF = NULL
Step 4: Rptr = RHEADER
Step 5: While (Pptr ≠ NULL) and (Qptr ≠ NULL) do
Step 6: CASE: PptrEXP = QptrEXP
Step 7: new = GetNode (NODE)
Step 8: RptrLINK = new, Rptr = new
Step 9: RptrCOEFF = Pptr COEFF + Qptr COEFF
Step 10: RptrEXP = PptrEXP
Step 11: RptrLINK = NULL
Step 12: Pptr = PptrLINK, Qptr = QptrLINK
Step 13: CASE: PptrEXP > QptrEXP
Step 14: new = GetNode (NODE)
Step 15: RptrLINK = new, Rptr = new
Step 16: RptrCOEFF = Pptr COEFF
Step 17: RptrEXP = PptrEXP
Step 18: RptrLINK = NULL
Step 19: Pptr = PptrLINK
Step 20: CASE: PptrEXP < QptrEXP
Step 21: new = GetNode (NODE)
Step 22: RptrLINK = new, Rptr = new
Step 23: RptrCOEFF = Qptr COEFF
Step 24: RptrEXP = QptrEXP
Step 25: RptrLINK = NULL
Step 26: Qptr = QptrLINK
Step 27: Endwhile
Step 28: If (Pptr ≠ NULL) and (Qptr = NULL) then
Step 29: While (Pptr ≠ NULL) do
Step 30: new = GetNode (NODE)
Step 31: RptrLINK = new, Rptr = new
Step 32: RptrCOEFF = Pptr COEFF
Step 33: RptrEXP = PptrEXP
Step 34: RptrLINK = NULL
Step 35: Pptr = PptrLINK
Step 36: Endwhile
Step 37: Endif
Step 38: If (Pptr = NULL) and (Qptr ≠ NULL) then
Step 39: While (Qptr ≠ NULL) do
Step 40: new = GetNode (NODE)
Step 41: RptrLINK = new, Rptr = new
Step 42: RptrCOEFF = Qptr COEFF
Step 43: RptrEXP = QptrEXP
Step 44: RptrLINK = NULL
Step 45: Qptr = QptrLINK
Step 46: Endwhile
Step 47: Endif
Step 48: Return (RHEADER)
Step 49: Stop
Example:
P = 3x3 + 2x2 + 2x1
Q = 4x3 + 4x2 + 1x1
Result R = 7x3 + 6x2 + 3x1
POLYNOMIAL MULTIPLICATION:
This method is quite straight forward.
Let Pptr denote the current term in P and Qptr be that in Q.
For each term of P visit all the terms in Q. The exponent values in two terms are added
(REXP = PEXP + QEXP), the coefficient values are multiplied (RCOEFF = PCOEFF x
QCOEFF) and these values are included into R.
Algorithm:
Step 1: Pptr = PHEADER, Qptr = QHEADER
Step 2: RHEADER = GetNode (NODE)
Step 3: RHEADERLINK = NULL, RHEADERCOEFF = NULL,
RHEADEREXP = NULL
Step 4: If (PptrLINK = NULL) or (QptrLINK = NULL) then
Step 5: Exit
Step 6: Endif
Step 7: PptrLINK = NULL
Step 8: While (Pptr ≠ NULL) do
Step 9: While (Qptr ≠ NULL) do
Step 10: C = PptrCOEFF x QptrCOEFF
Step 11: X = PptrEXP + QptrEXP
Step 12: Rptr = RHEADER
Step 13: While (Rptr ≠ NULL) and (RptrEXP > X) do
Step 14: Rptr1 = Rptr
Step 15: Rptr = RptrLINK
Step 16: Endwhile
Step 17: If (RptrEXP = X) then
Step 18: RptrCOEFF = RptrCOEFF + C
Step 19: Else
Step 20: new = GetNode (NODE)
Stpe 21: newEXP = X, newCOEFF = C
Step 22: If (RptrLINK = NULL) then
Step 23: RptrLINK = new
Step 24: newLINK = NULL
Step 25: Else
Step 26: Rptr1LINK = new
Step 27: newLINK = Rptr
Step 28: Endif
Step 29: Endif
Step 30: Endwhile
Step 31: Endwhile
Step 32: Return (RHEADER)
Step 33: Stop
Example:
P = 3x3 + 2x2 + 1x1
Q = 2x2 + 2x1
R = 6x5 + 10x4 + 6x3 + 2x2
6.6.3 DYNAMIC STORAGE MANAGEMENT:
The basic task of any program is to manipulate data. These data should be stored in memory
during their manipulation.
There are two memory management schemes for the storage allocations of data.
 Static Storage Management
 Dynamic Storage Management
In static storage management scheme, the net amount of memory required for various data
for a program is allocated before the start of the execution of the program.
Once memory is allocated, it can neither be extended nor be returned to the memory bank.
The dynamic storage management scheme allows the user to allocate and deallocate the
memory during the execution of the program.
The dynamic storage management scheme is suitable in multiprogramming as well as single
user environment.
Allocation schemes:
There are two strategies for allocation,
 Fixed block allocation
 Variable block allocation
There are four strategies under variable block allocation,
i. First fit
ii. Next fit
iii. Best fit
iv. Worst fit
Deallocation schemes:
i. Random deallocation

ii. Ordered deallocation


QUESTIONS
CHAPTER – 5
1) Explain the representation of queue in detail.
2) Explain the operations on queue in detail.
3) Describe various structures of queue.
4) Explain circular queue.
5) Explain deque.
6) Explain priority queue.
CHAPTER – 6
1) Explain in detail about single linked list.
2) Explain the operations on single linked list in detail.
3) Explain circular linked list.
4) Explain the operations on double linked list.
5) Write a note on circular double linked list.
6) Explain sparse matrix manipulation with example.
7) Explain polynomial representation with example.
8) Explain dynamic storage management.
9) Write a note on memory representation.
10) Explain boundary tag system.
11) Explain storage allocation strategies.
12) Explain deallocation strategies.
13) Explain buddy system.

You might also like