Environment Requirement
- We will developing our program in Visual Studio Code and GCC compiler . To get it setup for you follow this link .
Prerequisite
- Basic Syntax and concept of C . If you haven't know yet kindly follow this link to learn basic concept and syntax
- Basic Knowledge about link list . We can read it from here
Implemention
- First Create Structure of Student to store value in it .
typedef struct node
{
int rollnumber;
charName[256];
charAddress[256];
int Telephone;
int standard;
struct node* next;
} 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;
}
- Now we have change new added pointer to head pointer . As For First node previous is NULL how New added node will point to Null and header to new Node . We will give this function name is prepend
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;
}
- For traverse the link list we will create on function name traverse . Which will traverse from first to the node those pointer point to null which is last Node . It will be mainly used to search, Update and Delete We will see the later section
typedef void (*callback)(node* data);
void traverse(node* head,callback f)
{
node* cursor = head;
while(cursor!=NULL)
{
f(cursor);
cursor=cursor->next;
}
}
- Some times We need to count the node . we will use traverse function to count node . By just increasing counter in it .
int count(node *head)
{
node *cursor = head;
int c =0;
while(cursor!=NULL)
{
c++;
cursor=cursor->next;
}
return c;
}
Insert Operation
- We Can Add Node the end of link list
- We Can Add Node in the middle of link list
- We Can Add Node in the start of Link List
For Our Example we will adding node in start of Link List Means . Which we will prepend
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;
}
Search Operation
To search for a node that stores a given data, we scan the whole list and return the first node that stores the searched data . We will be searching node by using roll number of students. The following illustrates the search function:
node* search(node* head,int rollnumber)
{
node *cursor = head;
while(cursor!=NULL)
{
if(cursor->rollnumber == rollnumber)
returncursor;
cursor=cursor->next;
}
returnNULL;
}
Update Operation
We will Update node first by searching through roll number as we get the pointer we can change its name and address only now .
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);
}
Delete Operation
We Can Delete Node From Start , End and From the middle . But in this example we will be deleteing on the basis of roll number . First we have to find is it first node , middle node or last after finding it . we Will delete it on the basis of our previous review .
- To delete a first node of the linked list is relatively simple. We point the
head
to the next node and remove the node that thehead
pointed to. For Delete we will use free function release that memory
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;
}
Delete Node From the end
- To Delete Pointer from the end we need to use two pointers . In Which one pointer moves ahead of second pointer . When First Pointer reaches end Means able to find NULL pointer which is end . We will delete node of second pointer which point to the end Node . Following Diagram illustrate that
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;
}
Delete Node from Middle
- Here Also we will be using two Pointers one temp on cursor . temp always move to find selected node as we find the selected node we will Assign selected node pointer to the previous node and to which now cursor is pointing and delete temp pointer node . Following example illustrate that
node* remove_any(node* head,node* nd)
{
/* if the node is the first node */
if(nd == head)
{
head = remove_front(head);
return head;
}
/* if the node is the last node */
if(nd->next==NULL)
{
head = remove_back(head);
return 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;
}
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
GCC linklist.c
./a (to run application)
/*
& 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;
}
Leave a Comment
No Comments Yet