c code for single linked list

  • clinked list
  • implement single linked list

    Write a menu-driven program to perform the following operations in a single linked list by using suitable user-defined functions for each case. a) Traversal of the list. b) Check if the list is empty. c) Insert a node at a certain position (at beginning/end/any position). d) Delete a node at a certain position (at the beginning/end/any position). e) Delete a node for the given key. f) Count the total number of nodes. g) Search for an element in the linked list. Verify & validate each function from the main method

    Code

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data; struct node *next; }; int init_count(struct node *r) { int count = 0; while (r) { r = r->next; count++; } return count; } void display(struct node *r) { printf("elements of list are : "); while (r) { printf("%d ---> ", r->data); r = r->next; } printf("NULL\n"); } struct node *create(struct node *head) { struct node *t, *p; int n; if (head == NULL) { t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", & t->data); t->next = NULL; head = t; } if (head != NULL) { do { t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", &t->data); t->next = NULL; p = head; while (p->next != NULL) { p = p->next; } p->next = t; printf("do you want to add more node press 1 for yes, 0 for no. "); scanf("%d", &n); } while (n); } return head; } struct node *insert_beginning(struct node *head) { struct node *t; t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: "); scanf("%d", &t->data); t->next = head; return t; } struct node *insert_end(struct node *head) { struct node *t, *p; t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", &t->data); p = head; while (p->next != NULL) { p = p->next; } p->next = t; t->next = NULL; return head; } struct node *insert_any_index(struct node *head) { int index; printf("enter position or index\n"); scanf("%d", &index); if (index > 0 && index < init_count(head)) { int i = 1; struct node *t, *p; t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", &t->data); p = head; while (i != index) { p = p->next; i++; } t->next = p->next; p->next = t; return head; } else { printf("INVALID INDEX\n"); } } struct node *delete_beginning(struct node *head) { struct node *t = head; head = head->next; free(t); return head; } struct node *delete_end(struct node *head) { struct node *t, *p; p = head; while (p->next->next != NULL) { p = p->next; } t = p->next; p->next = NULL; free(t); return head; } struct node *delete_any_index(struct node *head) { int index; printf("enter position or index\n"); scanf("%d", &index); struct node *p = head; struct node *q = head->next; printf("%d", index); if (index > 0 && index < init_count(head)) { for (int i = 1; i < index - 1; i++) { p = p->next; q = q->next; } p->next = q->next; free(q); return head; } else { printf("INVALID INDEX\n"); } } struct node *delete_key(struct node *head, int key) { struct node *p = head; struct node *q = head->next; p = head; while (q->data != key && q->next != NULL) { p = p->next; q = q->next; } if (q->data == key) { p->next = q->next; free(q); } return head; } int main() { int c, n, index, key, count, l, xd = 0; struct node *head = NULL, *p; head = create(head); printf("\n\n\n"); option: printf("\n\n*********OPTIONS**********\n"); printf("1) Traversal of the list.\n"); printf("2) Check if the list is empty.\n"); printf("3) Insert a node at the certain position.\n"); printf("4) Delete a node at the certain position.\n"); printf("5) Delete a node for the given key.\n"); printf("6) Count the total number of nodes.\n"); printf("7) Search for an element in the linked list.\n"); printf("8) exit.\n"); printf("\nenter your choice: "); scanf("%d", &c); switch (c) { case 1: display(head); goto option; break; case 2: if (head == NULL) { printf("empty linked list\n"); } else { printf("not empty\n"); } goto option; break; case 3: option1: printf("select from the list to perform\n"); printf("1)insert at beginning----> enter 1\n"); printf("2)insert at end----> enter 2\n"); printf("3)insert at any position except beginning and end----> enter 3\n"); printf("\n0-> to go back\n"); scanf("%d", &n); switch (n) { case 0: goto option; case 1: head = insert_beginning(head); goto option1; break; case 2: head = insert_end(head); goto option1; break; case 3: head = insert_any_index(head); goto option1; break; default: printf("INVALID / prev menu\n"); } goto option; break; case 4: option2: printf("select from the list to perform\n"); printf("1)delete at beginning----> enter 1\n"); printf("2)delete at end----> enter 2\n"); printf("3)delete at any position except beginning and end----> enter 3\n"); printf("\n0-> to go back\n"); scanf("%d", &n); switch (n) { case 0: goto option; case 1: head = delete_beginning(head); goto option2; break; case 2: head = delete_end(head); goto option2; break; case 3: head = delete_any_index(head); goto option2; break; default: printf("INVALID\n"); } goto option; break; case 5: printf("enetr the key\n"); scanf("%d", &key); head = delete_key(head, key); goto option; break; case 6: count = 0; p = head; while (p != NULL) { count++; p = p->next; } printf("number of nodes are : %d\n", count); goto option; break; case 7: printf("enetr the key\n"); scanf("%d", &key); p = head; while (p != NULL) { if (p->data == key) { printf("node found : %d\n", key); xd = 1; } p = p->next; } if (xd == 0) { printf("node not found\n"); } goto option; break; case 8: exit(0); default: printf("INVALID INPUT// exit\n"); goto option; } return 0; }