0% found this document useful (0 votes)
4 views

DSA Chapter 3 Part-One

Chapter 3 discusses data structures, categorizing them into static and dynamic types, with arrays as examples of static structures and linked lists as dynamic structures. It explains the characteristics, advantages, and operations of linked lists, including singly and doubly linked lists, and provides C++ implementations for various operations such as adding, deleting, and displaying nodes. The chapter emphasizes the flexibility and efficient memory usage of dynamic data structures compared to static ones.

Uploaded by

ejetamulugeta6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DSA Chapter 3 Part-One

Chapter 3 discusses data structures, categorizing them into static and dynamic types, with arrays as examples of static structures and linked lists as dynamic structures. It explains the characteristics, advantages, and operations of linked lists, including singly and doubly linked lists, and provides C++ implementations for various operations such as adding, deleting, and displaying nodes. The chapter emphasizes the flexibility and efficient memory usage of dynamic data structures compared to static ones.

Uploaded by

ejetamulugeta6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 3:

Data Structures and Applications


Overview of Data Structures
Data structures are essential for organizing and managing data in computing. They can be broadly
categorized based on their memory allocation strategies:

1. Static Data Structures


2. Dynamic Data Structures

Static Data Structures

Static data structures are defined and allocated at compile time. Their size remains fixed during
execution, which means they cannot be resized.

Example: Array implementation of ADTs.

The array is a classic example of a static data structure. Once an array is declared, its size cannot
be altered.

Characteristics:

➢ Memory Allocation: Allocated at compile time.


➢ Efficiency: Fast access to elements since the memory locations are contiguous.
➢ Limitations: Inflexibility in size; resizing requires creating a new array and copying the
elements.

Dynamic Data Structures

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.

Example: Linked lists are a prominent example of dynamic data structures.

Characteristics:

➢ Memory Allocation: Allocated at runtime.


➢ Efficiency: More memory-efficient as they can grow and shrink as needed.
➢ Flexibility: Users do not need to specify the size at the beginning.

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:

❖ Allocate memory for a new node.


❖ Assign values and set the next pointer to NULL.
❖ Update the last node's next pointer to point to the new node.

o At the Front:

❖ Allocate a new node.


❖ Set its next pointer to the current head.
❖ Update head to point to the new node.

3. Displaying the Linked List:


o Traverse from the head until a NULL pointer is encountered, displaying each node's
data.
4. Deleting a Node:
o Handle various cases (front, end, specific data) ensuring pointers are updated to
maintain list integrity.

Types of Linked Lists


1. Singly Linked Lists: Each node points to the next node, with the last node pointing to
NULL.
2. Doubly Linked Lists: Each node contains pointers to both the next and previous nodes,
allowing for bidirectional traversal.
3. Circular Linked Lists: The last node points back to the first node, forming a circular
structure.

4|Page
C++ Implementation of a Singly Linked List

Operations on Linked Lists

• Adding nodes to the front, end, or at a specific position.


• Deleting nodes from various positions.
• Displaying the list in both forward and backward directions.

Operations on Singly Linked Lists

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

✓ 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.

Operations of Doubly Linked List

✓ Adding a node to the end of a doubly linked list


✓ Adding a node to the front of a doubly linked list
✓ Adding a node to the left of a specific data in a doubly linked list
✓ Adding a node to the right of a specific data in a doubly linked list
✓ Deleting a node from the end of a doubly linked list
✓ Deleting a node from the front of a doubly linked list
✓ Deleting any node using the search data from a doubly linked list
✓ Display the node from the doubly linked list in a forward manner
✓ Display the node from the doubly linked list in a backward manner.

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.

Example Adding node at front with Comments:

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

// Step 2: Check if the list is empty


if (head == NULL) {
// If the list is empty, the new node is both head and tail
head = temp;
tail = temp;
} else {
// Step 3: If the list is not empty
temp->next = head; // New node's next points to the current head
head->prev = temp; // Current head's prev points to the new node
head = temp; // Update head to point to the new node
}
}
At the End:
✓ Insertion at the end of a doubly linked list involves adding a new node after the last node. The
new node’s prev pointer points to the current last node, and its next pointer is set to NULL
(since it will be the new last node). The next pointer of the current last node is updated to point
to the new node. Finally, the tail pointer of the list is updated to point to the new node.

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

You might also like