How to write a C Program in Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list in C Programming Language ?
/*Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list */
#include<stdio.h>
#include<stdlib.h>
struct node* addofll(struct node*); //prototype of the new linked list block creation
void display(struct node*); // prototype to display the elements in the linked list
void search(struct node*); //prototype for searching to check if the element is in the list
struct node* insert(struct node*); //prototype for inserting a node into the linked list
struct node* delete(struct node*); //prototype for deletion of a node from the linked list
/*creating the struct for the block of linked list*/
struct node //struct node is required for linked list so as to get data and address
{
int data;
struct node* ptr; // struct node type pointer is required so as to hold the pointer of other struct node type for linking
};
int main()
{
struct node* head; //this pointer holds on the starting value of the data
int i,b;
head=NULL;
while(1)
{
printf("\nwhat do you want to do now?\nPress \n1-> to proceed for element processing \n2-> to display the elements \n3-> to search any element in the list \n4->Insert a node into linked list\n5->Delete the node from linked list\n");
scanf("%d",&b);
switch(b)
{
case 1: head=addofll(head);
break;
case 2: display(head);
break;
case 3: search(head);
break;
case 4: head=insert(head);
break;
case 5: head=delete(head);
break;
default: printf("You have entered an invalid option");
}
}
return 0;
}
void search(struct node*head)
{
int ele;
printf("Enter the element to be searched \n");
scanf("%d",&ele);
if(head!=NULL)
{
while (head!=NULL)
{
if(head->data==ele)
{
printf("\nthe element %d is found in the data\n",ele);
head=head->ptr;
}
else
{
while(head != NULL && head->data != ele)
{
head=head->ptr;
}
}
}
printf("\nThe search didnt find any element later\n");
}
else
printf("\nThere are no elements in the list\nPlease insert a new node\n");
}
void display(struct node* head)
{
printf("the data is displayed below\n");
while(head !=NULL)
{
printf("\n%d \t",head->data);
head=head->ptr;
}
}
struct node* addofll(struct node* head)
{
int dat;
printf("Enter data \n");
scanf("%d",&dat);
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=dat;
newnode->ptr=NULL;
newnode->ptr=head;
return newnode;
}
struct node* insert(struct node* head)
{
int info,num;
struct node* newnode;
struct node* temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the value to be inserted\n");
scanf("%d",&info);
newnode->data=info;
printf("\nPoint out the location of insertion in terms of data of linked list\n");
scanf("%d",&num);
temp=head;
if(temp == NULL)
{
printf("\nThe list is empty\nThe new node is the first node\n");
newnode->ptr=NULL;
return newnode;
}
while(temp !=NULL && temp->data !=num)
temp=temp->ptr;
if(temp !=NULL && temp->data ==num)
{
printf("\nyou have found the location of the insertion: inserting %d in list\n",info);
newnode->ptr=temp->ptr;
temp->ptr=newnode;
}
printf("\nyou are out of the loop\n");
return head;
}
struct node* delete(struct node* head)
{
struct node *temp,*del;
int num;
printf("\nEnter the lisked list data element to be removed\n");
scanf("%d",&num);
temp=head;
if(temp !=NULL && temp->data == num)
{
printf("\nThe value is detected and this node is getting deleted\n");
head=temp->ptr;
free(temp);
return head;
}
if(temp !=NULL && temp->ptr->data ==num)
{
del=temp->ptr;
temp->ptr=temp->ptr->ptr;
free(del);
return head;
}
while(temp !=NULL && temp-> data != num)
{
if(temp !=NULL && temp->data !=num) {
temp=temp->ptr;
}
else
return head;
}
if(temp == NULL)
{
printf("\nThere are no elements in the list\nCreate a new node first\n");
return 0;
}
}
Learn More :
Display
- DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program To Display The Number In A Specific Formats
- C Program that Display a IBM Logo
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- LED ON OFF For One Sec/Count and Display on the Attached Serial Monitor
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers & store all prime numbers in an array & display this result
- C program to display the transpose of given 3 X 3 matrix
- C Program to accept a string from user, delete all vowels from that string & display the result
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Creation and Display of Elements in Both Forward and Reverse Direction in a Doubly Linked List
- C Program to Display a real time clock (HH:MM:SS) on the LCD
- Program to Display Pie Chart Accepting User Input C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Program to display the following pattern in C
- Display the Following Pattern * ** *** **** ***** C Program
Menu Driven Program
Search
- Dictionary Word Search C Program
- C Depth First Search
- C Program To Search A Number Inside The Array
- C Program To Write Data In A File And Search Data From File
- C Program to search an element using linear search or binary search (menu driven program)
- C Program Performs a search and replace for a specified target and replacement string
- C Program Recursive function that searches for a given file in a given folder
- Pre Order, Post order, In order Implement Binary Tree using linked list
- BST Tree in C
- C Program Search Function For Sequence
- Linked List For Getting Employee Details, Display and Search For Salary C Program
Linked List
- C Program to Demonstrates a linked list for numbers.
- C Program to Implementation of List ADT as linked-list
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C program allocates new nodes and creates a four element list with fixed values
- C Program Simple linked list
- Program to Add Two Polynomials Using Linked List C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
Insertion
Creation
Node