Linked List Implementation
Linked List Implementation
1
Linked list
This content of this file is an extension of the previous file for linked
list
2
Linked list
Linked list operations
Inserting an element.
Insert a new node at the beginning of the list.
Insert a new node at the end of the list.
Insert a new node at a specified position in the list.
Traversing an element.
3
Linked list implementation
#include <iostream>
using namespace std;
// define a struct for a linked list node
template <typename T>
struct Node {
T data;
Node<T>* next;
};
// define a class for a linked list
template <typename T>
class LinkedList {
public:
Node<T>* head;
LinkedList() { head = NULL; } // constructor for an empty list
5
Linked list implementation
// This function inserts a new node with value "val" at the beginning of the linked list
template <typename T>
void LinkedList<T>::insertAtBeginning(T val) {
Node<T>* newNode = new Node<T>; // create a new node with type T
newNode->data = val; // assign the value "val" to the data field of the new node
newNode->next = head; // set the next pointer of the new node to the current head
head = newNode; // set the new node as the new head of the linked list
}
// This function inserts a new node with value "val" at the end of the linked list
template <typename T>
void LinkedList<T>::insertAtEnd(T val) {
Node<T>* newNode = new Node<T>; // create a new node with type T
newNode->data = val; // assign the value "val" to the data field of the new node
newNode->next = NULL; // set the next pointer of the new node to NULL
if (head == NULL) // if the linked list is empty, set the new node as the head
{ head = newNode; return; }
Node<T>* temp = head; // start from the head of the linked list
while (temp->next != NULL) // traverse to the end of the linked list
{ temp = temp->next; }
temp->next = newNode; // set the next pointer of the last node to the new node
6
}
Linked list implementation
// This function inserts a new node with value "val" at the specified position in the linked
list
template <typename T>
void LinkedList<T>::insertAtPosition(T val, int pos) {
Node<T>* newNode = new Node<T>; // Create a new node
newNode->data = val; // Assign the value to the new node
// Check if the new node should be inserted at the beginning of the list
if (pos == 1) {
newNode->next = head; // Set the new node's next pointer to the current head of the
list
head = newNode; // Update the head of the list to point to the new node
return;
} 7
Linked list implementation
// Traverse the list to find the position to insert the new node
Node<T>* temp = head;
for (int i = 2; i < pos; i++) {
if (temp->next != NULL) {
temp = temp->next; // Move to the next node in the list
} else {
cout << "Invalid position" << endl; // The position is out of range
delete newNode; // Delete the new node to prevent memory leaks
return;
}
}
8
Linked list implementation
// This function deletes the first node of the linked list
template <typename T>
void LinkedList<T>::deleteFromBeginning() {
if (head == NULL) {
// If the list is empty
cout << "List is empty" << endl;
return;
}
if (head->next == NULL) {
// If there is only one node in the list (head node)
delete head; // Delete the head node
head = NULL; // Set the head pointer to NULL
return;
}
Node<T>* temp = head;// Save the reference to the head node in a temporary pointer
head = head->next;// Update the head pointer to the next node
delete temp;// Delete the previous head node
} 9
Linked list implementation
//This function deletes the last node from the linked list.
template <typename T>
void LinkedList<T>::deleteFromEnd() {
// If the list is empty, there is nothing to delete
if (head == NULL) {
cout << "List is empty" << endl;
return; }
// If there is only one node in the list, delete it and set head to NULL
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
Node<T>* temp = head; // Find the second to last node in the list
while (temp->next->next != NULL) {
temp = temp->next;}
// Delete the last node and set the second to last node's next pointer to NULL
delete temp->next;
temp->next = NULL;
} 10
Linked list implementation
// This function deletes a node at a specified position in the list
template <typename T>
void LinkedList<T>::deleteFromPosition(int pos) {
// If the list is empty, print a message and return
if (head == NULL) {
cout << "List is empty" << endl;
return;
}
// If the position is 1, call deleteFromBeginning() function and return
if (pos == 1) {
Node<T>* temp = head;// Save the reference to the head node in a temporary pointer
head = head->next;// Update the head pointer to the next node
delete temp;// Delete the previous head node
return;
}
11
Linked list implementation
// Traverse the list until we reach the position (pos - 1)
Node<T>* temp = head;
for (int i = 2; i < pos; i++)
{
// If the next node is not NULL, move the temp pointer to the next node
if (temp->next != NULL)
{temp = temp->next; }
// If the next node is NULL, we have an invalid position, print a message and return
else
{ cout << "Invalid position" << endl; return; }
}
// We have reached the position (pos - 1), so we can delete the next node
Node<T>* nodeToDelete = temp->next;
// If the next node is NULL, we have an invalid position, print a message and return
if (nodeToDelete == NULL)
{ cout << "Invalid position" << endl; return; }
// Otherwise, update the next pointer of temp to point to the next of the node to be deleted
temp->next = nodeToDelete->next;
delete nodeToDelete; // Delete the node
} 12
Linked list implementation
// Delete the node with value 'val' from the linked list
template <typename T>
void LinkedList<T>::deleteNode(T val) {
if (head == NULL) // If the list is empty
{ cout << "List is empty" << endl;
return;}
if (head->data == val) {
// If the node to be deleted is the head node
Node<T>* temp = head;
head = head->next;
delete temp;
return;
}
13
Linked list implementation
Node<T>* temp = head;
// Traverse the linked list until the end or until the node to be deleted is
found
while (temp->next != NULL) {
if (temp->next->data == val) {
// If the node is found, remove it from the list
Node<T>* delNode = temp->next;
temp->next = delNode->next;
delete delNode;
return;
}
temp = temp->next;
}
cout << "Element not found in list" << endl;// If the node is not found in the
list
}
14
Linked list implementation
// This function searches for a specific value in the linked list and returns true if it is
found and false otherwise.
template <typename T>
bool LinkedList<T>::search(T key) {
Node<T>* temp = head;// Start by setting a temporary pointer to the head of the linked
list
while (temp != NULL) // Traverse the linked list to find the key value
{ if (temp->data == key) // If the key is found, return true
return true;
temp = temp->next; // Move the temporary pointer to the next node
}
return false; // If the key is not found, return false
}
15
Linked list implementation
// This function displays the elements of the linked list in the order they appear,
starting from the head.
template <typename T>
void LinkedList<T>::displayList() {
// Check if the list is empty
if (head == NULL) {
cout << "List is empty" << endl;
return;
}
// Initialize a temporary node pointer to traverse the list
Node<T>* temp = head;
// Loop through the list and print each element
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
// Print a new line character after printing all the elements
cout << endl;
}
16
Linked list implementation
#include <iostream>
using namespace std;
#include "linkedList.h"
int main() {
// create a new linked list
LinkedList<int> list;
// insert some elements into the list
list.insertAtBeginning(5);
list.insertAtEnd(10);
list.insertAtPosition(15, 3);
list.insertAtPosition(100, 2);
// display the contents of the list
list.displayList();
18