Week3 LinkedLists
Week3 LinkedLists
30 100
Information
part
Pointer
(Address part) 4
Advantages of linked lists
5
Singly Linked Lists
◼ A singly linked list is a concrete data
structure consisting of a sequence of next
data
nodes - linear ordering
◼ Each node stores
◼ A data element node
◼ A link (pointer) to the next node
A B C D NULL
6
Singly Linked Lists
• Basically Singly Linked Lists are uni-directional as they can only
point to the next Node in the list but not to the previous.
7
Singly Linked Lists
• A node references another node, the next reference
inside a node is a link or pointer to another node.
8
Singly Linked Lists
• Thus, we can link hop through the list starting at the
head and ending at the tail.
9
Singly Linked Lists
• Like an array
– A singly linked list keeps its elements in a certain order.
– This order is determined by the chain of next links going
from each node to its successor in the list.
• Unlike an array
– A singly linked list does not have a predetermined fixed size
and uses space proportional to the number of its elements.
– Likewise, we do not keep track of any index numbers for
the nodes in a linked list.
– So, we cannot tell just by examining a node if it is the
second, fifth, or twentieth node in the list – disadvantage.
10
Implementing a Singly Linked List
• Data holds the data in the Node while Next (the link) holds the
address to the next Node in the list.
• How to define a linked list node:
Node in a Singly // Define a class for a singly linked list
Linked List node
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = NULL;
}
};
11
Insertion at the beginning of a Singly Linked List
• When using a singly linked list, we can easily insert an element at the
head of the list.
• The main idea is:
– create a new node,
– set its next link to refer to the same object as head,
– and then set head to point to the new node.
12
Creating a Singly Linked List
// Class to represent the singly linked list
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
13
Creating a Singly Linked List
// Function to display the linked list
void display() {
Node* current = head;
while (current != NULL) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "NULL" << std::endl;
}
};
int main() {
LinkedList myList;
myList.insertAtBeginning(3);
myList.insertAtBeginning(2);
myList.insertAtBeginning(1);
15
Insertion at the tail of a Singly Linked List
Inserting a new node at the end of a singly linked list, given only a pointer to the head
(no tail).
This method works also if the list is empty.
// Function to insert a new element at the end of the list
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
} else {
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
16
Deleting the head of a Singly Linked List
The reverse operation of inserting a new element at the head of a linked
list is to remove an element at the head.
17
Deleting the head of a Singly Linked List
18
Deleting the tail of a Singly Linked List
// Function to delete the tail (last element) of the list
void deleteTail() {
if (head == NULL) {
return; // Nothing to delete
} else if (head->next == NULL) {
delete head;
head = NULL;
} else {
Node* current = head;
while (current->next->next != NULL) {
current = current->next;
}
delete current->next;
current->next = NULL;
}
}
19
Deleting the tail of a Singly Linked List - Analysis
• The only way to access the tail node is to start from the
head of the list and search all the way through the list, but
such a sequence of link hopping operations could take a
long time.
20
Doubly Linked Lists
• It is time consuming to remove any node other than the head
in a singly linked list, since we do not have a quick way of
accessing the node in front of the one we want to remove.
previous = NULL;
}
};
Doubly Linked Lists
A doubly linked list with head and tail, marking the ends of
the list.
24
Insertion at the beginning of a Doubly Linked List
Note that this method works also on an empty list.
• Deleting the tail is also easy. Indeed, the previous links eliminate the
need to traverse the list to get to the node just before the tail.
27
Deleting the tail of a Doubly Linked List
Removing the node storing P:
• Set tail to temp->previous.
• delete tail→next.
• Set tail→next to NULL
28
Deleting the tail of a Doubly Linked List
// Function to delete the tail (last element) of the list
void deleteTail() {
if (tail != NULL) {
if (tail->previous != NULL) {
tail = tail->previous;
delete tail->next;
tail->next = NULL;
} else {
delete tail;
head = NULL;
tail = NULL;
}
}
}
29
Circular Singly Linked Lists
• A circular singly linked list has the same kind of nodes as a singly
linked list.
• Each node in a circular linked list has a next pointer and a data
element.
33