Search This Blog

Sunday, December 14, 2025

SINGLE LINKED LIST BASIC CODE

 #include <stdio.h>

#include <stdlib.h>

struct Node

{

    int data;

    struct Node *next;

};

void insert(struct Node **head, int value)

{

    struct Node *newNode = (struct Node *)malloc

            (sizeof(struct Node));

    newNode->data = value;

    newNode->next = NULL;

    if (*head == NULL)

    {

        *head = newNode;

        return;

    }

    struct Node *temp = *head;

    while (temp->next != NULL)

    {

        temp = temp->next;

    }

    temp->next = newNode;

}

void display(struct Node *head)

{

    if (head == NULL)

    {

        printf("List is empty\n");

        return;

    }

    struct Node *temp = head;

    printf("Linked List: ");

    while (temp != NULL)

    {

        printf("%d -> ", temp->data);

        temp = temp->next;

    }

    printf("NULL\n");

}

int main()

{

    struct Node *head = NULL;

    int choice, value;

    while (1)

    {

        printf("\n1. Insert\n2. Display\n3. Exit

               \nEnter your choice: ");

        scanf("%d", &choice);

        switch (choice)

        {

            case 1:

                printf("Enter value to insert: ");

                scanf("%d", &value);

                insert(&head, value);

                break;

            case 2:

                display(head);

                break;

            case 3:

                exit(0);

            default:

                printf("Invalid choice\n");

        }

    }

    return 0;

}


No comments:

SINGLE LINKED LIST BASIC CODE

 #include <stdio.h> #include <stdlib.h> struct Node {     int data;     struct Node *next; }; void insert(struct Node **head, in...