Tutorial 09 and 10

 ------------singlyLinkedList.cpp

#include <iostream>


// Node class to represent each element in the linked list

class Node 

{

public:

    int data; // Data stored in the node

    Node* next; // Pointer to the next node


    // Constructor to initialize the node with data and nullptr

    Node(int data) 

    {

        this->data = data;

        next = nullptr;

    }

};


// Linked list class

class LinkedList {

public:

    Node* head; // Pointer to the first node (head) of the list


public:

    // Constructor to initialize an empty linked list

    LinkedList() 

    {

        head = nullptr;

    }


    // Destructor to free memory allocated for nodes

    ~LinkedList() 

    {

        Node* current = head;

        while (current != nullptr) {

            Node* next = current->next;

            delete current;

            current = next;

        }

    }


    // Function to insert a new node at the beginning of the list

    void insertAtBeginning(int data)

     {

        Node* newNode = new Node(data);

        newNode->next = head;

        head = newNode;

    }


    // Function to insert a new node at the end of the list

    void insertAtEnd(int data)

     {

        Node* newNode = new Node(data);

        if (head == nullptr) {

            head = newNode;

            return;

        }

        Node* current = head;

        while (current->next != nullptr) {

            current = current->next;

        }

        current->next = newNode;

    }


    // Function to insert a new node after a given node

    void insertAfter(Node* prevNode, int data)

    {

        if (prevNode == nullptr) {

            std::cout << "Previous node cannot be null.\n";

            return;

        }


        Node* newNode = new Node(data);

        newNode->next = prevNode->next;

        prevNode->next = newNode;

    }


    // Function to delete the first occurrence of a node with given data

    void deleteNode(int data) {

        if (head == nullptr)

            return;


        if (head->data == data) {

            Node* temp = head;

            head = head->next;

            delete temp;

            return;

        }


        Node* current = head;

        while (current->next != nullptr) {

            if (current->next->data == data) {

                Node* temp = current->next;

                current->next = current->next->next;

                delete temp;

                return;

            }

            current = current->next;

        }

    }


    // Function to search for a node with given data

    bool search(int data) {

        Node* current = head;

        while (current != nullptr)

         {

            if (current->data == data) {

                return true; // Node found

            }

            current = current->next;

        }

        return false; // Node not found

    }


    // Function to display the elements of the linked list

    void display() {

        Node* current = head;

        std::cout << "Linked List Elements: ";

        while (current != nullptr) {

            std::cout << current->data << " ";

            current = current->next;

        }

        std::cout << std::endl;

    }

};


int main() {

    LinkedList list;

    int choice, data, prevData;


    do {

        std::cout << "\nChoose operation:\n";

        std::cout << "1. Insert at beginning\n";

        std::cout << "2. Insert at end\n";

        std::cout << "3. Insert after a node\n";

        std::cout << "4. Delete node\n";

        std::cout << "5. Search node\n";

        std::cout << "6. Display list\n";

        std::cout << "0. Exit\n";


        std::cout << "Enter your choice: ";

        std::cin >> choice;


        switch (choice) {

            case 1:

                std::cout << "Enter data to insert at the beginning: ";

                std::cin >> data;

                list.insertAtBeginning(data);

                break;

            case 2:

                std::cout << "Enter data to insert at the end: ";

                std::cin >> data;

                list.insertAtEnd(data);

                break;

            case 3:

                std::cout << "Enter data of the node after which you want to insert: ";

                std::cin >> prevData;

                std::cout << "Enter data to insert: ";

                std::cin >> data;

                if (list.search(prevData)) {

                    Node* prevNode = list.head;

                    while (prevNode != nullptr && prevNode->data != prevData) {

                        prevNode = prevNode->next;

                    }

                    list.insertAfter(prevNode, data);

                } else {

                    std::cout << "Node with data " << prevData << " not found in the list.\n";

                }

                break;

            case 4:

                std::cout << "Enter data to delete: ";

                std::cin >> data;

                list.deleteNode(data);

                break;

            case 5:

                std::cout << "Enter data to search for: ";

                std::cin >> data;

                if (list.search(data)) {

                    std::cout << "Node with data " << data << " found in the list.\n";

                } else {

                    std::cout << "Node with data " << data << " not found in the list.\n";

                }

                break;

            case 6:

                list.display();

                break;

            case 0:

                std::cout << "Exiting...\n";

                break;

            default:

                std::cout << "Invalid choice. Please try again.\n";

        }

    } while (choice != 0);


    return 0;

}


1 comment:

Fell free to write your query in comment. Your Comments will be fully encouraged.