Circularly LinkedList Deletion at any position

Algorithm

INPUT: HEADER IS A POINTER TO THE HEADER NODE OF THE LINKEDLIST AND X IS THE POSITION OF THE ELEMENT TO BE DELETED
OUTPUT: LINKEDLIST WITHOUT THE ELEMENT AT X TH POSITION
DATA STRUCTURE: SINGLY LINKEDLIST

STEP 1: START
STEP 2:PTR=HEADER->LINK
STEP 3: FOR( I TO X-1)
STEP 4: PTR1=PTR
STEP 5: PTR=PTR->LINK;
STEP 6: ENDFOR
STEP 7: PTR1->LINK=PTR->LINK
STEP 8: FREE(PTR)
STEP 9: END

Code

 #include <stdio.h>
 #include <stdlib.h>
 int getnode(int);
 void display();
 void newnode();int x;struct node{int data;struct node *link;};struct node *prev,*head,*news,*p,*ptr,*ptr1;
 struct node *header;
 int main(){
 int n,x;
 printf("ENTER THE LIMIT");
 scanf("%d",&n);
 getnode(n);
 return 0;}
 void display(struct node *head){
 struct node *ptr;
 ptr=header->link;
 while(ptr!=header){
 printf("%d ",ptr->data);
 ptr=ptr->link;} }
 void newnode(){
 ptr=head;
 ptr1=head;
 for(int i=1;i<x;i++){
    ptr1=ptr; 
 ptr=ptr->link;}
 ptr1->link=ptr->link;
 free(ptr);
 ptr=NULL;
 }
 int getnode(int n){
 header=malloc(sizeof(struct node));
 head=NULL;
 for(int i=0;i<n;i++){
 p=malloc(sizeof(struct node));
 scanf("%d",&p->data);
 p->link=NULL;
 if(head==NULL)
 head=p;
 else
 prev->link=p;
 prev=p;
 }
 p->link=header;
 header->link=head;
 display(header);
 printf("enter the position where you want to delete");
 scanf("%d",&x);
 newnode();
 display(header);
 return 0;}

Output

user@computer$ : ENTER THE LIMIT5 
1
2
3
4
5
1 2 3 4 5 enter the position where you want to delete2
1 3 4 5

1 thought on “Circularly LinkedList Deletion at any position”

Leave a Comment

Your email address will not be published. Required fields are marked *