0% found this document useful (0 votes)
8 views18 pages

Linked List Implementation

Linked

Uploaded by

salmamoahmmed4
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)
8 views18 pages

Linked List Implementation

Linked

Uploaded by

salmamoahmmed4
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/ 18

Linked List

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.

 Deletion for an element.

 Delete the first node in the list.


 Delete the last node in the list.
 Delete the node at a specified position in the list.
 Delete a node with a specified value from the list
 Searching for an element.

 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

LinkedList(T val) // constructor for a list with a single element


{head = new Node<T>;
head->data = val;
head->next = NULL; }
4
Linked list implementation
void insertAtBeginning(T val);// function to insert a new node at the beginning of the
list
void insertAtEnd(T val); // function to insert a new node at the end of the list
void insertAtPosition(T val, int pos);// function to insert a new node at a specified
position in the list
void deleteFromBeginning(); // function to delete the first node in the list
void deleteFromEnd(); // function to delete the last node in the list
void deleteFromPosition(int pos);// function to delete the node at a specified
position in the list
void deleteNode(T val);// function to delete a node with a specified value from the
list
bool search(T key);// function to search the list for a node with a specified key
void displayList();// function to display the contents of the 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 list is empty


if (head == NULL) {
head = newNode; // If empty, make the new node the head of the list
return;
}

// 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;
}
}

// Insert the new node at the desired position


newNode->next = temp->next; // Set the new node's next pointer to the next node in the
list
temp->next = newNode; // Set the previous node's next pointer to the new node
}

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();

// delete some elements from the list


list.deleteFromPosition(2);
list.deleteFromEnd();
list.deleteNode(5);
17
Linked list implementation
// display the contents of the list
list.displayList();

// search for an element in the list


int key = 7;
if (list.search(key))
cout << key << " is found in the list" << endl;
else
cout << key << " is not found in the list" << endl;
return 0;
}

18

You might also like