Okk so we have already dealt with creating, viewing and inserting in the linked list. Now i will be dealing with deleting a node.
4)Deleting a node:- void delete_link_list(struct link *);
void delete_link_list(struct link *node) {
int delete_node;
int node_number=1;
struct link *previous;
printf("\nEnter the node number which u want to delete:");
scanf("%d",&delete_node);
node=start->next;
previous=start;
while(node) {
if(node_number==delete_node) {
previous->next=node->next;
free(node);
}
else{
node=node->next;
previous=previous->next;
}
node_number++;
}
printf("\n\n");
}
Main code to be interpreted:- previous->next=node->next;
We have already declared 'previous' as a node which holds the address of the node prior to the node which is to be deleted.Again node has the address of the next node.Thus when we declare previous->next equals node->next we make a direct link between the nodes deleting the current node.Again to free the resources we use free() function.
5)Sorting Linked List:- void sort_link_list(struct link *);
void sort_link_list(struct link *node) {
struct link *counter;
int temp;
counter->next=(struct link *)malloc(sizeof(struct link));
printf("\nInside sort_link_list function");
node=start->next;
for(;node!=NULL;node=node->next){
for(counter=node->next;counter!=NULL;counter=counter->next){
if(counter->info<node->info) {
temp=node->info;
node->info=counter->info;
counter->info=temp;
}
}
}
printf("\n\n");
}
Note:- Here is simple code for looping. We have for loop which loops through nodes through the code node=node->next.This is a code of simple bubble sort in terms of pointers.
6) Reversing linked list:- void reverse_link_list(struct link *);
void reverse_link_list(struct link *node) {
struct link *previous,*current,*nextnode;
previous=(struct link *)malloc(sizeof(struct link ));
current=(struct link *)malloc(sizeof(struct link ));
nextnode=(struct link *)malloc(sizeof(struct link ));
node=start;
previous=node;
current=previous->next;
nextnode=current->next;
previous->next->next=NULL;
while(nextnode!=NULL) {
previous=current;
current=nextnode;
nextnode=current->next;
current->next=previous;
}
node->next=current;
}
Note:- The trick here is that we want the 'previous' node to form the reversed linked list. The current node and the nextnode are for interchanging and storing the temporary data. Hence we have declared previous->next->next =NULL; signalling that the it is the end of list.Again Reversing of the list is done by the back chaining pointer manipulation command current->next=previous; in the while loop.Rest is simple pointer arithmetics.
Now i finally present the whole program for Linked List:
//Creating a linked list and sorting the list
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct link {
int info;
struct link *next;
};
struct link *start=NULL;
void create_link_list(struct link *);
void display_link_list(struct link *);
void insert_link_list(struct link *);
void delete_link_list(struct link *);
void sort_link_list(struct link *);
void reverse_link_list(struct link *);
void create_link_list(struct link *node) {
int info;
int node_number=1;
char choice;
printf("\n\nInside create_link_list function");
printf("\nEnter n to break from the loop");
choice=getch();
node=start;
while(choice!='n') {
node->next=(struct link *)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\nEnter the value of the node%d:",node_number);
scanf("%d",&info);
node->info=info;
printf("\nEnter n to break from the loop");
choice=getch();
node_number++;
}
printf("\n\n");
}
void display_link_list(struct link *node) {
printf("\n\nInside display_link_list function");
node=start->next;
while(node) {
printf("%d\t",node->info);
node=node->next;
}
printf("\n\n");
}
void insert_link_list(struct link *node) {
int insert_node;
int node_number=1;
struct link *new1;
printf("\n\nInside insert_link_list function");
printf("\nEnter the node number where the node is to be inserted:");
scanf("%d",&insert_node);
node=start;
while(node->next) {
if(node_number==insert_node) {
new1->next=(struct link *)malloc(sizeof(struct link));
new1->next=node->next;
node->next=new1;
printf("\nInserting the value at node%d",node_number);
printf("\nEnter the value of the new node:");
scanf("%d",&new1->info);
break;
}
else {
node=node->next;
}
node_number++;
}
printf("\n\n");
}
void delete_link_list(struct link *node) {
int delete_node;
int node_number=1;
struct link *previous;
printf("\nEnter the node number which u want to delete:");
scanf("%d",&delete_node);
node=start->next;
previous=start;
while(node) {
if(node_number==delete_node) {
previous->next=node->next;
free(node);
}
else{
node=node->next;
previous=previous->next;
}
node_number++;
}
printf("\n\n");
}
void sort_link_list(struct link *node) {
struct link *counter;
int temp;
counter->next=(struct link *)malloc(sizeof(struct link));
printf("\nInside sort_link_list function");
node=start->next;
for(;node!=NULL;node=node->next){
for(counter=node->next;counter!=NULL;counter=counter->next){
if(counter->info<node->info) {
temp=node->info;
node->info=counter->info;
counter->info=temp;
}
}
}
printf("\n\n");
}
void reverse_link_list(struct link *node) {
struct link *previous,*current,*nextnode;
previous=(struct link *)malloc(sizeof(struct link ));
current=(struct link *)malloc(sizeof(struct link ));
nextnode=(struct link *)malloc(sizeof(struct link ));
node=start;
previous=node;
current=previous->next;
nextnode=current->next;
previous->next->next=NULL;
while(nextnode!=NULL) {
previous=current;
current=nextnode;
nextnode=current->next;
current->next=previous;
}
node->next=current;
}
void main() {
int choice;
struct link *node=(struct link *)malloc(sizeof(struct link ));
clrscr();
printf("\n1)Create link list \n2)Display link list ");
printf("\n3)Insert node in link list");
printf("\n4)Delete node");
printf("\n5)Sort link list");
printf("\n6)Reverse link list");
printf("\n0)Exit");
printf("\nEnter ur choice");
scanf("%d",&choice);
while(choice!=0) {
switch(choice) {
case 1:
printf("\nCreating the linked list");
create_link_list(node);
break;
case 2:
printf("\nDisplaying the linked list:\n");
display_link_list(node);
break;
case 3:
printf("\n\nInserting node in the linked list:\n");
insert_link_list(node);
break;
case 4:
printf("\n\nDeleting node from the linked list:\n");
delete_link_list(node);
break;
case 5:
printf("\n\nSorting the elements of the linked list:\n");
sort_link_list(node);
break;
case 6:
printf("\n\nReversing the link list");
reverse_link_list(node);
// printf("\nSorry but the reverse algorithm is yet not finished");
break;
default:
printf("\n\nYou have probably entered illegal number");
printf("\nBetter luck next time");
break;
}
printf("\n1)Create link list \n2)Display link list \n3)Insert a node in the list");
printf("\n4)Delete node");
printf("\n5)Sort linked list");
printf("\n6)Reverse linked list");
printf("\n0)Exit");
printf("\nEnter ur choice");
scanf("%d",&choice);
}
}
I hope this brief presentation would be sufficient to understand the intricacies of the Linked List. I love to hear some comments from you all visitors.