Create delete from position in circular linked list

this is a code for adding a node on the given position in a circular linked list
pull/1034/head
avniagarwal23 2022-10-14 13:29:32 +05:30 committed by GitHub
parent 1cc547fd8b
commit c4d164ef44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,58 @@
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *newnode,*current,*nextnode,*tail;
int choice,i=1,length=0,position;
tail=0;
while(choice)(
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d",&newnode->data);
newnode->next=0;
if(tail==0)
{
tail=newnode;
tail->next=newnode;
}
else
{
newnode->next=tail->next;
tail->next=newnode;
tail=newnode;
}
length++;
printf("Do you want to continue(0,1): ");
scanf("%d",&choice);
}
current=tail->next;
printf("Enter the position from where you want to delete the elements: ");
scanf("%d",&position);
if(position<0 && position>length)
{
printf("Invalid Position");
}
else
{
while(i<position-1)
{
current=current->next;
i++;
}
nextnode=current->next;
current->next=nextnode->next;
free(nextnode);
}
printf("After deleting the element from %d position the resultant element in th list are: ",position);
current=tail->next;
do
{
printf("%d",current->data);
current=current->next;
}while(current!=tail->next);