Example & Tutorial understanding programming in easy ways.

Write code to delete the item at end in circular linked list.

Example-
Linked list is:3->4->1->9
Deletion at end
Linked list become: 3->4->1

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 del()
{
struct node *cpt,*ptr=first;
while(ptr->next!=first)
{
cpt=ptr;
ptr=ptr->next;
}
cpt->next=first;
free(ptr);
}

void main()
{
clrscr();
create();
traverse();
del();
printf("After deletion at endingn");
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
After deletion at ending
Linklist is :
2 4 1 9

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




Read More →