Skip to main content

5. Singly Linked List - Menu driven program

 


#include <stdio.h>

#include <stdlib.h>

#include <string.h>
struct MyLinkedList
{
    int data;
    struct MyLinkedList *next;
};
struct MyLinkedList *head = NULL;
struct MyLinkedList *tail = NULL;
void create()
{
    struct MyLinkedList *new = (struct MyLinkedList *)malloc(sizeof(struct MyLinkedList));
    if (new == NULL)
        printf("Creation of Linked List is not possible...");
    else if (head == NULL)
    {
        printf("Enter value: ");
        scanf("%d", &new->data);
        new->next = NULL;
        head = tail = new;
    }
    else
    {
        printf("Enter value: ");
        scanf("%d", &new->data);
        new->next = NULL;
        tail->next = new;
        tail = new;
    }
}
int count()
{
    int j = 0;
    struct MyLinkedList *ptr = head;
    while (ptr != NULL)
    {
        j++;
        ptr = ptr->next;
    }
    return j;
}
void insertAtBegin()
{
    struct MyLinkedList *new = (struct MyLinkedList *)malloc(sizeof(struct MyLinkedList));
    if (new == NULL)
        printf("Insertion not possible...");
    else
    {
        printf("Enter value to be inserted: ");
        scanf("%d", &new->data);
        new->next = head;
        head = new;
        printf("Insertion successful");
    }
}
void insertAtEnd()
{
    struct MyLinkedList *new = (struct MyLinkedList *)malloc(sizeof(struct MyLinkedList));
    if (new == NULL)
        printf("Insertion not possible...");
    else
    {
        printf("Enter value to be inserted: ");
        scanf("%d", &new->data);
        new->next = NULL;
        tail->next = new;
        tail = new;
        printf("Insertion successful");
    }
}
void insertAtAny()
{
    int pos, i = 1;
    struct MyLinkedList *new = (struct MyLinkedList *)malloc(sizeof(struct MyLinkedList));
    struct MyLinkedList *ptr = head;
    if (new == NULL)
        printf("Insertion not possible");
    else
    {
        printf("Enter the position you want to insert: ");
        scanf("%d", &pos);
        if (pos == 1)
            insertAtBegin();
        else if (pos == count() + 1)
            insertAtEnd();
        else if (pos <= 0 || pos > count() + 1)
            printf("Insertion not possible...");
        else{
            while (ptr->next != NULL)
        {
            if (i == pos - 1)
            {
                printf("Enter the value to be inserted: ");
                scanf("%d", &new->data);
                new->next = ptr->next;
                ptr->next = new;
                printf("Insertion successful...");
                break;
            }
            i++;
            ptr = ptr->next;
        }
        }
    }
}
void deleteAtBegin()
{
    struct MyLinkedList *ptr = head;
    if (head == NULL)
        printf("Deletion not possible...");
    else if (head == tail)
    {
        printf("Deleted element: %d\nLinked list is now empty...", ptr->data);
        head = tail = NULL;
    }
    else
    {
        head = head->next;
        printf("Deleted element: %d", ptr->data);
    }
    free(ptr);
}
void deleteAtEnd()
{
    struct MyLinkedList *ptr = head;
    if (head == NULL)
        printf("Deletion not possible...");
    else if (head == tail)
    {
        printf("Deleted element: %d\nLinked list is now empty...", ptr->data);
        head = tail = NULL;
    }
    else
    {
        while (ptr->next != tail)
        {
            ptr = ptr->next;
        }
        tail = ptr;
        ptr = ptr->next;
        tail->next = NULL;
        printf("Deleted element: %d", ptr->data);
    }
    free(ptr);
}
void deleteAtAny()
{
    int i = 1, pos;
    struct MyLinkedList *ptr = head;
    struct MyLinkedList *prev;
    printf("Enter the position to be deleted: ");
    scanf("%d", &pos);
    if (pos == 1)
        deleteAtBegin();
    else if (pos == count())
        deleteAtEnd();
    else if (pos <= 0 ||  pos > count())
        printf("Deletion not possible...");
    else
    {
        while (ptr->next != NULL)
        {
            prev = ptr;
            ptr = ptr->next;
            i++;
            if (i == pos)
            {
                prev->next = ptr->next;
                printf("Deleted element: %d", ptr->data);
                free(ptr);
                break;
            }
        }
    }
}
void display()
{
    struct MyLinkedList *ptr = head;
    if (head == NULL)
        printf("Linked list is empty, so there is no element to show...");
    else
    {
        printf("Printing the elements of Linked list...\n");
        while (ptr != NULL)
        {
            printf("%d ", ptr->data);
            ptr = ptr->next;
        }
    }
}
void search()
{
    int item, i = 1, k = 0;
    struct MyLinkedList *ptr = head;
    printf("Enter the element to be searched: ");
    scanf("%d", &item);
    while (ptr != NULL)
    {
        if ((*ptr).data == item)
        {
            printf("%d is founded at position %d\n", item, i);
            k++;
        }
        ptr = (*ptr).next;
        i++;
    }
    if (k == 0)
        printf("%d not founded...", item);
}
void star()
{
    int j = 0;
    printf("\n\n\t\t");
    while (j <= 5)
    {
        printf("===============");
        j++;
    }
    printf("\n\n");
}

void main()
{
    int choices, n;
    char choice[10];
    star();
    printf("\t\t\t\t *****CREATING A LINKED LIST*****\n\n\t\t\tEnter the initial number of elements in linked list: ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        create();
    star();
    display();
    while (1)
    {
        star();
        printf("\n\t\t\t\tOPERATIONS IN LINKED LIST\n\n\t\t\tPress '1' to INSERT element in the linked list\n\t\t\tPress '2' to DELETE the elements from the linked list\n\t\t\t
        Press '3' to DISPLAY the linked list\n\t\t\tPress '4' to SEARCH an element in the linked list\n\t\t\tPress 'any other key' to EXIT\n\t\t\tEnter your choice: ");
        scanf("%d", &choices);
        star();
        if (choices == 1)
        {
            fflush(stdin);
            printf("\t\t\tPress 'B' to insert at BEGIN\n\t\t\tPress 'E' to insert at END\n\t\t\t
            Press 'A' to insert at ANY SPECIFIC POSITION\n\t\t\tEnter your choice: ");
            scanf("%c", &choice);
            if (strcmp(choice, "B") == 0)
            {
                insertAtBegin();
                star();
            }
            else if (strcmp(choice, "E") == 0)
            {
                insertAtEnd();
                star;
            }
            else if (strcmp(choice, "A") == 0)
            {
                insertAtAny();
                star;
            }
            else
            {
                printf("\t\t\tYour choice is not valid...\n");
                star();
            }
        }
        else if (choices == 2)
        {
            fflush(stdin);
            printf("\t\t\tPress 'B' to delete at BEGIN\n\t\t\tPress 'E' to delete at END\n\t\t\t
            Press 'A' to delete at ANY SPECIFIC POSITION\n\t\t\tEnter your choice: ");
            scanf("%c", &choice);
            if (strcmp(choice, "B") == 0)
            {
                deleteAtBegin();
                star();
            }
            else if (strcmp(choice, "E") == 0)
            {
                deleteAtEnd();
                star;
            }
            else if (strcmp(choice, "A") == 0)
            {
                deleteAtAny();
                star;
            }
            else
            {
                printf("\t\t\tYour choice is not valid...\n");
                star();
            }
        }
        else if (choices == 3)
        {
            display();
            star();
        }
        else if (choices == 4)
        {
            search();
            star();
        }
        else
        {
            printf("Exiting...");
            star();
            exit(0);
        }
    }
}

Output :

========================================================================================== *****CREATING A LINKED LIST***** Enter the initial number of elements in linked list: 3 Enter value: 1 Enter value: 2 Enter value: 3 ========================================================================================== Printing the elements of Linked list... 1 2 3 ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 1 ========================================================================================== Press 'B' to insert at BEGIN Press 'E' to insert at END Press 'A' to insert at ANY SPECIFIC POSITION Enter your choice: B Enter value to be inserted: 0 Insertion successful ========================================================================================== ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 1 ========================================================================================== Press 'B' to insert at BEGIN Press 'E' to insert at END Press 'A' to insert at ANY SPECIFIC POSITION Enter your choice: E Enter value to be inserted: 4 Insertion successful ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 1 ========================================================================================== Press 'B' to insert at BEGIN Press 'E' to insert at END Press 'A' to insert at ANY SPECIFIC POSITION Enter your choice: A Enter the position you want to insert: 3 Enter the value to be inserted: 6 Insertion successful... ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 3 ========================================================================================== Printing the elements of Linked list... 0 1 6 2 3 4 ========================================================================================== ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 2 ========================================================================================== Press 'B' to delete at BEGIN Press 'E' to delete at END Press 'A' to delete at ANY SPECIFIC POSITION Enter your choice: B Deleted element: 0 ========================================================================================== ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 2 ========================================================================================== Press 'B' to delete at BEGIN Press 'E' to delete at END Press 'A' to delete at ANY SPECIFIC POSITION Enter your choice: E Deleted element: 4 ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 2 ========================================================================================== Press 'B' to delete at BEGIN Press 'E' to delete at END Press 'A' to delete at ANY SPECIFIC POSITION Enter your choice: A Enter the position to be deleted: 2 Deleted element: 6 ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 3 ========================================================================================== Printing the elements of Linked list... 1 2 3 ========================================================================================== ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 4 ========================================================================================== Enter the element to be searched: 3 3 is founded at position 3 ========================================================================================== ========================================================================================== OPERATIONS IN LINKED LIST Press '1' to INSERT element in the linked list Press '2' to DELETE the elements from the linked list Press '3' to DISPLAY the linked list Press '4' to SEARCH an element in the linked list Press 'any other key' to EXIT Enter your choice: 5 ========================================================================================== Exiting... ==========================================================================================

Comments