Singly Linked List in Data Structure using C++

 

Introduction

A Singly Linked List (SLL) is a dynamic data structure where each node consists of two parts:

  1. Data – Stores the actual value.
  2. Pointer – Points to the next node in the list.

Unlike arrays, a linked list provides efficient insertion and deletion operations without requiring contiguous memory allocation.

This article presents a C++ program to implement a singly linked list, featuring insertion, deletion, and display functionalities.

//Program for Student Details

#include<iostream>

using namespace std;


struct Node {

    int rno;

    string name;

    Node *next;

} *head, *temp;


class sll {

public:

    sll() {

        head = NULL;

        temp = head;

    }


    // Function to insert a new node

    void insert() {

        Node *newnode = new Node;

        cout << "Enter Roll number and Name to Insert: ";

        cin >> newnode->rno;

        cin >> newnode->name;

        newnode->next = NULL;


        if (head == NULL) {

            head = newnode;

            temp = head;

        } else {

            temp->next = newnode;

            temp = newnode;

        }

    }


    // Function to delete a node by name

    void del() {

        if (head == NULL) {

            cout << "List is empty\n";

            return;

        }


        string stuname;

        cout << "Enter Name to delete: ";

        cin >> stuname;


        if (head != NULL && head->name == stuname) {

            cout << "The deleted element is " << head->name << endl;

            head = head->next;

            return;

        }


        Node *current = head;

        Node *prev = NULL;


        while (current != NULL) {

            if (current->name == stuname) {

                prev->next = current->next;

                cout << "Deleted: " << current->name << endl;

                delete current;

                return;

            }

            prev = current;

            current = current->next;

        }

        cout << "No item found\n";

    }


    // Function to display the linked list

    void display() {

        Node *current = head;

        if (current == NULL) {

            cout << "SLL is empty\n";

            return;

        }


        while (current != NULL) {

            cout << "|" << current->rno << ", " << current->name << "|| ---> ";

            current = current->next;

        }

        cout << "NULL\n";

    }

};


int main() {

    sll s;

    int ch;

    while (1) {

        cout << "\n1. Insert\n";

        cout << "2. Delete\n";

        cout << "3. Display\n";

        cout << "4. Exit\n";

        cout << "Enter Choice: ";

        cin >> ch;


        switch (ch) {

            case 1: s.insert(); break;

            case 2: s.del(); break;

            case 3: s.display(); break;

            case 4: exit(0);

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

        }

    }

}


Output:
1. Insert
2. Delete
3. Display
4. Exit
Enter Choice: 1
Enter Roll number and Name to Insert: 101 Tejas

1. Insert
2. Delete
3. Display
4. Exit
Enter Choice: 1
Enter Roll number and Name to Insert: 102 Abin

1. Insert
2. Delete
3. Display
4. Exit
Enter Choice: 1
Enter Roll number and Name to Insert: 103 Maria

1. Insert
2. Delete
3. Display
4. Exit
Enter Choice: 3
|101, Tejas|| ---> |102, Abin|| ---> |103, Maria|| ---> NULL

1. Insert
2. Delete
3. Display
4. Exit
Enter Choice: 2
Enter Name to delete: Abin
Deleted: Abin

1. Insert
2. Delete
3. Display
4. Exit
Enter Choice: 3
|101, Tejas|| ---> |103, Maria|| ---> NULL

This example shows how insertion, deletion, and display functions work in the Singly Linked List (SLL) implementation. 

Comments

Popular Posts