Linked List
Linked List
Node Class
class Node {
public:
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};
LinkedList Class
class LinkedList {
Node* head;
public:
LinkedList() : head(nullptr) {}
void insertAtHead(int data) {
Node* newNode = new Node(data);
newNode->next = head;
head = newNode;
}
void print() {
Node* temp = head;
while (temp) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
Main Function
int main() {
LinkedList list;
list.insertAtHead(4);
list.insertAtHead(3);
list.insertAtHead(2);
list.insertAtHead(1);
list.print();
return 0;
}
Class Definition
class node {
public:
int data;
node* next;
node(int val) {
data = val;
next = NULL;
}
};
class node {: Defines a class named node that will be used to create nodes in a
linked list.
public:: Access specifier that makes the following members accessible from outside
the class.
int data;: A variable to store the data of the node.
node* next;: A pointer to the next node in the linked list.
node(int val) {: A constructor for the node class that initializes a new node with a
given value.
data = val;: Sets the data of the node to the passed value val.
next = NULL;: Initializes the next pointer to NULL, indicating the end of the list or
that the node does not point to another node yet.
}: End of the constructor definition.
};: End of the node class definition.
Insert Functions
Insert at Head
Insert at Position
if (position == 0) {
insertAtHead(head, val);
return;
}
if (temp == NULL) {
cout << "Position out of bounds!" << endl;
delete n;
return;
}
n->next = temp->next;
temp->next = n;
}
Display Function
void display(node* head) {
node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
void display(node* head) {: Defines a function to print all nodes in the list.
node* temp = head;: Creates a temporary pointer temp to traverse the list.
while (temp != NULL) {: Loops through each node in the list.
cout << temp->data << " -> ";: Prints the data of the current node followed by
an arrow.
temp = temp->next;: Moves to the next node.
cout << "NULL" << endl;: Prints "NULL" to indicate the end of the list.
}: End of the function.
Search Function
bool search(node* head, int key) {
node* temp = head;
while (temp != NULL) {
if (temp->data == key) {
return true;
}
temp = temp->next;
}
return false;
}
bool search(node* head, int key) {: Defines a function to search for a node
with a specific key.
node* temp = head;: Creates a temporary pointer temp to traverse the list.
while (temp != NULL) {: Loops through each node in the list.
if (temp->data == key) {: Checks if the current node's data matches the key.
return true;: Returns true if the key is found.
temp = temp->next;: Moves to the next node.
return false;: Returns false if the key is not found in the list.
}: End of the function.
Main Function
int main() {
node* head = NULL;
insertAtTail(head, 1);
insertAtTail(head, 2);
insertAtTail(head, 3);
insertAtTail(head, 4);
insertAtTail(head, 5);
display(head);
insertAtHead(head, 4);
display(head);
return 0;
}
int main() {: Defines the main function, the entry point of the program.
node* head = NULL;: Initializes the head of the linked list to NULL (empty list).
insertAtTail(head, 1);: Inserts 1 at the end of the list.
insertAtTail(head, 2);: Inserts 2 at the end of the list.
insertAtTail(head, 3);: Inserts 3 at the end of the list.
insertAtTail(head, 4);: Inserts 4 at the end of the list.
insertAtTail(head, 5);: Inserts 5 at the end of the list.
display(head);: Displays the current state of the list.
insertAtHead(head, 4);: Inserts 4 at the head of the list.
display(head);: Displays the updated list.
insertAtPosition(head, 3, 10);: Inserts 10 at position 3 in the list.
display(head);: Displays the updated list.
insertAtPosition(head, 0, 20);: Inserts 20 at position 0 (head) in the list.
display(head);: Displays the updated list.
insertAtPosition(head, 10, 30);: Attempts to insert 30 at position 10, which is
out of bounds.
display(head);: Displays the list (unchanged after the out-of-bounds attempt).
cout << (search(head, 5) ? "Found" : "Not Found") << endl;: Searches for
the value 5 in the list and prints "Found" or "Not Found".
return 0;: Exits the program with a status code of 0, indicating successful
execution.
}: End of the main function.
Doubly LinkedList(all basic operation):
#include <iostream>
using namespace std;
class node {
public:
int data;
node* next;
node* prev;
node(int val) {
data = val;
next = NULL;
prev = NULL;
}
};
Insertions
void insertAtHead(node* &head, int val) {
node* n = new node(val);
n->next = head;
if (head != NULL) {
head->prev = n;
}
head = n;
cout << "Inserted " << val << " at head." << endl;
}
1. node* n = new node(val);: Creates a new node with the value val.
2. n->next = head;: Sets the new node's next pointer to the current head of the list.
3. if (head != NULL) { head->prev = n; }: Updates the previous head node's
prev pointer to the new node if the list is not empty.
4. head = n;: Updates the head pointer to the new node.
5. cout << "Inserted " << val << " at head." << endl;: Prints a message
indicating the insertion.
Display Function
void display(node* head) {
if (head == NULL) {
cout << "The list is empty." << endl;
return;
}
1. if (head == NULL) { cout << "The list is empty." << endl; return; }:
Checks if the list is empty and prints a message if it is.
2. node* temp = head;: Initializes temp to traverse the list.
3. cout << "List: ";: Prints a prefix message for the list contents.
4. while (temp != NULL) { cout << temp->data << " "; temp = temp-
>next; }: Prints each node's data and moves to the next node.
5. cout << endl;: Moves to the next line after displaying the list.
Deletion Functions
void deleteAtHead(node* &head) {
if (head == NULL) {
cout << "Nothing to delete at head (the list is empty)." << endl;
return;
}
if (head->next == NULL) {
deleteAtHead(head);
cout << "Deleted node at tail (which was also the head)." << endl;
return;
}
if (pos == 1) {
deleteAtHead(head);
cout << "Deleted node at position " << pos << " (head)." << endl;
return;
}
if (temp == NULL) {
cout << "Position " << pos << " is out of bounds." << endl;
return;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
delete temp;
cout << "Deleted node at position " << pos << "." << endl;
}
1. if (head == NULL) { cout << "Nothing to delete at position " << pos
<< " (the list is empty)." << endl; return; }: Checks if the list is empty
and prints a message if it is.
2. if (pos == 1) { deleteAtHead(head); ... return; }: If deleting at position
1, calls deleteAtHead.
3. node* temp = head; int count = 1;: Initializes temp for traversal and count for
position tracking.
4. while (temp != NULL && count != pos) { temp = temp->next; count++; }:
Traverses to the node at the specified position.
5. if (temp == NULL) { cout << "Position " << pos << " is out of
bounds." << endl; return; }: Checks if the position is valid.
6. if (temp->prev != NULL) { temp->prev->next = temp->next; }: Updates the
previous node’s next pointer.
7. if (temp->next != NULL) { temp->next->prev = temp->prev; }: Updates the
next node’s prev pointer.
8. delete temp;: Deletes the node at the specified position.
9. cout << "Deleted node at position " << pos << "." << endl;: Prints a
message indicating the deletion.
Main Function
int main() {
node* head = NULL;
insertAtTail(head, 1);
insertAtTail(head, 2);
insertAtTail(head, 3);
insertAtTail(head, 4);
display(head);
insertAtHead(head, 5);
display(head);
return 0;
}
1. node* head = NULL;: Initializes the head of the list as NULL (empty list).
2. insertAtTail(head, 1); ...: Inserts nodes into the list and displays the list after
each operation.
3. insertAtHead(head, 5); ...: Inserts a node at the head and displays the list.
4. insertAtPosition(head, 3, 6);: Inserts a node at position 3 and displays the list.
5. deleteAtTail(head);: Deletes the last node and displays the list.
6. deleteAtPosition(head, 5); deleteAtPosition(head, 2);: Deletes nodes at
specific positions and displays the list.
7. return 0;: Ends the main function and returns 0 to indicate successful completion.