Data structure chapter 3
Data structure chapter 3
Introduction
A linked list is a collection of “nodes” connected together via links. 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.
the exact size of a piece of data can only be determined at run-time based on features of the
operating environment, including user input;
the size of the data needs to change over time;
the data manipulation involves combining or splitting the data, which is particularly common
with string processing;
the data is extremely large;
the data needs persistence and scope that are not global, but are also not tied to the execution
context of a single function.
Allocating memory
There are two ways that memory gets allocated for data storage:
1
Dynamic Memory Allocation
We can dynamically allocate storage space while the program is running, but
we cannot create new variable names "on the fly"
For this reason, dynamic allocation requires two steps:
1. Creating the dynamic space.
2. Storing its address in a pointer (so that the space can be accesed)
To dynamically allocate memory in C++, we use the new operator.
De-allocation:
If creating an array dynamically, use the same form, but put brackets with a
size after the type:
These statements above are not very useful by themselves, because the
allocated spaces have no names! BUT, the new operator returns the starting
address of the allocated space, and this address can be stored in a pointer:
2
int * p; // declare a pointer p
p = new int; // dynamically allocate an int and load address into p
double * d; // declare a pointer d
d = new double; // dynamically allocate a double and load address into d
Linked List
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 list is a type of data structure that is made up of nodes that are created using self
referential structures. Each of these nodes contain two parts, namely the data and the reference to the
next list node. Only the reference to the first list node is required to access the whole linked list. This
is known as the head. The last node in the list points to nothing so it stores NULL in that part.
Example
Live Demo
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
3
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
Output
The linked list is: 9 2 7 1 3
In the above program, the structure Node forms the linked list node. It contains the data and a pointer
to the next linked list node. This is given as follows.
struct Node {
int data;
struct Node *next;
};
The function insert() inserts the data into the beginning of the linked list. It creates a new_node and
inserts the number in the data field of the new_node. Then the new_node points to the head. Finally
the head is the new_node i.e. the linked list starts from there. This is given below.
4
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
The function display() displays the whole linked list. First ptr points to head. Then it is continuously
forwarded to the next node until all the data values of the nodes are printed. This is given below.
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
In the function main(), first various values are inserted into the linked list by calling insert(). Then
the linked list is displayed. This is given below.
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
Doubly linked list is a type of data structure that is made up of nodes that are created using self
referential structures. Each of these nodes contain three parts, namely the data and the reference to
the next list node and the reference to the previous list node.
Only the reference to the first list node is required to access the whole linked list. This is known as
the head. The last node in the list points to nothing so it stores NULL in that part. Also the doubly
linked list can be traversed in both directions as each node points to its previous and next node.
6
output
The doubly linked list is: 9 2 7 1 3
In the above program, the structure Node forms the doubly linked list node. It contains the data and a
pointer to the next and previous linked list node. This is given as follows.
struct Node {
int data;
struct Node *prev;
struct Node *next;
};
The function insert() inserts the data into the beginning of the doubly linked list. It creates a
newnode and inserts the number in the data field of the newnode. Then the prev pointer in newnode
points to NULL as it is entered at the beginning and the next pointer points to the head. If the head is
not NULL then prev pointer of head points to newnode. Finally the head is the newnode i.e. the
linked list starts from there. This is given below.
void insert(int newdata) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = newdata;
newnode->prev = NULL;
newnode->next = head;
if(head != NULL)
head->prev = newnode ;
head = newnode;
}
The function display() displays the whole doubly linked list. First ptr points to head. Then it is
continuously forwarded to the next node until all the data values of the nodes are printed. This is
given below.
void display() {
struct Node* ptr;
ptr = head;
while(ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
In the function main(), first various values are inserted into the doubly linked list by calling insert().
Then the doubly linked list is displayed. This is given below.
7
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The doubly linked list is: ";
display();
return 0;
}
3.4. Circular Linked Lists and Its Implementation
Circular singly linked list is a type of data structure that is made up of nodes that are created using
self referential structures. Each of these nodes contain two parts, namely the data and the reference to
the next list node.
Only the reference to the first list node is required to access the whole linked list. This is known as
the head. The last node in the list points to head or first node of the list. That is the reason this is
known as a circular linked list.
Example
Live Demo
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
struct Node *ptr = head;
newnode->data = newdata;
newnode->next = head;
if (head!= NULL) {
8
while (ptr->next != head)
ptr = ptr->next;
ptr->next = newnode;
} else
newnode->next = newnode;
head = newnode;
}
void display() {
struct Node* ptr;
ptr = head;
do {
cout<<ptr->data <<" ";
ptr = ptr->next;
} while(ptr != head);
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The circular linked list is: ";
display();
return 0;
}
Output
The circular linked list is: 9 2 7 1 3
In the above program, the structure Node forms the linked list node. It contains the data and a pointer
to the next linked list node. This is given as follows.
struct Node {
int data;
struct Node *next;
};
9
The function insert() inserts the data into the beginning of the linked list. It creates a newnode and
inserts the number in the data field of the newnode. If the head is NULL, then newnode points to
itself otherwise the last node in the circular linked list points to newnode. Then the head points to the
start of the list i.e. to the newnode. This is given below.
The function display() displays the whole linked list. First ptr points to head. Then it is continuously
forwarded to the next node until all the data values of the nodes are printed. This is given below.
void display() {
struct Node* ptr;
ptr = head;
do {
cout<< ptr->data <<" ";
ptr = ptr->next;
} while(ptr != head);
}
In the function main(), first various values are inserted into the circular linked list by calling insert().
Then the linked list is displayed. This is given below.
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
10
insert(9);
cout<<"The circular linked list is: ";
display();
return 0;
}
11