A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.
Operations to be performed:
- createList(): To create the list with the ānā number of nodes initially as defined by the user.
- traverse(): To see the contents of the linked list, it is necessary to traverse the given linked list. The given traverse() function traverses and prints the content of the linked list.
- insertAtFront(): This function simply inserts an element at the front/beginning of the linked list.
- insertAtEnd(): This function inserts an element at the end of the linked list.
- insertAtPosition(): This function inserts an element at a specified position in the linked list.
- deleteFirst(): This function simply deletes an element from the front/beginning of the linked list.
- deleteEnd(): This function simply deletes an element from the end of the linked list.
- deletePosition(): This function deletes an element from a specified position in the linked list.
- maximum(): This function finds the maximum element in a linked list.
- mean(): This function finds the mean of the elements in a linked list.
- sort(): This function sorts the given linked list in ascending order.
- reverseLL(): This function reverses the given linked list.
- display(): This function displays the linked list.
- search(): This function searches for an element user wants to search.
Below is the implementation of the above operations:
C
// C program for the all operations in // the Singly Linked List Ā
#include <stdio.h> #include <stdlib.h> Ā
// Linked List Node struct node { Ā Ā Ā Ā int info; Ā Ā Ā Ā struct node* link; }; struct node* start = NULL; Ā
// Function to create list with n nodes initially void createList() { Ā Ā Ā Ā if (start == NULL) { Ā Ā Ā Ā Ā Ā Ā Ā int n; Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nEnter the number of nodes: " ); Ā Ā Ā Ā Ā Ā Ā Ā scanf ( "%d" , &n); Ā Ā Ā Ā Ā Ā Ā Ā if (n != 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int data; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā struct node* newnode; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā struct node* temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newnode = malloc ( sizeof ( struct node)); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā start = newnode; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nEnter number to" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " be inserted : " ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā scanf ( "%d" , &data); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā start->info = data; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 2; i <= n; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newnode = malloc ( sizeof ( struct node)); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp->link = newnode; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nEnter number to" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " be inserted : " ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā scanf ( "%d" , &data); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā newnode->info = data; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nThe list is created\n" ); Ā Ā Ā Ā } Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nThe list is already created\n" ); } Ā
// Function to traverse the linked list void traverse() { Ā Ā Ā Ā struct node* temp; Ā
Ā Ā Ā Ā // List is empty Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nList is empty\n" ); Ā
Ā Ā Ā Ā // Else print the LL Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā Ā Ā Ā Ā Ā Ā Ā while (temp != NULL) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "Data = %d\n" , temp->info); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Function to insert at the front // of the linked list void insertAtFront() { Ā Ā Ā Ā int data; Ā Ā Ā Ā struct node* temp; Ā Ā Ā Ā temp = malloc ( sizeof ( struct node)); Ā Ā Ā Ā printf ( "\nEnter number to" Ā Ā Ā Ā Ā Ā Ā Ā " be inserted : " ); Ā Ā Ā Ā scanf ( "%d" , &data); Ā Ā Ā Ā temp->info = data; Ā
Ā Ā Ā Ā // Pointer of temp will be Ā Ā Ā Ā // assigned to start Ā Ā Ā Ā temp->link = start; Ā Ā Ā Ā start = temp; } Ā
// Function to insert at the end of // the linked list void insertAtEnd() { Ā Ā Ā Ā int data; Ā Ā Ā Ā struct node *temp, *head; Ā Ā Ā Ā temp = malloc ( sizeof ( struct node)); Ā
Ā Ā Ā Ā // Enter the number Ā Ā Ā Ā printf ( "\nEnter number to" Ā Ā Ā Ā Ā Ā Ā Ā " be inserted : " ); Ā Ā Ā Ā scanf ( "%d" , &data); Ā
Ā Ā Ā Ā // Changes links Ā Ā Ā Ā temp->link = 0; Ā Ā Ā Ā temp->info = data; Ā Ā Ā Ā head = start; Ā Ā Ā Ā while (head->link != NULL) { Ā Ā Ā Ā Ā Ā Ā Ā head = head->link; Ā Ā Ā Ā } Ā Ā Ā Ā head->link = temp; } Ā
// Function to insert at any specified // position in the linked list void insertAtPosition() { Ā Ā Ā Ā struct node *temp, *newnode; Ā Ā Ā Ā int pos, data, i = 1; Ā Ā Ā Ā newnode = malloc ( sizeof ( struct node)); Ā
Ā Ā Ā Ā // Enter the position and data Ā Ā Ā Ā printf ( "\nEnter position and data :" ); Ā Ā Ā Ā scanf ( "%d %d" , &pos, &data); Ā
Ā Ā Ā Ā // Change Links Ā Ā Ā Ā temp = start; Ā Ā Ā Ā newnode->info = data; Ā Ā Ā Ā newnode->link = 0; Ā Ā Ā Ā while (i < pos - 1) { Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā i++; Ā Ā Ā Ā } Ā Ā Ā Ā newnode->link = temp->link; Ā Ā Ā Ā temp->link = newnode; } Ā
// Function to delete from the front // of the linked list void deleteFirst() { Ā Ā Ā Ā struct node* temp; Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nList is empty\n" ); Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā Ā Ā Ā Ā Ā Ā Ā start = start->link; Ā Ā Ā Ā Ā Ā Ā Ā free (temp); Ā Ā Ā Ā } } Ā
// Function to delete from the end // of the linked list void deleteEnd() { Ā Ā Ā Ā struct node *temp, *prevnode; Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nList is Empty\n" ); Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā Ā Ā Ā Ā Ā Ā Ā while (temp->link != 0) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prevnode = temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā free (temp); Ā Ā Ā Ā Ā Ā Ā Ā prevnode->link = 0; Ā Ā Ā Ā } } Ā
// Function to delete from any specified // position from the linked list void deletePosition() { Ā Ā Ā Ā struct node *temp, *position; Ā Ā Ā Ā int i = 1, pos; Ā
Ā Ā Ā Ā // If LL is empty Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nList is empty\n" ); Ā
Ā Ā Ā Ā // Otherwise Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nEnter index : " ); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Position to be deleted Ā Ā Ā Ā Ā Ā Ā Ā scanf ( "%d" , &pos); Ā Ā Ā Ā Ā Ā Ā Ā position = malloc ( sizeof ( struct node)); Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse till position Ā Ā Ā Ā Ā Ā Ā Ā while (i < pos - 1) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Change Links Ā Ā Ā Ā Ā Ā Ā Ā position = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā temp->link = position->link; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Free memory Ā Ā Ā Ā Ā Ā Ā Ā free (position); Ā Ā Ā Ā } } Ā
// Function to find the maximum element // in the linked list void maximum() { Ā Ā Ā Ā int a[10]; Ā Ā Ā Ā int i; Ā Ā Ā Ā struct node* temp; Ā
Ā Ā Ā Ā // If LL is empty Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nList is empty\n" ); Ā
Ā Ā Ā Ā // Otherwise Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā Ā Ā Ā Ā Ā Ā Ā int max = temp->info; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse LL and update the Ā Ā Ā Ā Ā Ā Ā Ā // maximum element Ā Ā Ā Ā Ā Ā Ā Ā while (temp != NULL) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the maximum Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (max < temp->info) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max = temp->info; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nMaximum number " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "is : %d " , Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max); Ā Ā Ā Ā } } Ā
// Function to find the mean of the // elements in the linked list void mean() { Ā Ā Ā Ā int a[10]; Ā Ā Ā Ā int i; Ā Ā Ā Ā struct node* temp; Ā
Ā Ā Ā Ā // If LL is empty Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nList is empty\n" ); Ā
Ā Ā Ā Ā // Otherwise Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the sum and count of Ā Ā Ā Ā Ā Ā Ā Ā // element in the LL Ā Ā Ā Ā Ā Ā Ā Ā int sum = 0, count = 0; Ā Ā Ā Ā Ā Ā Ā Ā float m; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the LL Ā Ā Ā Ā Ā Ā Ā Ā while (temp != NULL) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the sum Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum = sum + temp->info; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the mean Ā Ā Ā Ā Ā Ā Ā Ā m = sum / count; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Print the mean value Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nMean is %f " , m); Ā Ā Ā Ā } } Ā
// Function to sort the linked list // in ascending order void sort() { Ā Ā Ā Ā struct node* current = start; Ā Ā Ā Ā struct node* index = NULL; Ā Ā Ā Ā int temp; Ā
Ā Ā Ā Ā // If LL is empty Ā Ā Ā Ā if (start == NULL) { Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Else Ā Ā Ā Ā else { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the LL Ā Ā Ā Ā Ā Ā Ā Ā while (current != NULL) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index = current->link; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the LL nestedly Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // and find the minimum Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // element Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (index != NULL) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Swap with it the value Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // at current Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (current->info > index->info) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = current->info; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā current->info = index->info; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index->info = temp; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index = index->link; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the current Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā current = current->link; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Function to reverse the linked list void reverseLL() { Ā Ā Ā Ā struct node *t1, *t2, *temp; Ā Ā Ā Ā t1 = t2 = NULL; Ā
Ā Ā Ā Ā // If LL is empty Ā Ā Ā Ā if (start == NULL) Ā Ā Ā Ā Ā Ā Ā Ā printf ( "List is empty\n" ); Ā
Ā Ā Ā Ā // Else Ā Ā Ā Ā else { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the LL Ā Ā Ā Ā Ā Ā Ā Ā while (start != NULL) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // reversing of points Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t2 = start->link; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā start->link = t1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t1 = start; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā start = t2; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā start = t1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // New head Node Ā Ā Ā Ā Ā Ā Ā Ā temp = start; Ā
Ā Ā Ā Ā Ā Ā Ā Ā printf ( "Reversed linked " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "list is : " ); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Print the LL Ā Ā Ā Ā Ā Ā Ā Ā while (temp != NULL) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "%d " , temp->info); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->link; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Function to search an element in linked list void search() { Ā Ā Ā Ā int found = -1; Ā Ā Ā Ā // creating node to traverse Ā Ā Ā Ā struct node* tr = start; Ā
Ā Ā Ā Ā // first checking if the list is empty or not Ā Ā Ā Ā if (start == NULL) { Ā Ā Ā Ā Ā Ā Ā Ā printf ( "Linked list is empty\n" ); Ā Ā Ā Ā } Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nEnter the element you want to search: " ); Ā Ā Ā Ā Ā Ā Ā Ā int key; Ā Ā Ā Ā Ā Ā Ā Ā scanf ( "%d" , &key); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // checking by traversing Ā Ā Ā Ā Ā Ā Ā Ā while (tr != NULL) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // checking for key Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (tr->info == key) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā found = 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // moving forward if not at this position Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā tr = tr->link; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // printing found or not Ā Ā Ā Ā Ā Ā Ā Ā if (found == 1) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Yes, %d is present in the linked list.\n" , Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā key); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "No, %d is not present in the linked " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "list.\n" , Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā key); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver Code int main() { Ā Ā Ā Ā createList(); Ā Ā Ā Ā int choice; Ā Ā Ā Ā while (1) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\n\t1 To see list\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t2 For insertion at" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " starting\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t3 For insertion at" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " end\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t4 For insertion at " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "any position\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t5 For deletion of " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "first element\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t6 For deletion of " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "last element\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t7 For deletion of " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "element at any position\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t8 To find maximum among" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " the elements\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t9 To find mean of " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "the elements\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t10 To sort element\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t11 To reverse the " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "linked list\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t12 Search an element in linked list\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\t13 To exit\n" ); Ā Ā Ā Ā Ā Ā Ā Ā printf ( "\nEnter Choice :\n" ); Ā Ā Ā Ā Ā Ā Ā Ā scanf ( "%d" , &choice); Ā
Ā Ā Ā Ā Ā Ā Ā Ā switch (choice) { Ā Ā Ā Ā Ā Ā Ā Ā case 1: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā traverse(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 2: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insertAtFront(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 3: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insertAtEnd(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 4: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insertAtPosition(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 5: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā deleteFirst(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 6: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā deleteEnd(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 7: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā deletePosition(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 8: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maximum(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 9: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mean(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 10: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sort(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 11: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā reverseLL(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 12: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā search(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā case 13: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā exit (1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break ; Ā Ā Ā Ā Ā Ā Ā Ā default : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā printf ( "Incorrect Choice\n" ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā return 0; } |
Output:
Menu:Ā
Insertion at the starting:Ā
Ā
Insertion at the end:Ā Ā
Insertion at specific position:Ā Ā
Print the Linked List:Ā Ā
Maximum among Linked List:Ā Ā
Sorting the Linked List:Ā Ā
Ā
Reverse the Linked List:Ā Ā
Delete the first and last element with choice 5 and 6:Ā Ā
Searching the element with choice 12:
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!