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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include <stdio.h> #include <stdlib.h> 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; } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.