Professional Documents
Culture Documents
Unit Iii
Unit Iii
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
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
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
7. If (next ≠ FRONT)then
8. REAR=next
9. CQ[REAR]=ITEM
10. Else
12. Endif
13. Endif
14. Stop
ALGORITHM FOR DEQUEUE-CQ
STEPS:
1. If (FRONT=0)then
3. Exit
4. Else
5. ITEM=CQ[FRONT]
6. If(FRONT=REAR)then
7. FRONT=0
8. REAR=0
9. Else
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:
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
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
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
7. If(next≠FRONT)then
8. REAR=next
9. DQ[REAR]=ITEM
10. Else
12. Endif
13. Endif
14. Stop
ALGORITHM EJECT-DQ
STEPS:
1. If (FRONT=0)then
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.
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
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)
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.
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.
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: newLINK = HEADERLINK
Step 7: newDATA = X
Step 8: HEADERLINK = 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 (ptrLINK ≠ NULL) do
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: ptrLINK = new
Step 11: newDATA = 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 (ptrDATA ≠ KEY) and (ptrLINK ≠ NULL) do
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: If (ptrLINK = NULL) then
Step 11: Print “KEY is not available in the list”
Step 12: Exit
Step 13: Else
Step 14: newLINK = ptrLINK
Step 15: newDATA = X
Step 16: ptrLINK = 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 = HEADERLINK
Step 2: If (ptr = NULL) then
Step 3: Print “The list is empty: No deletion”
Step 4: Exit
Step 5: Else
Step 6: ptr1 = ptrLINK
Step 7: HEADERLINK = ptr1
Step 8: ReturnNode(ptr)
Step 9: End if
Step 10: Stop
Algorithm:
Step 1: ptr = HEADER
Step 2: If (ptrLINK = NULL) then
Step 3: Print “The list is empty: No deletion”
Step 4: Exit
Step 5: Else
Step 6: While (ptrLINK ≠ NULL) do
Step 7: ptr1 = ptr
Step 8: ptr = ptrLINK
Step 9: End while
Step 10: ptr1LINK = 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 = ptr1LINK
Step 3: While (ptr ≠ NULL) do
Step 4: If (ptrDATA ≠ KEY) then
Step 5: ptr1 = ptr
Step 6: ptr = ptrLINK
Step 7: Else
Step 8: ptr1LINK = ptrLINK
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: ptr1DATA = NULL
Step 5: While (ptr ≠ NULL) do
Step 6: new = GetNode(NODE)
Step 7: newDATA = ptrDATA
Step 8: ptr1LINK = new
Step 9: newLINK = NULL
Step 10: ptr1 = new
Step 11: ptr = ptrLINK
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 (ptrLINK ≠ NULL) do
Step 3: ptr = ptrLINK
Step 4: End while
Step 5: ptrLINK = HEADER2LINK
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 = HEADERLINK
Step 2: flag = 0, LOCATION = NULL
Step 3: While (ptr ≠ NULL) and (flag = 0) do
Step 4: If (ptrDATA = 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 = ptrLINK
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 = HEADERLINK
Step 2: While (ptr ≠ NULL) do
Step 3: If (ptrDATA ≠ KEY) then
Step 4: ptr = ptrLINK
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 = HEADERLINK
Step 2: While (ptrDATA ≠ KEY) and (ptr ≠ HEADER) do
Step 3: ptr = ptrLINK
Step 4: End while
Step 5: If (ptrDATA = 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 (ptrDATA ≠ KEY) do because in that case there will be an execution error
for ptrDATA 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 = HEADER1LINK
Step 2: ptr2 = HEADER2LINK
Step 3: HEADER1LINK = ptr2
Step 4: While (ptr2LINK ≠ HEADER2) do
Step 5: ptr2 = ptr2LINK
Step 6: End while
Step 7: ptr2LINK = 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.
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 = HEADERRLINK
Step 2: new = GetNode(NODE)
Step 3: If (new ≠ NULL) then
Step 4: newLLINK = HEADER
Step 5: HEADERRLINK = new
Step 6: newRLINK = ptr
Step 7: ptrLLINK = new
Step 8: newDATA = X
Step 9: Else
Step 10: Print “Unable to allocate memory: No insertion”
Step 11: End if
Step 12: Stop
Algorithm:
Step 1: ptr = HEADER
Step 2: While (ptrRLINK ≠ NULL) do
Step 3: ptr = ptrRLINK
Step 4: End while
Step 5: new = GetNode(NODE)
Step 6: If (new ≠ NULL) then
Step 7: newLLINK = ptr
Step 8: ptrRLINK = new
Step 9: newRLINK = NULL
Step 10: newDATA = 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 (ptrDATA ≠ KEY) and (ptrRLINK ≠ NULL) do
Step 3: ptr = ptrRLINK
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 (ptrRLINK = NULL) then
Step 11: newLLINK = ptr
Step 12: ptrRLINK = new
Step 13: newRLINK = NULL
Step 14: newDATA = x
Step 15: Else
Step 16: ptr1 = ptrRLINK
Step 17: newLLINK = ptr
Step 18: newRLINK = ptr1
Step 19: ptrRLINK = new
Step 20: ptr1LLINK = new
Step 21: ptr = new
Step 22: newDATA = 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 = HEADERRLINK
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 = ptrRLINK
Step 7: HEADERRLINK = ptr1
Step 8: If (ptr1 ≠ NULL) then
Step 9: ptr1LLINK = 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 (ptrRLINK ≠ NULL) do
Step 3: ptr = ptrRLINK
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 = ptrLLINK
Step 10: ptr1RLINK = NULL
Step 11: ReturnNode (NODE)
Step 12: End if
Step 13: Stop
Deleting a node from any position of a Double Linked List:
Algorithm:
Sorting:
Algorithm:
Step 1: ptrBeg = HEADERLLINK
Step 2: PtrEnd = HEADERRLINK
Step 3: While (ptrBeg ≠ ptrEnd) do
Step 4: ptr1 = ptrBeg
Step 5: ptr2 = ptr1RLINK
Step 6: While (ptr2 ≠ ptrEnd) do
Step 7: If order (ptr1DATA, ptr2DATA) = FALSE then
Step 8: Swap (ptr1, ptr2)
Step 9: End if
Step 10: ptr1 = ptr1RLINK
Step 11: ptr2 = ptr2RLINK
Step 12: End while
Step 13: ptrEnd = ptrEndLLINK
Step 14: End while
Step 15: Stop
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) = 3x 8 – 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.
RptrCoeff = PptrCoeff + QptrCoeff and
RptrExp = PptrExp
Case 2: PptrExp > QptrExp 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: PptrExp < QptrExp 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 = PHEADERLINK, Qptr = QHEADERLINK
Step 2: RHEADER = GetNode(NODE)
Step 3: RHEADERLINK = NULL, RHEADEREXP = NULL,
RHEADERCOEFF = NULL
Step 4: Rptr = RHEADER
Step 5: While (Pptr ≠ NULL) and (Qptr ≠ NULL) do
Step 6: CASE: PptrEXP = QptrEXP
Step 7: new = GetNode (NODE)
Step 8: RptrLINK = new, Rptr = new
Step 9: RptrCOEFF = Pptr COEFF + Qptr COEFF
Step 10: RptrEXP = PptrEXP
Step 11: RptrLINK = NULL
Step 12: Pptr = PptrLINK, Qptr = QptrLINK
Step 13: CASE: PptrEXP > QptrEXP
Step 14: new = GetNode (NODE)
Step 15: RptrLINK = new, Rptr = new
Step 16: RptrCOEFF = Pptr COEFF
Step 17: RptrEXP = PptrEXP
Step 18: RptrLINK = NULL
Step 19: Pptr = PptrLINK
Step 20: CASE: PptrEXP < QptrEXP
Step 21: new = GetNode (NODE)
Step 22: RptrLINK = new, Rptr = new
Step 23: RptrCOEFF = Qptr COEFF
Step 24: RptrEXP = QptrEXP
Step 25: RptrLINK = NULL
Step 26: Qptr = QptrLINK
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: RptrLINK = new, Rptr = new
Step 32: RptrCOEFF = Pptr COEFF
Step 33: RptrEXP = PptrEXP
Step 34: RptrLINK = NULL
Step 35: Pptr = PptrLINK
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: RptrLINK = new, Rptr = new
Step 42: RptrCOEFF = Qptr COEFF
Step 43: RptrEXP = QptrEXP
Step 44: RptrLINK = NULL
Step 45: Qptr = QptrLINK
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
(REXP = PEXP + QEXP), the coefficient values are multiplied (RCOEFF = PCOEFF x
QCOEFF) and these values are included into R.
Algorithm:
Step 1: Pptr = PHEADER, Qptr = QHEADER
Step 2: RHEADER = GetNode (NODE)
Step 3: RHEADERLINK = NULL, RHEADERCOEFF = NULL,
RHEADEREXP = NULL
Step 4: If (PptrLINK = NULL) or (QptrLINK = NULL) then
Step 5: Exit
Step 6: Endif
Step 7: PptrLINK = NULL
Step 8: While (Pptr ≠ NULL) do
Step 9: While (Qptr ≠ NULL) do
Step 10: C = PptrCOEFF x QptrCOEFF
Step 11: X = PptrEXP + QptrEXP
Step 12: Rptr = RHEADER
Step 13: While (Rptr ≠ NULL) and (RptrEXP > X) do
Step 14: Rptr1 = Rptr
Step 15: Rptr = RptrLINK
Step 16: Endwhile
Step 17: If (RptrEXP = X) then
Step 18: RptrCOEFF = RptrCOEFF + C
Step 19: Else
Step 20: new = GetNode (NODE)
Stpe 21: newEXP = X, newCOEFF = C
Step 22: If (RptrLINK = NULL) then
Step 23: RptrLINK = new
Step 24: newLINK = NULL
Step 25: Else
Step 26: Rptr1LINK = new
Step 27: newLINK = 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