Introduction
This program demonstrates a basic implementation of a singly linked list in C++. The linked list includes operations to insert elements at the end, delete specific elements, reverse the list, and display the elements in the list. The structure of a linked list is defined by nodes, where each node points to the next, forming a chain-like sequence. This implementation uses a Node
structure and defines a series of functions to perform these fundamental operations.
Here's a C++ program that demonstrates how to create a singly linked list and implement basic operations like inserting, deleting, and reversing the list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// Function to create a new node
Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
// Function to insert a node at the end of the linked list
void insert(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to delete a node by value
void deleteNode(Node*& head, int data) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
if (head->data == data) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != data) {
temp = temp->next;
}
if (temp->next == nullptr) {
cout << "Node not found." << endl;
return;
}
Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
delete nodeToDelete;
}
// Function to reverse the linked list
void reverseList(Node*& head) {
Node* prev = nullptr;
Node* curr = head;
Node* next = nullptr;
while (curr != nullptr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
// Function to display the linked list
void display(Node* head) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "nullptr" << endl;
}
// Main function to demonstrate the operations
int main() {
Node* head = nullptr;
// Insert elements
insert(head, 10);
insert(head, 20);
insert(head, 30);
insert(head, 40);
cout << "Linked List: ";
display(head);
// Delete an element
cout << "Deleting 20 from the list." << endl;
deleteNode(head, 20);
cout << "Linked List after deletion: ";
display(head);
// Reverse the linked list
cout << "Reversing the list." << endl;
reverseList(head);
cout << "Linked List after reversal: ";
display(head);
return 0;
}
Explanation of the Program
createNode(int data)
: Creates a new node with the given data.insert(Node*& head, int data)
: Inserts a new node with the specified data at the end of the linked list.deleteNode(Node*& head, int data)
: Deletes the first occurrence of a node with the given data.reverseList(Node*& head)
: Reverses the entire linked list.display(Node* head)
: Displays all elements of the linked list.
Output
Linked List: 10 -> 20 -> 30 -> 40 -> nullptr
Deleting 20 from the list.
Linked List after deletion: 10 -> 30 -> 40 -> nullptr
Reversing the list.
Linked List after reversal: 40 -> 30 -> 10 -> nullptr
This code provides a basic, functional example of a singly linked list with the operations you specified.
Conclusion
This program provides a straightforward approach to implementing and managing a linked list with key operations. By using simple functions to insert, delete, and reverse nodes, it covers the essential use cases of linked list manipulation. This implementation can serve as a foundation for more complex data structures and is a helpful demonstration of dynamic memory management and pointer manipulation in C++.