Complete Single Linked List
unknown
c_cpp
a month ago
7.2 kB
0
Indexable
Never
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *address; }Node; Node *create_linked_list(int num) { int i = 0; Node *nodes[num], *head = NULL; for(i = 0; i < num; i++) { nodes[i] = (Node *)malloc(sizeof(Node)); printf("\nEnter the data to be stored in Node %d: ",i+1); scanf("%d",&(nodes[i]->data)); } head = nodes[0]; for(i = 0; i < num; i++) { if(i == num-1) { nodes[i]->address = NULL; } else { nodes[i]->address = nodes[i+1]; } } return(head); } void display_nodes(Node *head) { if(head == NULL) { printf("\nLinked List is EMPTY!!!\n"); return; } else { Node *temp = head; printf("\n"); while(temp != NULL) { printf("%d -> ",temp->data); temp = temp->address; } printf("The End\n"); } } int size(Node *head) { if(head == NULL) { printf("\nLinked List is EMPTY!!!\n"); return(0); } else { int count = 0; Node *temp = head; while (temp != NULL) { count++; temp = temp->address; } return(count); } } int search(Node *head, int key) { if(head == NULL) { printf("\nLinked List is EMPTY!!!\n"); return(-1); } else { Node *temp = head; while(temp != head) { if(temp->data == key) { return(1); } } } } void insertion_at_beginning(Node **head, int new_data) { Node *temp, *new_node = (Node *)malloc(sizeof(Node)); new_node->data = new_data; if(*head == NULL) { printf("\nLinked List is EMPTY so inserting %d as first Node/Element.\n",new_data); *head = new_node; new_node->address = NULL; return; } else { new_node->address = *head; *head = new_node; return; } } void insertion_at_specific_position(Node **head, int position, int new_data) { if(*head == NULL) { printf("\nLinked List is EMPTY!!!\n"); return; } if(position < 0 || position > size(*head)) { printf("\nPosition is Out Of Range!!!\n"); return; } else { Node *temp, *new_node = (Node *)malloc(sizeof(Node)); new_node->data = new_data; temp = *head; for(int i = 0; i < position; i++) { temp = temp->address; } new_node->address = temp->address; temp->address = new_node; } } void insertion_at_end(Node **head, int new_data) { if(*head == NULL) { printf("\nLinked List is EMPTY!!!\n"); return; } else { Node *temp, *new_node = (Node *)malloc(sizeof(Node)); new_node->data = new_data; new_node->address = NULL; temp = *head; while(temp->address != NULL) { temp = temp->address; } temp->address = new_node; } } void deletion(Node **head, int value) { if(*head == NULL) { printf("\nLinked List is EMPTY!!!\n"); return; } if(search(*head,value) == 0) { printf("\n%d is not present in the linked list!!!\n",value); return; } else { Node *temp = *head; if((*head)->data == value && (*head)->address == *head) { free(temp); *head = NULL; printf("\nValue %d is deleted!!!\n",value); } else { if((*head)->data == value) { *head = (*head)->address; free(temp); printf("\nValue %d is deleted!!!\n",value); } else { Node *previous_node = temp; while(temp->address != NULL && temp->data != value) { previous_node = temp; temp = temp->address; } if(temp->data == value) { previous_node->address = temp->address; free(temp); printf("\nValue %d is deleted!!!\n",value); return; } } } } } void deallocation(Node **head) { Node *current = *head; while (current != NULL) { Node *temp = current; current = current->address; free(temp); } *head = NULL; return; } void main() { int num, size_res, search_res, data, position, user_selection; printf("\nEnter the no.of required nodes: "); scanf("%d",&num); Node *head = create_linked_list(num); label: printf("\nPlease Select One Of The Following Operations:\n1 - Display Linked List\n2 - Insertion At Beginning\n3 - Insertion At Specific Position\n4 - Insertion At Ending\n5 - Searching for an Element/Node\n6 - Size of the Linked List\n7 - Deletion\n8 - Exit\n"); scanf("%d",&user_selection); switch(user_selection) { case 1: display_nodes(head); goto label; break; case 2: printf("\nEnter the data to be inserted at the beginning: "); scanf("%d",&data); insertion_at_beginning(&head,data); display_nodes(head); goto label; break; case 3: printf("\nEnter the position after which you want to add a node: "); scanf("%d",&position); printf("\nEnter the data to be inserted at %d position: ",position); scanf("%d",&data); insertion_at_specific_position(&head,position-1,data); display_nodes(head); goto label; break; case 4: printf("\nEnter the data to be inserted at the ending: "); scanf("%d",&data); insertion_at_end(&head,data); display_nodes(head); goto label; break; case 5: printf("Enter the Value go be searched: "); scanf("%d",&data); if(search(head,data) != -1) { printf("\nElement is found!!!"); } else { printf("\nElement is Not Found!!!"); } goto label; break; case 6: printf("\nSize of Linked List: %d",size(head)); goto label; break; case 7: printf("\nEnter the VALUE to be deleted: "); scanf("%d",&data); deletion(&head,data); display_nodes(head); goto label; break; case 8: deallocation(&head); printf("\nThank You!!!\n"); exit(0); default: printf("\nPlease Select A Valid Option."); goto label; break; } }