Insertion At Location in Circular linked list
C function for insertion at given Location
void insert_location(struct link *node) { int node_no=1,insert_no,flag=0,count; node=start->next; ptr=start; count=i; printf("\n Enter position where you want to insert new node:-"); scanf("%d",&insert_no); while(count) { if(node_no==insert_no) { new1=(struct link *)malloc(sizeof(struct link)); printf("\n Insert data for new node:-"); scanf("%d",&new1->data); ptr->next=new1; new1->next=node; flag=1; break; } else { ptr=ptr->next; node=node->next; } node_no++; count--; } if(flag==0) { printf("\n Position not found"); } else { i++; } }
C programe for insertion at given Location in Circular linked list
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct link { int data; struct link *next; }; int i=0; struct link *node,*start,*ptr,*new1; void create_link(struct link *node) { char ch; start->next=NULL; node=start; fflush(stdin); printf("\n Enter 'n' for break:"); ch=getchar(); while(ch!='n') { node->next=(struct link *)malloc(sizeof(struct link)); node=node->next; printf("\n Enter data for node:"); scanf("%d",&node->data); node->next=start; fflush(stdin); printf("\n Enter 'n' for break:"); ch=getchar(); i++; } } void insert_location(struct link *node) { int node_no=1,insert_no,flag=0,count; node=start->next; ptr=start; count=i; printf("\n Enter position where you want to insert new node:-"); scanf("%d",&insert_no); while(count) { if(node_no==insert_no) { new1=(struct link *)malloc(sizeof(struct link)); printf("\n Insert data for new node:-"); scanf("%d",&new1->data); ptr->next=new1; new1->next=node; flag=1; break; } else { ptr=ptr->next; node=node->next; } node_no++; count--; } if(flag==0) { printf("\n Position not found"); } else { i++; } } void display(struct link *node) { int count; node=start->next; count=i; while(count) { printf("\t%d",node->data); node=node->next; count--; } } void main() { char ch; clrscr(); create_link(node); insert_location(node); printf("List Item Are:\n"); display(node); getch(); }