0% found this document useful (0 votes)
18 views8 pages

Single LinkedList

Uploaded by

chahmedk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Single LinkedList

Uploaded by

chahmedk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

All Operations of Single Linked List

Here is a complete C++ program that implements all essential operations of a **Singly Linked
List**, such as:

1. Insertion at the beginning, end, or after a specific node

2. Deletion from the beginning, end, or by value

3. Searching for an element

4. Displaying the list

C++ Code

#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* next;

// Constructor to initialize a new node

Node(int value) {

data = value;

next = nullptr;

};

class LinkedList {

private:

Node* head;

public:

// Constructor to initialize an empty list

LinkedList() {
head = nullptr;

// 1. Insert at the beginning

void insertAtBeginning(int value) {

Node* newNode = new Node(value);

newNode->next = head;

head = newNode;

// 2. Insert at the end

void insertAtEnd(int value) {

Node* newNode = new Node(value);

if (head == nullptr) {

head = newNode;

return;

Node* temp = head;

while (temp->next != nullptr) {

temp = temp->next;

temp->next = newNode;

// 3. Insert after a specific node value

void insertAfter(int key, int value) {

Node* temp = head;

while (temp != nullptr && temp->data != key) {

temp = temp->next;

}
if (temp == nullptr) {

cout << "Node with value " << key << " not found.\n";

return;

Node* newNode = new Node(value);

newNode->next = temp->next;

temp->next = newNode;

// 4. Delete from the beginning

void deleteFromBeginning() {

if (head == nullptr) {

cout << "List is empty.\n";

return;

Node* temp = head;

head = head->next;

delete temp;

// 5. Delete from the end

void deleteFromEnd() {

if (head == nullptr) {

cout << "List is empty.\n";

return;

if (head->next == nullptr) {

delete head;

head = nullptr;
return;

Node* temp = head;

while (temp->next->next != nullptr) {

temp = temp->next;

delete temp->next;

temp->next = nullptr;

// 6. Delete by value

void deleteByValue(int value) {

if (head == nullptr) {

cout << "List is empty.\n";

return;

if (head->data == value) {

Node* temp = head;

head = head->next;

delete temp;

return;

Node* temp = head;

while (temp->next != nullptr && temp->next->data != value) {

temp = temp->next;

if (temp->next == nullptr) {

cout << "Value " << value << " not found in the list.\n";
return;

Node* toDelete = temp->next;

temp->next = temp->next->next;

delete toDelete;

// 7. Search for a value

bool search(int value) {

Node* temp = head;

while (temp != nullptr) {

if (temp->data == value) return true;

temp = temp->next;

return false;

// 8. Display the list

void display() {

if (head == nullptr) {

cout << "List is empty.\n";

return;

Node* temp = head;

while (temp != nullptr) {

cout << temp->data << " -> ";

temp = temp->next;

}
cout << "NULL\n";

};

int main() {

LinkedList list;

list.insertAtBeginning(10);

list.insertAtEnd(20);

list.insertAtEnd(30);

list.insertAfter(20, 25);

cout << "List after insertion: ";

list.display();

list.deleteFromBeginning();

cout << "List after deleting from beginning: ";

list.display();

list.deleteFromEnd();

cout << "List after deleting from end: ";

list.display();

list.deleteByValue(25);

cout << "List after deleting value 25: ";

list.display();

if (list.search(20)) {

cout << "Element 20 found in the list.\n";


} else {

cout << "Element 20 not found in the list.\n";

return 0;

Explanation of the Code

1. **Class `Node`**:

- Represents a single node containing data and a pointer to the next node.

2. **Class `LinkedList`**:

- Manages all operations on the linked list.

3. **Insertion Methods**:

- **Beginning**: Adds a new node at the start.

- **End**: Traverses to the end and appends a new node.

- **After a given value**: Finds a node and inserts a new node after it.

4. **Deletion Methods**:

- **Beginning**: Removes the first node.

- **End**: Removes the last node.

- **By value**: Deletes a node with a specific value.

5. **Search Method**:

- Looks for an element in the list.

6. **Display Method**:

- Prints all elements in the list.

7. **Main Function**:

- Demonstrates the use of all operations.


Sample Output

List after insertion: 10 -> 20 -> 25 -> 30 -> NULL

List after deleting from beginning: 20 -> 25 -> 30 -> NULL

List after deleting from end: 20 -> 25 -> NULL

List after deleting value 25: 20 -> NULL

Element 20 found in the list.

You might also like