Single Linked List
Single Linked List
Introduction
• List of elements which are connected in sequence
to each other by a set of pointers.
• Commonly used linear data structure.
• Each element is known as a node.
• A node consists of two parts
– Data (value or values to be stored in a node).
– Pointer (links or references to other nodes in a
list).
• Types
– Singly, Doubly, and Circular.
Contd…
• Advantages
– Dynamic in nature, i.e. allocates memory when required.
– Insertion and deletion operations can be executed easily.
– Stacks and queues can be implemented easily.
– Reduces the access time.
– Efficient memory utilization, i.e no need to pre-allocate
memory.
• Disadvantages
– Wastage of memory as pointers require extra memory space.
– No random access; everything sequential.
– Reverse traversal is difficult.
– Memory space restriction as new node can only be created if
space is available in heap.
Operations
• Traversal (Searching, Displaying)
• Insertion
– At the beginning.
– At the end.
– At a specific location.
• Deletion
– At the beginning.
– At the end.
– At a specific location.
Singly Linked List
Introduction
• The most basic type of linked list.
• Two successive nodes are linked together as each node
contains address of the next node to be followed, i.e.
successor.
• A node may has multiple data fields but only single link
for the next node.
• Only forward sequential access is possible (or
unidirectional).
• Address of the first node is always stored in a reference
node known as front or head.
• The last node does not have any successor and has
reference to NULL.
0 -2017
Contd…
• Pictorial representation of a node
Pointer to the next
data next node in sequence
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = nullptr;
}
Contd…
head = temp;
• Link the new node temp in the existing empty list.
• Again dynamically allocate memory for a node and
initialize all members of a node.
*temp = (struct node *) malloc (sizeof(struct node));
scanf("%d",&num);
temp -> data = num;
temp -> next = NULL;
• Link the new node temp in the existing list at head.
temp -> next = head;
head = temp;
• This process is repeated for all the nodes. A node can
be inserted anywhere in the list.
03-02-2017 Linked List
Search an element in the list
• Algorithm search(head, num)
• Input: Pointer to the first node (head) and a value to search (num).
• Output: Appropriate message will be displayed.
1. If (head == NULL)
2. Print [List is Empty].
3. Return.
4. Initialize a node pointer (temp) with head.
5. while (temp is not NULL AND temp[data] is not equal to value)
6. temp = temp[next]
7. if (temp is NULL)
8. Print [Element not found].
9. Else
10. Print [Element found].
Search an element in the list
void display()
{
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
Insertion at beginning of the list
• Algorithm insertBeg(head, num)
• Input: Pointer to the first node (head) and a new
value to insert (num).
• Output: Node with value num gets inserted at the
first position.
1. Create a node pointer (temp).
2. temp[data] = num.
3. temp[next] = head.
4. head = temp.
Insertion at beginning of the list
if (current == nullptr) {
std::cout << "Value not found. Node not
inserted." << std::endl;
} else {
newNode->next = current->next;
current->next = newNode;
}
}
Insertion after a specific position in the list
void insertAtPosition(int value, int position) while (current != nullptr && currentPosition <
{ position - 1) {
if (position < 0) { current = current->next;
std::cout << "Invalid position." << currentPosition++;
std::endl; }
return;
} if (current == nullptr) {
std::cout << "Position out of range."
Node* newNode = new Node(value); << std::endl;
return;
if (position == 0) { }
newNode->next = head;
head = newNode; newNode->next = current->next;
} else { current->next = newNode;
Node* current = head; }
int currentPosition = 0; }
Delete from beginning of the list
• Algorithm deleteBeg(head)
• Input: Pointer to the first node (head).
• Output: The first node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. head = head[next]
6. Release the memory location pointed by temp.
7. end if
Delete from beginning of the list
void deleteAtBeginning()
{
if (head == nullptr) {
std::cout << "Linked list is empty.
Nothing to delete." << std::endl;
} else {
Node* temp = head;
head = head->next;
delete temp;
}
}
Delete from end of the list
• Algorithm deleteEnd(head)
• Input: Pointer to the first node (head).
• Output: The last node gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp[next] is not NULL)
6. initialize a node pointer (pre) with temp.
7. temp = temp[next]
8. if (temp == head)
9. head = NULL
10. else
11. pre[next] = NULL
12. Release the memory location pointed by temp.
13. end if
Delete from end of the list
void deleteAtEnd()
{
if (head == nullptr) {
std::cout << "Linked list is empty.
Nothing to delete." << std::endl;
} else if (head->next == nullptr) {
delete head;
head = nullptr;
} else {
Node* current = head;
while (current->next->next != nullptr) {
current = current->next;
}
delete current->next;
current->next = nullptr;
}
}
Delete a specific node from the list
• Algorithm deleteSpecific(head,num)
• Input: Pointer to the first node (head) and a value num to be deleted.
• Output: The node with value num gets deleted.
1. If (head == NULL)
2. Print [List is Empty].
3. Else
4. initialize a node pointer (temp) with head.
5. while (temp is not NULL AND temp[data] is not equal to value)
6. initialize a node pointer (pre) with temp.
7. temp = temp[next]
8. if (temp is NULL)
9. Print [Element not found].
10. Return.
Contd…
12. else if (temp == head)
13. head = head[next]
14. else if (temp[next] == NULL)
15. pre[next] = NULL
16. else
17. pre[next] = temp[next]
18. Release the memory location pointed by temp.
19. end if (line 8).
20. end if (line 1).
Delete a specific node from the list(C++)
void deleteNodeWithValue(int value) {
if (head == nullptr) {
std::cout << "List is Empty." << std::endl;
return;
}
if (temp == nullptr) {
std::cout << "Element not found." <<
Node* temp = head;
std::endl;
Node* pre = nullptr;
return;
}
// Traverse the linked list to find the node with the
specified value
if (temp == head) {
while (temp != nullptr && temp->data != value)
head = head->next;
{
} else if (temp->next == nullptr) {
pre = temp;
pre->next = nullptr;
temp = temp->next;
} else {
}
pre->next = temp->next;
}
delete temp;
}
Delete a specific positon from the list
void deleteAtPosition(int position) while (current != nullptr && currentPosition <
{ position - 1) {
if (position < 0) { current = current->next;
std::cout << "Invalid position." << currentPosition++;
std::endl; }
return;
} if (current == nullptr || current->next ==
nullptr) {
if (head == nullptr) { std::cout << "Position out of range." <<
std::cout << "Linked list is empty. std::endl;
Nothing to delete." << std::endl; return;
return; }
}
if (position == 0) { Node* temp = current->next;
Node* temp = head; current->next = current->next->next;
head = head->next; delete temp;
delete temp; }
} else { }
Node* current = head;
int currentPosition = 0;