You are on page 1of 7

Data Structures and Algorithms

CIS-318
Lab Report no.8
Spring 2023
Obtained Marks
Total Marks

Lab Engineer Signature &


Comments

Student Name
1. Anas Zohrab

Section: A (Electronics)
Experiment No: 08 Date of Submission:
April-10-2023
Experiment Title:
Introduction to Queues and its implementation using Link List
Batch: Teacher:
BSEE 2019-23 Dr. Kamran Safdar
Semester Lab Engineer:
8th Mr. Ghaznooq Ahmad

Department of Electrical Engineering


2 Lab 6 : Introduction to Queues and its implementation using Link List

Table of Contents
Introduction to Queues and its implementation using Link List........................................................................ 1
8.1 Title: Introduction to Doubly and Circular Link List ............................................................................. 3
8.2 Abstract: .................................................................................................................................................. 3
8.3 Objectives: .............................................................................................................................................. 3
8.4 Introduction: ............................................................................................................................................ 3
8.5 Results and Code..................................................................................................................................... 4
1. • void enqueue(int x) ............................................................................................................................ 4
2. Result:.................................................................................................................................................... 5
1. • void dequeue() .................................................................................................................................... 5
2. Result:.................................................................................................................................................... 5
• printQueue ............................................................................................................................................ 5
3. Result:.................................................................................................................................................... 6
• isEmpty() ............................................................................................................................................... 6
4. Result:.................................................................................................................................................... 6
8.6 Home Tasks ............................................................................................................................................ 6
8.7 Discussion and Conclusion: .................................................................................................................... 7
8.8 References: .............................................................................................................................................. 7
3 Lab 6 : Introduction to Queues and its implementation using Link List

8.1 Title: Introduction to Doubly and Circular Link List

8.2 Abstract:
The objective of this lab was to gain a deeper understanding of Queues. Queues are a
fundamental data structure used in many applications. This lab explored the implementation of
queues using linked lists in C++. We first reviewed the queue data structure and its basic
operations: enqueue, dequeue, display, and isEmpty. We then implemented these operations
on a queue implemented using a linked list.

8.3 Objectives:
• Understanding of queues
• Implementing the basic operations of queues using link list

8.4 Introduction:
Data Structure List:
A list is a data structure used to store a collection of elements in a particular order. Each element
in the list is called a node, and each node contains a value (also known as data or key) and a
reference to the next node in the list. The first node is called the head of the list, and the last
node is called the tail of the list.

Figure 1 Linked List

Queue:

Queues are a type of abstract data structure that represents a collection of elements in
a linear order, where elements are added at the back and removed from the front, following
the "First-In-First-Out" (FIFO) principle. In a queue, the element that is first to be added is
also the first to be removed. Elements that are added later are placed at the back of the queue
and are removed only after all the earlier elements have been removed. Queues can be
visualized as a line of people waiting for a service, where the person who arrives first is the
first to receive the service. As more people arrive, they line up behind the last person, and are
served in the order in which they joined the line.

Queues can be implemented using various data structures such as arrays, linked lists,
and stacks. The linked list implementation is particularly useful when the size of the queue is
unknown or can change during runtime. Queues have several practical applications, such as
in computer systems where they are used to manage requests from multiple users or to
4 Lab 6 : Introduction to Queues and its implementation using Link List

schedule tasks to be executed by a computer. They are also used in transportation systems,
such as bus stations and airports, to manage waiting lines.

Figure 2Queue Data Structure

Basic Operations:

• Enqueue: It adds a new element to the back of the queue. This operation is also called
insertion. It takes an item as an input parameter and inserts it at the back of the queue.

• Dequeue: It removes the first element from the front of the queue. This operation is
also called deletion. It removes the front element of the queue and returns the
removed element.

• Display: It displays all the elements in the queue. It starts from the front of the queue
and displays each element in turn.

• IsEmpty: It checks whether the queue is empty or not. It returns true if the queue is
empty and false otherwise.

8.5 Results and Code


Create a class Queues and implement the following functions
• Enqueue(Adding/Inserting of Items)
• Dequeue(Deleting/removing of items)
• Display(Display entire queue)
• IsEmpty(Return True if empty)

1. • void enqueue(int x)
void enqueue(int x) {

Node* newNode = new Node;


newNode->data = x;
newNode->next = NULL;

if (isEmpty()) {
front = rear = newNode;
}

else {
5 Lab 6 : Introduction to Queues and its implementation using Link List

rear->next = newNode;
rear = newNode;
}
}
2. Result:

Figure 3 adding 1 20 555 40 in the Queue

1. • void dequeue()

void dequeue() {

if (isEmpty()) {
return;
}

if (front == rear) {
front = rear = NULL;
}

else {
Node* temp = front;
front = front->next;
delete temp;
}
}
2. Result:

Figure 3 removing element “1” from the front

• printQueue
void printQueue() {

if (isEmpty()) {
cout << "Queue is empty" << endl;
return;
}
6 Lab 6 : Introduction to Queues and its implementation using Link List

Node* temp = front;


while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
3. Result:

Figure 4 Printing the Queue

• isEmpty()
bool isEmpty() {
return (front == NULL && rear == NULL);
}
if (isEmpty()) {
cout << "Queue is empty" << endl;
return;
}
4. Result:

Figure 5 Queue is Empty when its printed at the start without adding any bits to it.

8.6 Home Tasks


Task 1 : What is the use of rear end pointer

The rear end pointer is used in a queue data structure that is implemented using a linked
list. It keeps track of the last node in the queue. When a new element is added to the queue, it
is inserted at the rear end of the queue and the rear pointer is updated to point to the newly
added element. Similarly, when an element is removed from the queue, the front end pointer is
updated to point to the next element in the queue and the old front element is deleted. The rear
7 Lab 6 : Introduction to Queues and its implementation using Link List

end pointer is useful because it allows for constant time insertion at the end of the queue, which
is an important operation for queue operations.

Task 2: What is time complexity of queue functions.


For a queue implemented using a linked list, the time complexity of enqueue,
dequeue, and isEmpty operations are O(1) since they only involve modifying the head and
tail pointers of the linked list. However, the time complexity of display operation in a linked
list implementation is O(n) since it requires traversing the entire linked list to print out all the
elements.

Q3: Can queues be implemented using arrays? If yes then what are the advantages or
disadvantages of using array.
Yes, queues can be implemented using arrays. The advantage of using an array to implement
a queue is that it is simple and easy to implement. The disadvantage of using an array is that
it has a fixed size, which means that it can only hold a limited number of elements. If the
queue becomes full, then new elements cannot be added to the queue, even if there is space in
memory.

8.7 Discussion and Conclusion:


In this lab, we aimed to implement the basic operations of queues using a linked list. We first
reviewed the queue data structure, which is a FIFO (first-in, first-out) linear data structure where
elements are added at the back of the queue and removed from the front. We then implemented the
four basic operations of a queue:

Enqueue: This operation adds an element to the back of the queue. We implemented this by
creating a new node, setting its data to the input element, and adjusting the rear pointer to point to
this new node. Dequeue: This operation removes the front element from the queue. We implemented
this by first checking if the queue is empty. If not, we remove the front node by advancing the front
pointer to the next node and deleting the old front node. Display: This operation prints all the
elements of the queue from front to back. We implemented this by traversing the nodes from the
front pointer and printing the data of each node. IsEmpty: This operation checks if the queue is
empty. We implemented this by simply checking if both the front and rear pointers are null.

Overall, this lab helped strengthen our understanding of the queue data structure and how to
implement its basic operations using a linked list. The linked list implementation is useful because it
allows for an unlimited number of elements to be stored in the queue. In conclusion, this lab helped
us to gain a deeper understanding of the data structure list and to implement it using a Singly Linked
List. We implemented the basic operations of Linked Lists such as InsertNodeAtFront(),
InsertNodeAtBack(), DeleteNodeFromFront(), DeleteNodeFromBack(), and PrintList

8.8 References:
• •Tutorials Point. (n.d.). Queues. Retrieved from
https://www.tutorialspoint.com/data_structures_algorithms/queue_data_structure.htm
• •Programiz. (n.d.). Queue in C++. Retrieved from https://www.programiz.com/dsa/queue
• •GeeksforGeeks. (n.d.). Queue. Retrieved from https://www.geeksforgeeks.org/queue-data-
structure/

You might also like