FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Linked List Example With Insertion At the Beginning, Mid and End In c


#include 
#include 
typedef struct node      // structure node
{
    int info;             // a information that is to be stored
    struct node *next;    // create a next name pointer which will point to node type
}node_t;                  // node_t object is created

node_t *first,*rear,*printing;
// first is for storing first information pointer
// rear is to store where we have last stored the information
// printing is to track while printing.

void createlist();          // it create a empty list
void insert_element_at_beg(int);
// above funtion will create a new node and insert that into list
// below function will create a new node and insert that into list
void insert_element_at_end(int);
// below function will create a new node and insert it after
// at specific node if found else print node not found
void insert_element_at_mid();
void print_list();          // it will print the list
void main()
{
    int info,choice,after;
    clrscr();
	// create list at first
    createlist();
	// loop till user wants to exit
    do
    {    // ask user for choices
	printf("Enter your choice\n\n");
	printf("Press 1 to Enter the Number at the begining\n");
	printf("Press 2 to Enter the Number at the end\n");
	printf("Press 3 to Enter the Number at mid\n");
	printf("Press 4 to Print the list\n");
	printf("Press 5 to exit\n");
	scanf("%d",&choice);
		// if one call insert_element()
	if(choice==1)
	{
	    printf(" \n Enter the element");
	    scanf("%d",&info);
	    insert_element_at_beg(info);
	}
	else if(choice==2){
	 printf("\n Enter the element");
	 scanf("%d",&info);
	 insert_element_at_end(info);

	}
	else if(choice==3){
	 insert_element_at_mid();

	}
	else if(choice==4){
	 print_list();      // it will print the list
	 printf("\n");
	}
    }
    while(choice!=5); // if choice is 2 break the loop

    getch();
}

void createlist()
{
    node_t *createlist=malloc(sizeof(node_t));  // create a new node
	// intialize everyting with null
    createlist->next=NULL;
    createlist->info=NULL;
	// initialize first and rear with the first node created
    first=createlist;
    rear=createlist;
}

void insert_element_at_beg(int element)
{
    node_t *new_node=malloc(sizeof(node_t)); // create a new node
	// check if list is empty
	// if yes then enter the first element
    if(first->info==NULL)
    {
	
	first->info=element;
    }
	// if not first node then
	// enter the info in newly created node and
	// point its next to our last created node.
	// and make our rear pointing to newly created node.
    else
    {
	new_node->info=element;
	new_node->next=rear;
	rear=new_node;
    }
}

void insert_element_at_end(int element){
     node_t *new_node=malloc(sizeof(node_t)); // create a new node
	// check if list is empty
	// if yes then enter the first element
    if(first->info==NULL)
    {
	first->info=element;
    }
	// if not first node then
	// enter the info in newly created node and
	// point its next to our last created node.
	// and make our rear pointing to newly created node.
    else
    {
	new_node->info=element;
	new_node->next=NULL;
	first->next=new_node;
	first=new_node;
    }



}

void insert_element_at_mid(){
	node_t *new_node=malloc(sizeof(node_t)); // create a new node
	int element,afterv;
    char choice1;
     if(first->info==NULL)
	 {
      printf("\nList is empty. Add some numbers first\n");
     }
     else{
     printf("\n Enter the element after which you want to add\n");
	 scanf("%d",&afterv);
	 printf("\n Enter the element which you wnat to add\n");
	 scanf("%d",&element);
     printing=rear;
	 // loop to find the element after which we have to enter the new number
		while(printing->info!=afterv){  
		printing=printing->next;
		if(printing==NULL)   // if not element not found then break the loop
		break;
		}
		if(printing==NULL){
		printf("Element Not Found");
	}
	else{    // if found than add the node.
	new_node->info=element;
	new_node->next=printing->next;
	printing->next=new_node;
     }
    }
}
// prints the whole list
void print_list()
{
    printing=rear;  // intializes the printing
	// while loop -- until a node with next=null is not found
    while(printing!=NULL)
    {   if(printing->next==NULL && printing->info==NULL){
    printf("List is Empty");
    }
	printf("%d ",printing->info);
	printing=printing->next;
    }


}