#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *tail = NULL; void addnode(int data); int countnode(); void display(); void insertnode(int ip,int data); int main() { int n,num,i=0,ip; printf("Enter the number of nodes required in linked list: "); scanf("%d",&n); while(i!=n) { printf("Enter data in node %d: ",(i+1)); scanf("%d",&num); addnode(num); i++; } printf("\nThe number of nodes in the list is %d\n",countnode()); display(); printf("\nEnter the position at which you want to insert new node: "); scanf("%d",&ip); printf("Enter the data to be inserted in new node: "); scanf("%d",&num); insertnode(ip,num); printf("\nThe number of nodes in the list is %d\n",countnode()); display(); } void addnode(int data) { struct node *newnode = (struct node *)malloc(sizeof(struct node)); newnode->data= data; newnode->next= NULL; if(head == NULL) { head = newnode; tail = newnode; } else { tail->next=newnode; tail = newnode; } } int countnode() { int count=0; struct node *current = head; while(current !=NULL) { count++; current = current->next; } return count; } void display() { struct node *current = head; while(current != NULL) { printf("%d ",current->data); current = current->next; } } void insertnode(int ip,int data) { struct node *newnode = (struct node *)malloc(sizeof(struct node)); struct node *temp; struct node *ptr = head; if(ip == 1) { newnode->data = data; newnode->next = head; head = newnode; } else { for(int i=1;i<ip-1;i++) { ptr = ptr->next; } temp = ptr->next; ptr->next = newnode; newnode->data = data; newnode->next = temp; } }