Knowee
Questions
Features
Study Tools

C program to performs all operations on singly linked listWrite a program that uses functions to perform the following operations on singly linked listi) Creation ii) Insertion  iii) Deletion  iv) TraversalSample Test CasesTest Case 1:Expected Output:Singly·Linked·List·Example·-·All·OperationsOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·111Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·222Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·333Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·444Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·222·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·2Enter·position·of·the·element·for·deleteing·the·element·:·2Deleted·successfullyOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·4No·of·elements·in·the·linked·list·are·:·3Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·5

Question

C program to performs all operations on singly linked listWrite a program that uses functions to perform the following operations on singly linked listi) Creation ii) Insertion  iii) Deletion  iv) TraversalSample Test CasesTest Case 1:Expected Output:Singly·Linked·List·Example·-·All·OperationsOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·111Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·222Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·333Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·444Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·222·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·2Enter·position·of·the·element·for·deleteing·the·element·:·2Deleted·successfullyOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·4No·of·elements·in·the·linked·list·are·:·3Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·5

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C program that performs all the operations on a singly linked list:

#include<stdio.h>
#include<stdlib.h>

struct node {
    int data;
    struct node *next;
}*head;

void insert(int num) {
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if(head == NULL) {
        head=temp;
        head->next=NULL;
    } else {
        temp->next=head;
        head=temp;
    }
}

void delete(int num) {
    struct node *temp, *prev;
    temp=head;
    while(temp!=NULL) {
        if(temp->data==num) {
            if(temp==head) {
                head=temp->next;
                free(temp);
                return;
            } else {
                prev->next=temp->next;
                free(temp);
                return;
            }
        } else {
            prev=temp;
            temp= temp->next;
        }
    }
    printf("\n%d not found in the list\n", num);
}

void display() {
    struct node *temp;
    temp=head;
    while(temp!=NULL) {
        printf("%d ", temp->data);
        temp=temp->next;
    }
}

int count() {
    struct node *temp;
    int count = 0;
    temp=head;
    while(temp!=NULL) {
        count++;
        temp=temp->next;
    }
    return count;
}

int main() {
    int choice, num;
    head=NULL;
    while(1) {
        printf("\nOptions\n1 : Insert into linked list\n2 : Delete from linked list\n3 : Display linked list\n4 : Count linked list\n5 : Exit\nEnter your option : ");
        scanf("%d", &choice);
        switch(choice) {
            case 1: printf("\nEnter the number to insert : ");
                    scanf("%d", &num);
                    insert(num);
                    break;
            case 2: printf("\nEnter the number to delete : ");
                    scanf("%d", &num);
                    delete(num);
                    break;
            case 3: display();
                    break;
            case 4: printf("\nNo of elements in the linked list = %d", count());
                    break;
            case 5: exit(0);
        }
    }
    return 0;
}

This program creates a singly linked list and provides options to insert, delete, display, and count the elements in the list. The insert function inserts a new node at the beginning of the list, the delete function deletes a node with a given value, the display function prints all the elements in the list, and the count function returns the number of elements in the list.

This problem has been solved

Similar Questions

Write a program in C, to create a link list and perform operation such as inserting or deleting a node from list

Write a program in C, to create and display Single Linked List

Linked List Operations: Traverse, Insert and DeleteTraversal - access each element of the linked list.Insertion - adds a new element to the linked list.Deletion - removes the existing elements.Search - find a node in the linked list.Sort - sort the nodes of the linked list.

What additional operations can be performed on a Doubly Linked List compared to a Singly Linked List?a)Deleting at the end only.b)Searching for an element.c)Traversing in both directions.d)Inserting at the beginning only

Write a C program to reverse a single linked list recursively. Sample Test Cases Test case 1 No·of·nodes:·5 Data·for·node·1:·5 Data·for·node·2:·4 Data·for·node·3:·3 Data·for·node·4:·2 Data·for·node·5:·1 Original·linked·list:·5·->·4·->·3·->·2·->·1·->·Null⏎ Reversed·linked·list:·1·->·2·->·3·->·4·->·5·->·Null⏎ Test case 2 No·of·nodes:·7 Data·for·node·1:·1 Data·for·node·2:·2 Data·for·node·3:·3 Data·for·node·4:·4 Data·for·node·5:·3 Data·for·node·6:·2 Data·for·node·7:·1 Original·linked·list:·1·->·2·->·3·->·4·->·3·->·2·->·1·->·Null⏎ Reversed·linked·list:·1·->·2·->·3·->·4·->·3·->·2·->·1·->·Null⏎

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.