Now we will see how to delete node at position 'N' in circular link list.
Algorithm to delete node at position 'N' in circular link list:
In this algorithm, TAIL points to last node of the link list and POS is position where the new node to be deleted. The steps are as follows:- Initialize TEMP and TEMP2
- If list have no node or position is less than one (TAIL == NULL or POS < 1)
- Node cannot be deleted
- Else
- If (POS == 1)
- If list have one node (TAIL->NEXT == TAIL)
- Delete TAIL (TAIL = NULL)
- Else
- Delete the first node (TAIL->NEXT = TAIL->NEXT->NEXT)
- End If;
- If list have one node (TAIL->NEXT == TAIL)
- Else
- Set TEMP = TAIL->NEXT (TEMP now points to first node)
- Traverse the list for (POS-1)th node using TEMP
- If node to be deleted is last node i.e. (TEMP->NEXT == TAIL)
- Set TAIL = TEMP
- End If;
- If node to be deleted is last node i.e. (TEMP->NEXT == TAIL)
- Set TEMP->NEXT = TEMP->NEXT->NEXT
- End If;
- If (POS == 1)
- End If;
Function to delete node at position 'N' in circular link list:
int deleteAtPosition(NODE **tail, int pos){
NODE *temp, *temp2;
int info = -1;
if((*tail == NULL) || pos < 1){
printf("\nERROR: List have no nodes or invalid position");
}
else{
if(pos == 1){
if((*tail)->next == *tail){
info = (*tail)->info;
free(*tail);
*tail = NULL;//
}
else{
temp2 = (*tail)->next;
info = temp2->info;
(*tail)->next = (*tail)->next->next;//
free(temp2);
}
}
else{
temp = (*tail)->next;
for( int i = 1; i < (pos-1); i++){
temp = temp->next;
if(temp == *tail){
printf("\nError: Cannot be deleted, invalid position");
return -1;
}
}
if(temp->next == *tail){
*tail = temp;
}
temp2 = temp->next;
info = temp2->info;
temp->next = temp->next->next;//
free(temp2);
}
}
return info;
}
Program to delete node at position 'N' in circular link list:
#include <stdio.h>
#include <malloc.h>
struct node{
int info;
struct node *next;
};
typedef struct node NODE;
void insertAtFirst(NODE **, int);
int deleteAtPosition(NODE **, int);
void traverse(NODE **);
int main(){
NODE *tail = NULL;
insertAtFirst(&tail, 1); // 1
insertAtFirst(&tail, 2); // 2 1
insertAtFirst(&tail, 3); // 3 2 1
insertAtFirst(&tail, 4); // 4 3 2 1
traverse(&tail); // 4 3 2 1
deleteAtPosition(&tail, 4);
traverse(&tail); // 4 3 2
deleteAtPosition(&tail, 1);
traverse(&tail); // 3 2
deleteAtPosition(&tail, 3); // invalid position
printf("\n");
traverse(&tail); // 3 2
return 0;
}
int deleteAtPosition(NODE **tail, int pos){
NODE *temp, *temp2;
int info = -1;
if((*tail == NULL) || pos < 1){
printf("\nERROR: List have no nodes or invalid position");
}
else{
if(pos == 1){
if((*tail)->next == *tail){
info = (*tail)->info;
free(*tail);
*tail = NULL;//
}
else{
temp2 = (*tail)->next;
info = temp2->info;
(*tail)->next = (*tail)->next->next;//
free(temp2);
}
}
else{
temp = (*tail)->next;
for( int i = 1; i < (pos-1); i++){
temp = temp->next;
if(temp == *tail){
printf("\nError: Cannot be deleted, invalid position");
return -1;
}
}
if(temp->next == *tail){
*tail = temp;
}
temp2 = temp->next;
info = temp2->info;
temp->next = temp->next->next;//
free(temp2);
}
}
return info;
}
void insertAtFirst(NODE **tail, int info){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
ptr->info = info;
if(*tail == NULL){
ptr->next = ptr;
*tail = ptr;
}
else{
ptr->next = (*tail)->next;
(*tail)->next = ptr;
}
}
void traverse(NODE **tail){
NODE *temp;
temp = *tail;
if(*tail){
do{
printf("%d ", temp->next->info);
temp = temp->next;
} while(temp != *tail);
printf("\n");
}
}
4 3 2 1 4 3 2 3 2 Error: Cannot be deleted, invalid position 3 2
Comments
Post a Comment