LIST ADT USING LINKED LIST

 #include <iostream>

using namespace std;


struct Node {

    int data;

    Node* next;

};


Node* head = nullptr;


void insert() {

    int value;

    cout << "\nEnter the element to insert: ";

    cin >> value;

    Node* newNode = new Node{value, nullptr};

    if (!head) {

        head = newNode;

    } else {

        newNode->next = head;

        head = newNode;

    }

}


void display() {

    if (!head) {

        cout << "\nList is empty\n";

        return;

    }

    Node* temp = head;

    cout << "\nList elements:\n";

    while (temp) {

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

        temp = temp->next;

    }

    cout << "\n";

}


int main() {

    int choice;

    cout << "\nMAIN MENU";

    cout << "\n1- INSERT\n2- DISPLAY\n3- EXIT\n";

    do {

        cout << "\nEnter your choice: ";

        cin >> choice;

        switch (choice) {

            case 1: insert(); break;

            case 2: display(); break;

            case 3: cout << "Exiting...\n"; break;

            default: cout << "Invalid choice\n";

        }

    } while (choice != 3);

    return 0;

}


Comments

Popular posts from this blog

CLASSES CREATION

SORTING PROGRAM