2. Queue :
A queue is a linear list of elements in which deletions can take
place only at one end, called FRONT, and insertion can take only
at the other end, called the REAR.
Queues are also called First-in-First-out (FIFO).
Static Representation of Queue:
AAA BBB CCC
FRONT REAR
BBB CCC
FRONT REAR
After deleting
“AAA”
FRONT
incremented
After inserting
“DDD”
REAR
incremented
& overflow
BBB CCC
FRONT REAR
DDD
Inserting “EEE”
REAR is not
Incremented
Because Queue
Is full
3. QINSERT(Q, N, FRONT, REAR,ITEM)
This procedure inserts an element ITEM into a queue.
1. [Queue already filled?]
If REAR >= N, then
Write OVERFLOW, and Return.
2. [Find new value of REAR]
If FRONT : = NULL, then [Queue initially empty.]
Set FRONT := 1 and REAR := 1
Else
Set REAR := REAR +1
[End of If structure]
3. [This inserts new element.]
Set Q[REAR] := ITEM
4. Return
4. QDELETE (Q, N, FRONT,REAR, ITEM)
This procedure deletes an element from a queue and assigns
it to the variable ITEM.
1. [Queue already empty?]
If FRONT := NULL, then : Write UNDERFLOW, and Return
2. Set ITEM := Q[FRONT]
3. [Find new value of Front.]
If FRONT=REAR, then :
Set FRONT := NULL and REAR := NULL.
Else
Set FRONT := FRONT +1.
[End of If structure.]
4. Return
5. Dynamic Representation of Queue:
Linked Representation
The linked presentation of queue, i.e. queue is implemented
using a SLL.
The nodes are divided into two parts:
1. INFO hold the element of the queue.
2. LINK hold pointers to the next element in the queue.
AAA BBB CCC
FRONT REAR
6. ILINKQ(INFO,LINK,FRONT,REAR,AVAIL,ITEM)
This procedure inserts an ITEM into a linked queue.
1. [Available space?]
If AVAIL=NULL, then Write OVERFLOW and Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. [Copies ITEM into new node]
Set INFO[NEW] := ITEM and LINK[NEW]=NULL
4. If (FRONT = NULL) then FRONT= REAR=NEW
Else set LINK[REAR] := NEW and REAR = NEW
5. Exit
7. DLINKQ(INFO, LINK, FRONT,REAR, AVAIL, ITEM)
This procedure deletes the front element of a linked list and
assigns
it to the variable ITEM.
1. [Queue has an item to be removed?]
If FRONT = NLL then Write : UNDERFLOW & Exit
2. Set TEMP = FRONT
3. Set ITEM := INFO[TEMP]
4. [Reset FRONT to point to the next element in the queue]
Set FRONT := LINK[TEMP]
5. [Return deleted node to the AVAIL list]
Set LINK[TEMP] := AVAIL and AVAIL := TEMP
6. Exit
8. The queue operations
1.Queue Create an empty queue
2.~Queue Destroy an existing queue
3.isEmpty Determine whether queue is empty
4.isFull Determine whether the queue is full
5.enqueue Add an item to the end of the queue
6.dequeue Remove the item from front of queue
7. peek Retrieve the item at front of queue
9. Circular Queue :
In circular queue is one in which the first element comes after the
last element.
Circular queues are the queues implemented in circular form
rather than in a straight line.
Circular queues overcome the problem of unutilized space in
linear queue implemented as an array.
10. CQINSERT(Q, N, FRONT, REAR,ITEM)
This procedure inserts an element ITEM into a circular queue.
1. [Queue already filled?]
If FRONT = 1 and REAR = N, or if FRONT = REAR + 1, then
Write OVERFLOW, and Return.
2. [Find new value of REAR]
If FRONT : = NULL, then [Queue initially empty.]
Set FRONT := 1 and REAR := 1
Else if REAR =N then:
Set REAR := 1
Else
Set REAR := REAR +1
[End of If structure]
3. [This inserts new element.]
Set Q[REAR] := ITEM
4. Return
11. CQDELETE (Q, N, FRONT,REAR, ITEM)
This procedure deletes an element from a queue and assigns
it to the variable ITEM.
1. [Queue already empty?]
If FRONT := NULL, then : Write UNDERFLOW, and Return
2. Set ITEM := Q[FRONT]
3. [Find new value of Front.]
If FRONT=REAR, then :
Set FRONT := NULL and REAR := NULL.
Else if FRONT=N then:
Set FRONT := 1
Else
Set FRONT := FRONT +1.
[End of If structure.]
4. Return
12. Delete E
F R
F
Delete F
Delete C & DE
Empty
F R
F R
Insert AA
Insert BA B
F R
Insert CA B
F R
C
Insert DA B
F R
C D
Delete AB
F R
C D
Insert EE B
R F
C D
Delete BE
R F
C D
F R
Insert FE
FR
C DF
F
F R
13. 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 the same priority are processed
according to the order in which they were added to the
queue.
14. There are two ways of maintaining a priority queue in memory,
1. One-way list
2. Multiple queues
In One-way list, each node will contain three items of
information.
INFO PRN LINK
Priority No.
In multiple queue, one queue grows from position of 1 of the
array and the other grows from the last position.
15. Applications of Queues :
1. Round Robin Technique for processor scheduling is
implemented using queues.
2. All types of customer service (like railway ticket reservation)
center software are designed using queues to store customer’s
information.
3. Printer server routines are designed using queues. A number
of
users share a printer using printer server, then server spool’s
all the jobs from all the users to the server’s hard disk in a
queue. From here job number in the queue.
16. Linked Lists :
A linked list is a linear collection of data elements called nodes,
where the linear order is given by means of pointers.
That is, each node is divided into two parts :
the first part contains the information of the elements, and
the second part, called the link field or nextpointer field,
contains the address of the next node in the list.
START
17. Advantages :
1. Linked list are dynamic data structure. i.e. they can grow or
shrink during execution.
2. Efficient memory utilization. Here memory is not pre-
allocated. Memory is allocated whenever is required.
3. Insertion and deletions are easier and efficient.
Disadvantages :
1. More memory required
2. Access to an arbitrary data item is little bit time consuming.
18. Types of linked list :
1. Singly link list
2. Doubly linked list
3. Singly Circular linked list
4. Doubly Circular linked list
19. Singly Linked List :
Inserting a node at the beginning
INSFIRST(INFO,LINK,START,AVAIL,ITEM)
This algorithm inserts ITEM as the first node in the list.
1. [OVERFLOW]
If AVAIL=NULL then: Write : OVERFLOW, and Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := Link[AVAIL]
3. [Copies new data into new node.]
Set INFO[NEW] := ITEM
4. [New node now points to original first node.]
Set LINK[NEW] := START
5. [Changes START so it points to the new node]
Set START := NEW
6. Exit
20. Inserting after a given node :
INLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
This algorithm inserts ITEM so that ITEM follows th node with
location LOC or inserts ITEM as the first node when LOC = NULL.
1. [OVERFLOW]
IF AVAIL=NULL, then Write: OVERFLOW and Exit.
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. [Copies data into new node.]
Set INFO[NEW] := ITEM
4. If LOC=NULL then
Set LINK[NEW] := START and START := NEW
Else
Set LINK[NEW] := LINK[LOC] and LINK[LOC] := New
[End of If structure.]
5. Exit
21. Deletion from a linked list :
Deleting the node following a given node :
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
1. If LOCP = NULL then
Set START := LINK[START]
Else
Set LINK[LOCP] := LINK[LOC]
2. [Return deleted node to the AVAIL list].
Set LINK[LOC] := AVAIL and AVAIL := LOC
3. Exit
22. Deleting the node with a given ITEM of
Information
FINDB(INFO,LINK,START,ITEM,LOC,LOCP)
1. [List empty] If START = NULL then
Set LOC := NULL and LOCP := NULL, and Return
2. [ITEM in first node?]
If INFO[START] = ITEM then :
Set LOC := START and LOCP := NULL, and Return
3. [Initializes pointers.]
Set SAVE := START and PTR := LINK[START]
4. Repeat steps 5 and 6 while PTR != NULL
5. If INFO[PTR]=ITEM then:
Set LOC := PTR and LOCP := SAVE and
Return
6. Set SAVE := PTR and PTR := LINK[PTR]
23. 7. [Search unsuccessful]
Set LOC := NULL
8. Return
DELETE(INFO,LINK,START,AVAIL,ITEM)
1. Call FINDB(INFO,LINK,START,ITEM,LOC,LOCP)
2. If LOC = NULL then : write ITEM not in list, and Exit
3. [Delete node]
If LOCP=NULL then
Set START := LINK[START].
Else
Set LINK[LOCP]:=LINK[LOC]
4. [Return deleted node to the AVAIL list]
Set LINK[LOC] := AVAIL and AVAIL := LOC
5. Exit
24. Singly Circular Linked List :
It is a linked list in which the link field of the last node contains
the address of the first node of the list.
START
A circular linked list has no end. Therefore, it is necessary to
established the START & END nodes the linked list.
END
25. Inserting a node at the beginning
INSFIRST(INFO,LINK,START,AVAIL,ITEM,END)
This algorithm inserts ITEM as the first node in the list.
1. [OVERFLOW]
If AVAIL=NULL then: Write : OVERFLOW, and Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. [List is empty?]
If START = NULL then,
Set INFO[NEW] := ITEM
Set LINK[NEW] := NEW
Set START := END := NEW
[End of IF]
26. 4. [Copies new data into new node.]
Set INFO[NEW] := ITEM
5. [New node now points to original first node.]
Set LINK[NEW] := START
6. [Changes START so it points to the new node]
Set START := NEW
7. [Attach link of END node to START]
Set LINK[END] := NEW
8. Exit
28. Inserting a node at the END
INSEND(INFO,LINK,START,AVAIL,ITEM,END)
1. [OVERFLOW]
If AVAIL=NULL then: Write : OVERFLOW, and
Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. [List is empty?]
If START = NULL then,
Set INFO[NEW] := ITEM
Set LINK[NEW] := NEW
Set START := END := NEW
[End of IF]
29. 4. [Copies new data into new node.]
Set INFO[NEW] := ITEM
5. [New node now points to last node.]
Set LINK[END] := NEW
6. [Changes END so it points to the new node]
Set END := NEW
7. [Attach link of END node to START]
Set LINK[END] := START
8. Exit
31. Inserting a node at the Specified Position
INSEND(INFO,LINK,START,AVAIL,ITEM,TEMP,LOC)
1. [OVERFLOW]
If AVAIL=NULL then: Write : OVERFLOW, and
Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. [Copies new data into new node.]
Set INFO[NEW] := ITEM
32. 4. [Initialize the counter & pointer TEMP]
Set Cnt := 1
Set TEMP := START
5. Repeat Steps 6 until Cnt < LOC -1
6. Set TEMP := LINK[TEMP
Set Cnt := Cnt + 1
6. [Insert the node]
Set LINK[NEW] := LINK[TEMP]
Set LINK[TEMP} := NEW
6. Return
34. Deletion from circular linked list
From the Beginning :
DEL_FIRST(INFO,LINK,START,END,AVAIL,ITEM)
1. [Underflow?]
If START = NULL then, Write UNDERFLOW & Exit
2. [Assign START to ITEM]
Set ITEM := START
3. Set START := LINK[START]
35. 4. [Attach link of END to START]
Set LINK[END] := START
4. [Returns deleted node to AVAIL list]
Set LINK[ITEM] := AVAIL
Set AVAIL := ITEM
4. Exit
37. Deletion from circular linked list
From the END:
DEL_LAST(INFO,LINK,START,END,AVAIL,ITEM)
1. [Underflow?]
If START = NULL then, Write UNDERFLOW & Exit
2. [Points START to ITEM]
Set ITEM := START
3. [Traverse the list up to end]
Repeat step 5 until LINK[ITEM] != START
4. Set LOC := ITEM
Set ITEM:= LINK[ITEM]
38. 5. [Delete last node by deleting link]
Set LINK[LOC] := START
6. [Returns deleted node to AVAIL list]
Set LINK[ITEM] := AVAIL
Set AVAIL := ITEM
7. Exit
39. From specified position :
DEL_LOC(INFO,LINK,START,END,AVAIL,ITEM)
1. [Underflow ?]
If START = NULL then Write : UNDERFLOW & Exit
2. [Initialize the counter and TEMP]
Set Cnt := 1
Set TEMP := START
3. Repeat steps 4 to 6 until Cnt < Loc
4. Set PTR := TEMP
5. Set TEMP := LINK[TEMP]
6. Set Cnt := Cnt +1
40. 7. [Delete an element]
ITEM := INFO[TEMP]
8. [Adjust the link]
Set LINK[PTR] := LINK[TEMP]
9. [Return deleted node to AVAIL list]
Set LINK[ITEM] := AVAIL
Set AVAIL := ITEM
10. Exit
41. Doubly Linked List :
It is a doubly linked list in which all nodes have two link fields.
Structure of node is shown in following fig.
DATA
LEFT RIGHT
The LEFT link points to the predecessor node and the RIGHT
link points to the Successor node.
Insertion algorithms are on next slide:
42. At the Beginning :
INS_START(INFO,LEFT,RIGHT,AVAIL,ITEM,START)
1. [Overflow?]
If AVAIL = NULL, then Write : OVERFLOW & Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL
Set AVAIL := RIGHT[AVAIL]
3. [Copies new data into new node]
Set INFO[NEW] := ITEM
Set LEFT[NEW] := NULL
Set RIGHT[NEW] := NULL
43. 4. [New node now points to the original start node]
Set RIGHT[NEW] := START
Set LEFT[START] := NEW
5. [Change START to new node]
Set START := NEW
6. Exit
44. At the End :
INS_END(INFO,LEFT,RIGHT,AVAIL,ITEM,START)
1. [Overflow?]
If AVAIL = NULL, then Write : OVERFLOW & Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL
Set AVAIL := RIGHT[AVAIL]
3. [Copies new data into new node]
Set INFO[NEW] := ITEM
Set LEFT[NEW] := NULL
Set RIGHT[NEW] := NULL
4. Set LOC := START
45. 5. [Traverse complete linked list]
Repeat step 6 until RIGHT[LOC] != NULL
6. Set LOC := RIGHT[LOC]
7. [Insert node at the end]
Set RIGHT[LOC] := New
Set LEFT[NEW] := LOC
8. Exit
46. At specified position :
INS_LOC(INFO,LEFT,RIGHT,AVAIL,ITEM,START)
1. [OVERFLOW]
If AVAIL=NULL then: Write : OVERFLOW, and
Exit
2. [Remove first node from AVAIL list]
Set NEW := AVAIL and AVAIL := RIGHT[AVAIL]
3. [Copies new data into new node.]
Set INFO[NEW] := ITEM
Set LEFT[NEW] := NULL
Set RIGHT[NEW] := NULL
47. 4. [Initialize the counter & pointer TEMP]
Set Cnt := 1
Set TEMP := START
5. Repeat Steps 6 until Cnt < LOC -1
6. Set TEMP := RIGHT[TEMP]
Set Cnt := Cnt + 1
6. [Insert the node]
Set RIGHT[NEW] := RIGHT[TEMP]
Set RIGHT[TEMP] := NEW
Set LEFT[NEW] := TEMP
6. Return
48. Deletion from Doubly linked list :
From the Beginning :
DELETE_FIRST(INFO,RIGHT,LEFT,AVAIL,START,ITEM)
1. [Underflow?]
If START = NULL , then Write : UNDERFLOW & return
2. [Assign START to ITEM]
Set ITEM := START
3. Set START := RIGHT[START]
4. [Make left link is null]
Set LEFT[START] := NULL
5. [Returns deleted node to AVAIL list]
Set RIGHT[ITEM] := AVAIL
Set AVAIL := ITEM
6. Exit
49. From the End :
DELETE_END(INFO,RIGHT,LEFT,AVAIL,START,ITEM)
1. [Underflow?]
If START = NULL , then Write : UNDERFLOW & return
2. [Assign START to ITEM]
Set ITEM := START
3. [Traverse list upto end]
Repeat step 5 until RIGHT[ITEM] != NULL
50. 4. Set LOC := ITEM
Set ITEM := RIGHT[ITEM]
5. [Delete the last node]]
Set RIGHT[LOC] := NULL
6. [Returns deleted node to AVAIL list]
Set RIGHT[ITEM] := AVAIL
Set AVAIL := ITEM
7. Exit
51. From specified position :
DEL_LOC(INFO,RIGHT,LEFT,START,AVAIL,ITEM)
1. [Underflow ?]
If START = NULL then Write : UNDERFLOW & Exit
2. [Initialize the counter and TEMP]
Set Cnt := 1
Set TEMP := START
3. Repeat steps 4 to 6 until Cnt < Loc
4. Set PTR := TEMP
5. Set TEMP := RIGHT[TEMP]
6. Set Cnt := Cnt +1
52. 7. [Delete an element]
ITEM := INFO[TEMP]
8. [Adjust the link]
Set RIGHT[PTR] := RIGHT[TEMP]
9. [Return deleted node to AVAIL list]
Set RIGHT[ITEM] := AVAIL
Set AVAIL := ITEM
10. Exit
53. Operations on Linked Lists:
In a well-designed list data structure, you should be able to
manipulate its elements without knowing anything about its
data. Unlike arrays, the entry point into any linked list is the
head of the list.
1. Traversing a linked list.
2. Append a new node (to the end) of a list
3. Propend a new node (to the beginning) of the list
4. Inserting a new node to a specific position on the list
5. Deleting a node from the list
6. Updating a node in the list
54. Static and Dynamic Memory Allocation :
Static Memory Allocation
• Fast access to elements
• Expensive to insert/remove element
• Have fixed maximum size
• Memory is reserved at the time of compilation.
Eg: Arrays, Records
Dynamic Memory Allocation
• Fast insertion/deletion of element
• Slower access to the element
• Have flexible size
• Memory allocation for the data structure takes place at
the
run time, only required amount of memory is allocated.
Eg: Link lists, Stacks, Queues, Trees