Insertion At Last in Circular linked list
Procedure for insertion a node at the Last of list
Step1. Create the new nodeStep2. Set the new node’s next to itself (circular!)
Step3. If the list is empty,return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.
Algorithm for Insertion at the last of Circular linked list
node* AddEnd(node* tail, int num) { node *temp = (node*)malloc(sizeof(node)); temp->data = num; temp->next = temp; if (tail == NULL) return temp; temp->next = tail->next; tail->next = temp; return temp; }
C function for insertion at Last
void insert_last(struct link *node) { int count; node=start->next; ptr=start; count=i; while(count) { ptr=ptr->next; node=node->next; count--; } new1=(struct link *)malloc(sizeof(struct link)); printf("\n Insert data for last node:"); scanf("%d",&new1->data); ptr->next=new1; new1->next=node; i++; }
C programe for insertion at last 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_last(struct link *node) { int count; node=start->next; ptr=start; count=i; while(count) { ptr=ptr->next; node=node->next; count--; } new1=(struct link *)malloc(sizeof(struct link)); printf("\n Insert data for last node:"); scanf("%d",&new1->data); ptr->next=new1; new1->next=node; 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_last(node); printf("List Item Are:\n"); display(node); getch(); }