Example & Tutorial understanding programming in easy ways.

What is insertion at end in circular linked list, write code for that.

Example-
Linked list is:3->4->1->9
Insert: 8
Linked list become: 3->4->1->9->8

program-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *first;

void create()
{
struct node *ptr,*cpt;
char ch;
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter Numbern");
scanf("%d",&cpt->data);
first=cpt;
do
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter Numbern");
scanf("%d",&ptr->data);
cpt->next=ptr;
cpt=ptr;
printf("Continue(y/n)?n");
ch=getch();
}while(ch=='y');
cpt->next=first;
}

void traverse()
{
struct node *ptr=first;
printf("Linklist is :n");
while(ptr->next!=first)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}

void insertion()
{
struct node *ptr,*cpt=first;
while(cpt->next!=first)
cpt=cpt->next;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter Number to insertn");
scanf("%d",&ptr->data);
cpt->next=ptr;
ptr->next=first;
}

void main()
{
clrscr();
create();
traverse();
insertion();
traverse();
getch();
}


output-

Enter Number
2
Enter Number
4
Continue(y/n)?
Enter Number
1
Continue(y/n) ?
Enter Number
9
Continue(y/n) ?
Enter Number
0
Continue(y/n) ?
Linklist is :
2 4 1 9 0
Enter Number to insert
10
Linklist is :
2 4 1 9 0 10
-

-In this program, we have a three function create(), traverse() and insertion
-create() function are used to create a Circular Linked List
-traverse() function are used to traverse the circular linked list.
-insertion() function are used to insert the data at the end of the linked list




Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!