Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C implementation of inserting a new node to a link list

In this program, we are going to share a C implementation of Inserting a new Node to a link list. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C implementation of inserting a new node to a link list with the output.

  • Collection of 200+ C problems with solutions.

C implementation of inserting a new node to a link list

We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.

#include 
#include 

struct node{
	int data; // data field
	struct node *next;
};

void traverse(struct node* head){
	struct node* current=head; // current node set to head
	int count=0; // to count total no of nodes
	printf("\n traversing the list\n");
	while(current!=NULL){ //traverse until current node isn't NULL
		
		count++; //increase node count
		printf("%d ",current->data);
		current=current->next; // go to next node
		
	}
	
	printf("\ntotal no of nodes : %d\n",count);
}

struct node* creatnode(int d){
	struct node* temp=malloc(sizeof(struct node));
	temp->data=d;
	temp->next=NULL;
	return temp;
}

int main(){
	printf("creating the linked list by inserting new nodes at the begining\n");
	
	printf("enter 0 to stop building the list, else enter any integer\n");
	
	int k,count=1,x;
	
	struct node* curr;
	
	scanf("%d",&k);
	struct node* head=creatnode(k); //buliding list, first node
	scanf("%d",&k);
	///////////////////inserting at begining//////////////////////
	while(k){
	curr=creatnode(k);
	curr->next=head;                     //inserting each new node at the begining
	head=curr;
	scanf("%d",&k);
	}
	traverse(head); // displaying the list
	////////////////////////inserting at the end/////////////////////
	printf("enter the integer you want to add at the end of the list\n");
	scanf("%d",&k);
	struct node* temp=head;
	while(temp->next){
		temp=temp->next;  // traversing to the last node of existing list
	}
	struct node* cur=creatnode(k);
	temp->next=cur;  //appended
	printf("..............new node added at the end..............\n");
	traverse(head);
	
	////////////inserting at middle/////////////
	
	printf("Enter your desired position where you want to insert the new node\n");
	scanf("%d",&x);
	printf("enter desired node data\n");
	scanf("%d",&k);
	
	curr=head;
	
	while(count!=x-1){
		curr=curr->next;            // curr locating at desired node
		count++;
	}
	cur=creatnode(k);
	
	temp=curr->next;  //temp node as described as article
	
	curr->next=cur;    // inserting in the middle
	cur->next=temp;
	
	printf("New node added after the desired position...\n");
	
	traverse(head);
	return 0;
	
}

The post C implementation of inserting a new node to a link list appeared first on FreeWebMentor.



This post first appeared on Programming Blog Focused On Web Technologies, please read the originial post: here

Share the post

C implementation of inserting a new node to a link list

×

Subscribe to Programming Blog Focused On Web Technologies

Get updates delivered right to your inbox!

Thank you for your subscription

×