DSA Chapter 3 Part-One
DSA Chapter 3 Part-One
Static data structures are defined and allocated at compile time. Their size remains fixed during
execution, which means they cannot be resized.
The array is a classic example of a static data structure. Once an array is declared, its size cannot
be altered.
Characteristics:
Dynamic data structures can change in size during execution. They allow for the allocation and
deallocation of memory as needed, enabling more flexible data management.
Characteristics:
1|Page
Structures in Data Management
A structure in C++ is a user-defined data type that allows grouping of variables of different types
under a single name. Each variable in the structure is called a member.
✓ Declaration:
struct Student {
string name;
int age;
string dept;
};
Here, Student is a structure with three members: name, age, and dept.
✓ Accessing Members:
o Dot Operator (.): Used to access members of a structure variable.
Student stud;
stud.name = "John";
o Arrow Operator (->): Used to access members of a structure through a pointer.
Student *studPtr = &stud;
studPtr->age = 20;
Self-Referential Structures
A self-referential structure contains a pointer to a structure of the same type. This is the foundation
for building linked lists.
✓ Example:
struct Node {
int data;
Node* next;
};
Here, Node is a structure that contains an integer data and a pointer next to another Node.
2|Page
Linked Lists
A linked list is a self-referential structure composed of nodes, where each node contains data and
a pointer to the next node (in a singly linked list) or both the next and previous nodes (in a doubly
linked list).
The least complex linked list is the singly linked list, where a head node points to a node, that node
points to a node, and so on until the tail is reached.
A common example of this is a train: all cars are connected together singly.
Start (Head): Special pointer that points to the first node of a linked list, so that we can keep track
of the linked list. The last node should points to NULL to show that it is the last link in the chain
(in the linked list).
According to the above example in the figure, it is the singly linked list which has four nodes
in it, each with a link to the next node in the series (in the linked list).
Structure of a Node
Each node typically consists of:
➢ A data field to store the value.
➢ A pointer field to point to the next node.
Example Node Structure:
struct Node {
char name[20]; // Name can be up to 20 characters
int age; // Age of the student
float height; // Height in meters
Node* next; // Pointer to the next node
};
3|Page
Advantages of Linked Lists
✓ Dynamic Size: Unlike arrays, linked lists can grow or shrink as needed.
✓ Efficient Memory Use: Memory is allocated as necessary, which can lead to more
efficient usage.
Creating and Managing Linked Lists
1. Defining the Linked List Structure:
struct Node {
int data;
Node* next;
};
Node* head = NULL; // Initialize the head pointer
2. Adding a Node:
o At the End:
o At the Front:
4|Page
C++ Implementation of a Singly Linked List
1. Adding a Node:
➢ At the End:
struct Node {
int data;
Node* next;
};
Node* head = NULL; // Initialize head pointer
// Function to insert a node at the end
void insert_end(int x) {
Node* temp = new Node;
temp->data = x;
temp->next = NULL;
if (head == NULL) {
head = temp; // If list is empty, new node is head
} else {
Node* temp2 = head;
while (temp2->next != NULL) {
temp2 = temp2->next; // Traverse to the last node
}
temp2->next = temp; // Link last node to new node
}
}
➢ At the Front:
void insert_front(int x) {
Node* temp = new Node;
temp->data = x;
temp->next = head;
head = temp;
}
2. Deleting a Node:
5|Page
➢ From the Front:
void delete_front() {
if (head == NULL) {
cout << "List is empty\n";
} else {
Node* temp = head;
head = head->next;
delete temp;
}
}
➢ From the End:
void delete_end() {
if (head == NULL) {
cout << "List is empty\n";
} else {
Node* temp = head;
Node* prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
if (prev == NULL) {
head = NULL;
} else {
prev->next = NULL;
}
delete temp;
}
}
3. Displaying the List:
void display() {
Node* temp = head;
if (temp == NULL) {
cout << "List is empty\n";
} else {
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
6|Page
}
}
✓ Doubly linked lists enhance the functionality of singly linked lists by allowing traversal in
both directions.
✓ Each node points not only to Successor node (Next node), but also to Predecessor node
(Previous node).
✓ There are two NULL: at the first and last nodes in the linked list.
A doubly linked list is a type of linked list in which each node consists of 3 components:
1. *prev - address of the previous node
2. Data - data item
3. *next - address of next node
Advantage: given a node, it is easy to visit its predecessor (previous) node. It is convenient to
traverse linked lists Forwards and Backwards.
7|Page
Structure:
struct Node {
int data;
Node* prev;
Node* next;
}
Operations:
Adding a Node:
✓ At the Front:
✓ Insertion at the beginning in a doubly linked list involves adding a new node before the head
node. The new node’s next pointer points to the current head, and the current head’s prev
pointer is updated to the new node. The head of the list is then updated to the new node.
void insert_front(int x) {
// Step 1: Create a new node
Node* temp = new Node;
temp->data = x; // Set the data of the new node
temp->prev = NULL; // Since it's the new head, prev is NULL
8|Page
struct Node {
int data;
Node* next;
Node* prev;
};
void insert_end(Node*& head, Node*& tail, int x) {
Node* temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL) {
head = temp;
tail = temp;
} else {
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
Deleting a Node:
✓ From the Front:
void delete_front() {
if (head == NULL) {
cout << "List is empty\n";
} else {
Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
} else {
tail = NULL;
}
delete temp;
}}
9|Page
✓ From the End:
void delete_end() {
if (tail == NULL) {
cout << "List is empty\n";
} else {
Node* temp = tail;
tail = tail->prev;
if (tail != NULL) {
tail->next = NULL;
} else {
head = NULL;
}
delete temp;
}
}
Displaying the List:
✓ Forward:
void display_forward() {
Node* temp = head;
if (temp == NULL) {
cout << "List is empty\n";
} else {
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}
10 | P a g e