TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
QUEUES ADT
A queue is a linear data structure that is open at both ends and the operations are performed
in First in First out (FIFO) order; all additions to the list are made at one end, and all deletions
from the list are made at the other end. The element which is first pushed into the order, the
delete operation is first performed on that.
FIFO Principle of Queue:
A Queue is like a line waiting to purchase tickets, where the first person in line is the first
person served. (i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue (sometimes, head of the queue),
similarly, the position of the last entry in the queue, that is, the one most recently added, is
called the rear (or the tail) of the queue.
FIFO property of queue
Characteristics of Queue:
Queue can handle multiple data.
It can be accessed from both ends.
They are fast and flexible.
Queue Representation:
1. Queues can be represented in an array. Variables used in this case are:
Queue: the name of the array storing queue elements.
Front: the index where the first element is stored in the array representing the
queue.
Rear: the index where the last element is stored in an array representing the queue.
1
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
2. Linked List Representation of Queue: a queue can also be represented using following
entities:
Linked-lists,
Pointers, and
Structures.
Types of Queue:
1. Input Restricted Queue: This is a simple queue. In this type of queue, the input can be
taken from only one end but deletion can be done from any of the ends.
2. Output Restricted Queue: This is also a simple queue. In this type of queue, the input
can be taken from both ends but deletion can be done from only one end.
3. Circular Queue: This is a special type of queue where the last position is connected
back to the first position. Here also the operations are performed in FIFO order. To
know more refer this.
4. Double-Ended Queue (Dequeue): In a double-ended queue the insertion and deletion
operations, both can be performed from both ends. To know more refer this.
5. Priority Queue: A priority queue is a special queue where the elements are accessed
based on the priority assigned to them. To know more refer this.
Basic Operations for Queue in Data Structure:
1. Enqueue() – Adds (or stores) an element to the end of the queue..
START
Check if the queue is full.
If the queue is full, produce overflow error and exit.
If the queue is not full, increment rear pointer to point the next empty
space.
Add data element to the queue location, where the rear is pointing.
Return success.
END
2. Dequeue() – Removal of elements from the queue.
START
Check if the queue is empty.
If the queue is empty, produce underflow error and exit.
If the queue is not empty, access the data where front is pointing.
Increment front pointer to point to the next available data element.
Return success.
END
2
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
3. Peek() or front()- Acquires the data element available at the front node of the queue
without deleting it.
START
Return the element at the front of the queue
END
4. rear() – This operation returns the element at the rear end without removing it.
5. isFull() – Validates if the queue is full.
START
If the count of queue elements equals the queue size, return true
Otherwise, return false
END
6. isNull() – Checks if the queue is empty.
START
If the count of queue elements equals zero, return true
Otherwise, return false
END
Applications of Queue
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold people calling them
in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order
as they arrive i.e First come first served.
Example:
// Queue implementation in C
#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int items[SIZE], front = -1, rear = -1;
3
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
int main() {
//deQueue is not possible on empty queue
deQueue();
//enQueue 5 elements
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// 6th element can't be added to because the queue is full
enQueue(6);
display();
//deQueue removes element entered first i.e. 1
deQueue();
//Now we have just 4 elements
display();
return 0;
}
void enQueue(int value) {
if (rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("\nInserted -> %d", value);
}
}
void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
4
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
// Function to print the queue
void display() {
if (rear == -1)
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d ", items[i]);
}
printf("\n");
}
LINKED LISTADT
Linked List is an Abstract Data Type (ADT) that holds a collection of Nodes, the nodes can be
accessed in a sequential way. These nodes consist of the data to be stored and a pointer to the
address of the next node within the linked list. In the case of arrays, the size is limited to the
definition, but in linked lists, there is no defined size. Any amount of data can be stored in it and
can be deleted from it.
A linked list is a series of connected nodes, where each node is a data structure.
A linked list can grow or shrink in size as the program runs. This is possible because the
nodes in a linked list are dynamically allocated.
If new data need to be added to a linked list, the program simply allocates another node
and inserts it into the series.
If a particular piece of data needs to be removed from the linked list, the program deletes
the node containing that data.
Linked lists are among the simplest and most common data structures. They can be
used to implement other common abstract data types, including lists, stacks, queues, and
so on.
Linked List Representation
Linked list can be visualized as a chain of nodes, where every node points to the next node.
5
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
As per the above illustration, following are the important points to be considered. The Figure
below depicts the Nodes in the linked list as being very close to each other, neatly arranged in
a row. In reality, the Nodes may be scattered around various parts of memory.
Linked List contains a link element called first (head).
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
A Node with Data Member and Pointer
6
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
There are three types of linked lists −
Singly Linked List − the nodes only point to the address of the next node in the list.
Doubly Linked List − the nodes point to the addresses of both previous and next nodes.
Circular Linked List − the last node in the list will point to the first node in the list. It
can either be singly linked or doubly linked.
Singly Linked Lists
Singly linked lists contain two “buckets” in one node; one bucket holds the data and the other
bucket holds the address of the next node of the list. Traversals can be done in one direction only
as there is only a single link between two nodes of the same list.
Doubly Linked Lists
Doubly Linked Lists contain three “buckets” in one node; one bucket holds the data and the other
buckets hold the addresses of the previous and next nodes in the list. The list is traversed twice as
the nodes in the list are connected to each other from both sides.
Circular Linked Lists
Circular linked lists can exist in both singly linked list and doubly linked list.
Since the last node and the first node of the circular linked list are connected, the traversal in this
linked list will go on forever until it is broken.
7
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
Basic Operations in the Linked Lists
The basic operations in the Singly Linked Lists:
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this with
diagrams here. First, create a node using the same structure and find the location where it has to
be inserted.
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode).
Then point B.next to C −
NewNode.next −> RightNode;
It should look like this −
8
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
Now, the next node at the left should point to the new node.
LeftNode.next −> NewNode;
This will put the new node in the middle of the two. The new list should look like this −
Insertion in linked list can be done in three different ways. They are explained as follows −
Insertion at Beginning
In this operation, we are adding an element at the beginning of the list.
Algorithm
1. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and assign the head pointer to it.
5 If the list is not empty, add the data to a node and link to the current head. Assign the
head to the newly added node.
6. END
Insertion at Ending
In this operation, we are adding an element at the ending of the list.
Algorithm
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
9
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
Insertion at a Given Position
In this operation, we are adding an element at any position within the list.
Algorithm
1. START
2. Create a new node and assign data to it
3. Iterate until the node at position is found
4. Point first to new first node
5. END
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation. First,
locate the target node to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target node −
LeftNode.next −> TargetNode.next;
This will remove the link that was pointing to the target node. Now, using the following code, we
will remove what the target node is pointing at.
TargetNode.next −> NULL;
10
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
We need to use the deleted node. We can keep that in memory otherwise we can simply
deallocate memory and wipe off the target node completely.
Similar steps should be taken if the node is being inserted at the beginning of the list. While
inserting it at the end, the second last node of the list should point to the new node and the new
node will point to NULL.
Deletion in linked lists is also performed in three different ways. They are as follows −
Deletion at Beginning
In this deletion operation of the linked, we are deleting an element from the beginning of the list.
For this, we point the head to the second node.
Algorithm
1. START
2. Assign the head pointer to the next node in the list
3. END
Deletion at Ending
In this deletion operation of the linked, we are deleting an element from the ending of the list.
11
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
Algorithm
1. START
2. Iterate until you find the second last element in the list.
3. Assign NULL to the second last element in the list.
4. END
Deletion at a Given Position
In this deletion operation of the linked, we are deleting an element at any position of the list.
Algorithm
1. START
2. Iterate until find the current node at position in the list
3. Assign the adjacent node of current node in the list to its previous node.
4. END
Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed by the head node
and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall make it
point to its previous node −
We have to make sure that the last node is not the last node. So we'll have some temp node,
which looks like the head node pointing to the last node. Now, we shall make all left side nodes
point to their previous nodes one by one.
12
TOPIC 3: IMPLEMENTATION OF ADTs – QUEUES& LISTS
Except the node (first node) pointed by the head node, all nodes should point to their
predecessor, making them their new successor. The first node will point to NULL.
We'll make the head node point to the new first node by using the temp node.
Algorithm
Step by step process to reverse a linked list is as follows −
1 START
2. We use three pointers to perform the reversing: prev, next, head.
3. Point the current node to head and assign its next value to the prev node.
4. Iteratively repeat the step 3 for all the nodes in the list.
5. Assign head to the prev node.
13