CH 3 Queue and Linked List Part II
CH 3 Queue and Linked List Part II
1
Overview
Unit 3: Queue and Linked List (10 hrs)
1. Queue
1.1.Definition and Queue Operations
1.2.Queue ADT and its Array Implementation
1.3.Circular Queue and its Array Implementation
1.4.Double Ended Queue and Priority Queue
2. Linked List
2.1.List- Definition and List Operations
2.2.List ADT and its Array Implementation
2.3.Linked List- Definition and its Operations
2.4.Singly Linked List- Basic Operations, Singly Linked List ADT and
Implementation of Singly Linked List
2.5.Doubly Linked List and Circular Linked List
2.6.Linked Implementation of Stack and Queue
• In Array implementations:
• all elements of the list are usually assumed to have the same data
type
• In other implementation:
head tail
11 22 33 44
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
Write algorithms:
• To insert an item in sorted list
• To delete a given ITEM from the list
head tail
Item = 66
11 22 33 44
0 1 2 3 4
head tail
33 66 head tail
0 1 Concatenate
33 66 55 44 11
head tail
0 1 2 3 4
55 44 11
0 1 2
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 18
List: Classwork
Write algorithms
• To merge two lists into a single third list
head tail
33 66 head tail
0 1 Merge
11 33 44 55 66
head tail
0 1 2 3 4
11 44 55
0 1 2
Dr. Udaya R. Dhungana, Pokhara University, Nepal. udaya@pu.edu.np 19
Array and List
• In a list, the missing spot is filled in when something is deleted.
• In an array, an empty variable is left behind when something is
deleted.
head A B C null
head A B C null
head A B C null
head A B C null
node2 Pointer to
Data field Link field next node
node2
{
node2
int info;
node *link; node2
};
• Dynamic implementation
• To better understand linked list and its dynamic nature, let's use the analogy
of something we are all familiar with; contact list on our phone. Imagine will
have five names in our phone contact which are implemented using a static
data structure such as arrays like [Abby, Demi, Gina, Rita, Ursula]. In a
structure such this, if another name 'Bobby' is to be inserted between Abby
and Demi, it means the size of the structure has to be increased by one, and
the data items from Demi has to be shifted one position to the right. While
that may not seem like much work given the number of contacts in this case,
imagine a case of 10,000 or 1,000,000 contacts, shifting and repositioning
that much data would amount to enormous strain on the computer processing
resources. That is where dynamic data structures such as linked list come in.
Rather than representing data contiguously next to each other in the
computer memory, linked list employs the concept of nodes and pointers. A
node holds the data item in the particular memory block and a pointer links
from one node to another and references the memory address of the next and
previous nodes (in the case of doubly linked list). So, we can represent our
contacts list using a linked list as follows:
• —>[Abby]-->[Demi]-->[Gina]-->[Rita]-->[Ursula]-->NULL
• [Abby] is known as the head (first) node and the arrows represent the
pointers. [Ursula] is known as the tail (last) node and it references to
NULL to indicate the end of the list. Notice the first arrow has no
previous node, it is known as the local pointer variable without which
we can not traverse the linked list.
• To add [Bobby] in between [Abby] and [Demi], all we need to do is to set
[Bobby]'s next pointer to reference [Demi] and [Abby]'s next pointer to
reference [Bobby]. As you can see, there is no need to re-adjust the
positions of the other elements in the structure. In the same way,
deleting a node such as [Gina] would require that we disconnect its next
pointer from [Rita] while the next pointer of [Demi] is then set to
reference [Rita]. Regardless of whether the number of nodes is just a
few or in the millions, the insertion and removal operations is the same
and proves to be more effective than in other data structure types.
1. One or more data fields which stores the data in the list
• The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper limit
irrespective of the usage.
• Inserting a new element in an array of elements is expensive because the room has to
be created for the new elements and to create room existing elements have to be
shifted. Deletion is also expensive with arrays due to same reason.
• Pointers in linked list overcome the above limitations at the cost of extra space for pointers.
• The list is not required to be contiguously present in the memory. The node can reside any
where in the memory and linked together to make a list. This achieves optimized utilization
of space.
• list size is limited to the memory size and doesn't need to be declared in advance.
• Insert an element
• Delete an element
• Traversing an element
• Searching an element
head 11 22 33 null
Algorithm:
1.Create a newNode
2.Assign value to newNode
3.Check whether list is Empty (head == NULL)
4.If it is Empty then,
set newNode→next = NULL and
head = newNode.
5.If it is Not Empty then,
set newNode→next = head and
head = newNode.
head
he
=
d=
e
od
hea
wN
ne
44 node1
Algorithm:
1.Create a newNode
2.Assign value to newNode
3.Set newNode → next as NULL.
4.Check whether list is Empty (head == NULL).
5.If it is Empty then, set head = newNode.
6.If it is Not Empty then,
a.de ne a node pointer temp and ini alize
with head.
7.Keep moving the temp to its next node un l it
reaches to the last node in the list (un l temp →
next is equal to NULL).
8.Set temp → next = newNode.
head
11 node2 22 node3 33 newNode
44 null
newNode
Displaying the contents of a linked list is very simple. We keep moving the temp
Singly Linked List
Algorithm:
//C implementation
1. Check whether list is Empty (head == NULL) struct node *temp = head;
2. If it is Empty then, display 'List is Empty.' and printf("\n\nList elements are - \n");
terminate the function.
while(temp != NULL) {
3. If it is Not Empty then, define a Node
printf("%d --->",temp->data);
pointer 'temp' and initialize with head.
temp = temp->next;
4. Keep displaying temp → data with an arrow (--->)
until temp reaches to the last node }
• A doubly linked list is a list that has two references from each node,
• one to the next node and
• another to the previous node.
• Doubly linked lists also starts from head node, but provide access both ways.
• The flexibility of a doubly linked list is that one can traverse forward or
backward from any node.
• A doubly linked list may be suitable for applications that require frequent
updates to nodes that are in a close range.
TAIL
• A doubly linked list allows convenient access from a list node to the next node
and also to the preceding node on the list.
• The doubly linked list node accomplishes this in the obvious way by storing two
pointers:
• one to the node following it (as in the singly linked list), and
• A node is represented as
class node
{ node node1;
int data;
node *next;
node *prev;
}
node1
1. Insertion
Doubly Linked List
Node *newNode;
1.Create a newNode newNode = new Node();
2.Assign value to newNode newNode->data = 33;
3.Assign newNode → previous = NULL newNode->prev = null;
if(head == null){
4.Check whether list is Empty (head == NULL)
newNode->next = null;
5.If it is Empty then,
head = newNode;
set newNode→next = NULL and }
head = newNode. else {
6.If it is Not Empty then, newNode->next = head;
set newNode→next = head and head = newNode;
head = newNode. }
head
• In a doubly linked list, we have two types of traversals as we have two pointers with
different directions in the doubly linked list.
1. Forward traversal –
a. Traversal is done using the next pointer which is in the forward direction.
b. Start with the front node and visit all the nodes until the node becomes
NULL.
2. Backward traversal –
b. Start with the end node and visit all the nodes until the node becomes
NULL.
Assignment
Assignment
• Dynamic size: the size in linked list can grow or shrink in run time
• Fast and efficient insertion and deletion (since it does not require
shifting operations)
89