Data Structure Using C++ (Unit 02)
Data Structure Using C++ (Unit 02)
Chapter = 1, QUEUES
Definition:
A Queue is defined as a linear data structure that is open at both ends and the
operations are performed in First in First Out (FIFO) order.
We define a queue to be a list in which 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 operation is first performed on that.
The Queue Data Structure is a fundamental concept in computer science used for
storing and managing data in a specific order.
• It follows the principle of "First in, First out" (FIFO), where the first element added
to the queue is the first one to be removed.
• Queues are commonly used in various algorithms and applications for their simplicity
and efficiency in managing data flow.
Characteristics of Queue:
• Queue can handle multiple data.
• We can access both ends.
• They are fast and flexible.
Types of Queue Data Structure:
The major drawback of using a linear Queue is that insertion is done only from
the rear end. If the first three elements are deleted from the Queue, we cannot insert
more elements even though the space is available in a Linear Queue. In this case, the
linear Queue shows the overflow condition as the rear is pointing to the last element of
the Queue.
2. 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.
In Circular Queue, all the nodes are represented as circular. It is similar to the
linear Queue except that the last element of the queue is connected to the first element.
It is also known as Ring Buffer, as all the ends are connected to another end. The
representation of circular queue is shown in the below image -
The drawback that occurs in a linear queue is overcome by using the circular
queue. If the empty space is available in a circular queue, the new element can be
added in an empty space by simply incrementing the value of rear. The main
advantage of using the circular queue is better memory utilization.
o Input restricted deque - As the name implies, in input restricted queue, insertion
operation can be performed at only one end, while deletion can be performed from
both ends.
o Output restricted deque - As the name implies, in output restricted queue, deletion
operation can be performed at only one end, while insertion can be performed from
both ends.
4. Priority Queue: It is a special type of queue in which the elements are arranged based
on the priority. It is a special type of queue data structure in which every element has a
priority associated with it. Suppose some elements occur with the same priority, they
will be arranged according to the FIFO principle. The representation of priority queue
is shown in the below image -
Insertion in priority queue takes place based on the arrival, while deletion in the
priority queue occurs based on the priority. Priority queue is mainly used to implement
the CPU scheduling algorithms.
There are two types of priority queue that are discussed as follows -
1. Task Scheduling: A queue is an ideal data structure for task scheduling, which lets
you perform all the tasks in order. Initially, all the tasks are pushed to the end of the
queue. We can pick the task at the front of the queue and execute it. Once you
complete the task, it is removed from the queue, and you select the next task. This
process will continue until the queue is empty.
2. Batch Processing: Similar to task scheduling, you can use a queue data structure
when you need to perform a large number of tasks in batches. Queue helps to
complete all the tasks in order without leaving a task unprocessed.
3. Event Handling: In event handling, a queue data structure stores all the events that
have occurred but are yet to be processed by the system. If a new event occurs, it is
pushed to the end of the queue, and the system then processes the event from the front
of the queue and removes it once handled.
4. Message Buffering: Message buffer stores the messages in the order they arrive and
processes them one by one. The queue is an ideal data structure because of its FIFO
(First In, First Out) property.
5. Traffic Management: Circular queues are used to manage the traffic lights in a
traffic management system for switching the traffic lights one by one, in order.
6. Application of queues in operating systems are:
• It helps in First Come, First Serve to schedule.
• It also makes CPU scheduling easier.
• It is used in memory management.
7. Applications of Queue in Networks are:
• Queues are used in routers and switches.
• It is used in packet switching.
8. Some Other Useful Applications of Queue
• WhatsApp uses a queue to queue up the messages in the WhatsApp server if the
recipient’s internet connection is turned off.
• It is used in music applications to queue up a new song in a playlist.
• It is used in traffic management to manage traffic lights.
Chapter = 2, Linked List
Definition:
A linked list is a linear data structure which can store a collection of "nodes"
connected together via links i.e., pointers.
• Linked lists nodes are not stored at a contiguous location, rather they are linked
using pointers to the different memory locations. A node consists of the data
value and a pointer to the address of the next node within the linked list.
• A linked list is a dynamic linear data structure whose memory size can be
allocated or de-allocated at run time based on the operation insertion or
deletion, this helps in using system memory efficiently. Linked lists can be used
to implement various data structures like a stack, queue, graph, hash maps, etc.
A linked list starts with a head node which points to the first node. Every node
consists of data which holds the actual data (value) associated with the node and a next
pointer which holds the memory address of the next node in the linked list. The last
node is called the tail node in the list which points to null indicating the end of the list.
Example:
Example:
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.
Example:
A singly linked list is a fundamental data structure, it consists of nodes where each node
contains a data field and a reference to the next node in the linked list. The next of the last
node is null, indicating the end of the list. Linked Lists support efficient insertion and
deletion operations.
Understanding Node Structure
In a singly linked list, each node consists of two parts: data and a pointer to the next node.
This structure allows nodes to be dynamically linked together, forming a chain-like
sequence.
In this example, the Node class contains an integer data field (data) to store the information
and a pointer to another Node (next) to establish the link to the next node in the list.
Operations on Singly Linked List
• Traversal
• Searching
• Length
• Insertion:
1. Insert at the beginning
2. Insert at the end
3. Insert at a specific position
• Deletion:
1. Delete from the beginning
2. Delete from the end
3. Delete a specific node
Step-by-step approach:
Output
To delete the last node, traverse the list until the second-to-last node and update its next
field to None.
Step-by-step approach:
• Check if the head is NULL.
o If it is, return NULL (the list is empty).
• Check if the head's next is NULL (only one node in the list).
o If true, delete the head and return NULL.
• Traverse the list to find the second last node (second_last).
• Delete the last node (the node after second_last).
• Set the next pointer of the second last node to NULL.
• Return the head of the linked list.
C++ program to delete the last node of linked list
#include <iostream.h>
#include <conio.h>
struct Node {
int data;
Node* next;
Node(int data)
: data(data), next(NULL)
{
}
};
Node* removeLastNode(struct Node* head) {
if (head == NULL) {
return NULL;
}
if (head->next == NULL) {
delete head;
return NULL;
}
Node* second_last = head;
while (second_last->next->next != NULL) {
second_last = second_last->next;
}
delete (second_last->next);
second_last->next = NULL;
return head;
}
void printList(Node* head) {
while (head != NULL) {
cout << head->data;
if (head->next != NULL) {
cout << ", ";
}
head = head->next;
}
cout << endl;
}
void main() {
clrscr();
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
cout << "Original list: ";
printList(head);
head = removeLastNode(head);
cout << "List after removing the last node: ";
printList(head);
getch();
}
Output
Original list: 1,2,3,4,5
Memory Layout
Consider a singly linked list storing three elements: 12 13 15 18 NULL.
Its memory representation can be visualized as:
Address Data Next Pointer
150 12 250
250 13 350
350 15 450
450 18 NULL
Here:
• 1001, 2003, and 3005 are memory addresses of the respective nodes.
• The Next Pointer in each node holds the address of the next node in the list.
• The Next Pointer of the last node is NULL, indicating the end of the list.
Key Points
1. The head pointer of the list stores the memory address of the first node.
2. Traversing the list involves moving from one node to the next by following the Next
pointers until NULL is encountered.
3. Memory is dynamically allocated for each node during runtime, allowing the list size
to grow or shrink as needed.
4. Memory representation is not contiguous, meaning the nodes can be scattered across
the memory.
This structure is efficient for insertion and deletion operations but requires extra memory for
storing the pointers.
Memory Representation of a Doubly Linked List
A doubly linked list in C++ is a linear data structure where each node contains three
components:
1. Node Structure
In memory, these nodes are stored in non-contiguous locations. The prev and next pointers
create the links.
struct Node {
int data; // Data part
Node* prev; // Pointer to the previous node
Node* next; // Pointer to the next node
};
2. Head and Tail Pointers
In this diagram, -1 represents the NULL value, indicating the start or end of the list. For
example, the first node (A) has its previous address as -1, and the last node (D) has its next
address as -1, forming the structure -1 ← A ⇆ B ⇆ C ⇆ D → -1.
4.Memory representation of doubly linked list:
For example:
5. Key Points