Full Implementation code Given Below copy and Paste code in Visaual Studio code with file name linklist.c and click ctrl+` to open Terminal and type command
/*
& File : linklist.c
* Author : Taher Ali Badawarwala
* Purpose : C Linked List Data Structure
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int rollnumber;
charName[256];
charAddress[256];
int Telephone;
int standard;
struct node* next;
} node;
typedef void (*callback)(node* data);
/*
create a new node
initialize the student and next field
return the newly created node
*/
node* create( int rollnumber,char Name[256],char Address[256],int Telephone,int standard,node* next)
{
node* new_node = (node*)malloc(sizeof(node));
if(new_node ==NULL)
{
printf("Error creating a new node.\n");
exit(0);
}
new_node->rollnumber=rollnumber;
strcpy(new_node->Name,Name);
strcpy(new_node->Address,Address);
new_node->Telephone=Telephone;
new_node->standard=standard;
new_node->next = next;
return new_node;
}
/*
add a new node at the beginning of the list
*/
node* prepend(node* head,int rollnumber,char Name[256],char Address[256],int Telephone,int standard)
{
node* new_node = create(rollnumber,Name,Address,Telephone,standard,head);
head = new_node;
return head;
}
/*
traverse the linked list
*/
void traverse(node* head,callback f)
{
printf("--- Students Detail List--- \n\n");
node* cursor = head;
while(cursor!=NULL)
{
f(cursor);
cursor=cursor->next;
}
}
/*
remove node from the front of list
*/
node* remove_front(node* head)
{
if(head ==NULL)
returnNULL;
node *front = head;
head = head->next;
front->next = NULL;
/* is this the last node in the list */
if(front == head)
head = NULL;
free(front);
return head;
}
/*
remove node from the back of the list
*/
node* remove_back(node* head)
{
if(head ==NULL)
returnNULL;
node *cursor = head;
node *back = NULL;
while(cursor->next!=NULL)
{
back = cursor;
cursor=cursor->next;
}
if(back !=NULL)
back->next = NULL;
/* if this is the last node in the list*/
if(cursor== head)
head = NULL;
free(cursor);
return head;
}
/*
remove a node from the list
*/
node* remove_any(node* head,node* nd)
{
if(nd ==NULL)
returnNULL;
/* if the node is the first node */
if(nd == head)
return remove_front(head);
/* if the node is the last node */
if(nd->next==NULL)
return remove_back(head);
/* if the node is in the middle */
node* cursor = head;
while(cursor!=NULL)
{
if(cursor->next== nd)
break;
cursor=cursor->next;
}
if(cursor!=NULL)
{
node* tmp = cursor->next;
cursor->next= tmp->next;
tmp->next = NULL;
free(tmp);
}
return head;
}
/*
display a node
*/
void display(node* n)
{
// printf("--- Students Detail List--- \n\n");
if(n !=NULL)
printf("Name:-%s \t Address:-%s \t Roll Number:-%d \t Telephone:-%d \t Standard:-%d \n\n", n->Name,n->Address,n->rollnumber,n->Telephone,n->standard);
}
/*
Search for a specific node with input data
return the first matched node that stores the input data,
otherwise return NULL
*/
node* search(node* head,int rollnumber)
{
node *cursor = head;
while(cursor!=NULL)
{
if(cursor->rollnumber == rollnumber)
returncursor;
cursor=cursor->next;
}
returnNULL;
}
/*
remove all element of the list
*/
void dispose(node *head)
{
node *cursor, *tmp;
if(head !=NULL)
{
cursor= head->next;
head->next = NULL;
while(cursor!=NULL)
{
tmp = cursor->next;
free(cursor);
cursor= tmp;
}
}
}
/*
return the number of elements in the list
*/
int count(node *head)
{
node *cursor = head;
int c =0;
while(cursor!=NULL)
{
c++;
cursor=cursor->next;
}
return c;
}
/*
display the menu
*/
void menu()
{
printf("--- Students Detail System Using link List--- \n\n");
printf("0.menu\n");
printf("1.Insert a Student\n");
printf("2.search for a Student\n");
printf("3.Update Student Information\n");
printf("4.Delete Student Information\n");
printf("5.Display All\n");
printf("-1.quit\n");
}
void displayAll(node *head)
{
printf("--- Students Detail List--- \n\n");
printf("-- Name -- \t --Address-- \t -- Telephone -- \t --Roll Number -- \t -- Standard -- \n\n");
node *cursor=head;
while(cursor!=NULL)
{
cursor=cursor->next;
printf("-- %s -- \t -- %s -- \t -- %d -- \t --%d-- \t -- %d --",cursor->Name,cursor->Address,cursor->Telephone,cursor->rollnumber,cursor->standard);
}
}
int main()
{
int command =0;
int rollnumber;
charName[256];
charAddress[256];
int Telephone;
int standard;
node* head = NULL;
node* tmp = NULL;
callback disp = display;
menu();
while(1)
{
printf("\nEnter a command(0-5,-1 to quit):");
scanf("%d",&command);
if(command ==-1)
break;
switch(command)
{
case0:
menu();
break;
case1:
printf("Please enter the name of student:");
scanf("%s",&Name);
printf("\n Please Enter Address of Student:");
scanf("%s",&Address);
printf("\n Please Enter Roll number of Student:");
scanf("%d",&rollnumber);
printf("\n Please Enter Telephone of Student:");
scanf("%d",&Telephone);
printf("\n Please Enter Standard of Student:");
scanf("%d",&standard);
head = prepend(head,rollnumber,Name,Address,Telephone,standard);
traverse(head,disp);
break;
case2:
printf("Please enter a roll Number of Student:");
scanf("%d",&rollnumber);
tmp = search(head,rollnumber);
if(tmp !=NULL)
{
display(tmp);
}
else
{
printf("Student not found for roll number:- %d .",rollnumber);
}
break;
case3:
printf("Enter Student rollnumber to Update:");
scanf("%d",&rollnumber);
tmp = search(head,rollnumber);
if(tmp !=NULL)
{
printf("Please enter the New name of student:");
scanf("%s",&Name);
printf("\n Please Enter New Address of Student:");
scanf("%s",&Address);
strcpy(tmp->Name,Name);
strcpy(tmp->Address,Address);
display(tmp);
}
else
{
printf("Student not found for roll number:- %d .",rollnumber);
}
break;
case4:
printf("Enter the Roll Number of Student to remove:");
scanf("%d",&rollnumber);
tmp = search(head,rollnumber);
if(tmp !=NULL)
{
head=remove_any(head,tmp);
if(head !=NULL)
traverse(head,disp);
}
else
{
printf("Student not found for roll number:- %d .",rollnumber);
}
break;
case5:
traverse(head,disp);
break;
}
}
dispose(head);
return0;
}