If there's any direct connection(two way - from head to tail & tail to head) exists between head and tail of a list, then that list is called circular linked list.
See Also:
Example Program for Circular Doubly Linked List Implementation(in C):
See Also:
C Program To Merge Two Arrays
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
Insertion, Deletion, Traversal, Reversal And Search Operation on Arrays
Doubly Linked List - Insertion, Traversal, Searching, Delete Node, Delete List
Doubly Linked List - Insertion, Traversal, Searching, Delete Node, Delete List
Example Program for Circular Doubly Linked List Implementation(in C):
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *prev, *next;
};
struct node *head = NULL, *tail = NULL;
struct node *createNode(int);
void insertNode(int);
void deleteNode(int);
void display();
struct node* createNode(int data) {
struct node *ptr = (struct node *)malloc(sizeof (struct node));
ptr->data = data;
ptr->prev = NULL;
ptr->next = NULL;
return (ptr);
}
/* insertion in circular linked list */
void insertNode(int data) {
struct node *temp, *ptr = createNode(data);
/* list is empty */
if (!head) {
head = ptr;
head->next = head;
head->prev = head;
tail = head;
return;
} else {
/* only one node present in list */
if (head->next == head && head->prev == head) {
temp = head;
ptr->next = temp;
ptr->prev = temp->prev;
temp->prev = ptr;
temp->next = ptr;
tail = ptr;
} else {
/* do insertion next to head */
temp = head->next;
ptr->next = temp;
ptr->prev = temp->prev;
temp->prev->next = ptr;
temp->prev = ptr;
}
}
}
void deleteNode(int data) {
struct node *ptr, *temp = head;
/* if list is not present */
if (head == NULL) {
printf("Data unavailable\n");
return;
} else if (temp->data == data) {
/* deleting head node */
if (head == tail) {
temp->prev = NULL;
temp->next = NULL;
free(temp);
head = tail = NULL;
} else {
temp->next->prev = tail;
tail->next = temp->next;
head = temp->next;
temp->next = temp->prev = NULL;
free(temp);
}
} else {
while (temp->next != head && temp->data != data) {
ptr = temp;
temp = temp->next;
}
if (temp->next == head && temp->data != data) {
printf("Given data unvavailable in list\n");
return;
} else if (temp->next != head && temp->data == data) {
/* deleting any node in between head & tail */
ptr->next = temp->next;
temp->next->prev = temp->prev;
temp->next = NULL;
temp->prev = NULL;
free(temp);
printf("Data deleted successfully\n");
} else if (temp->next == head && temp->data == data) {
/* deleting the tail node */
ptr->next = temp->next;
temp->next->prev = ptr;
tail = ptr;
free(temp);
printf("Data deleted successfully\n");
}
}
}
/* traversing the list */
void display() {
struct node *ptr = head;
int i = 0;
while (ptr) {
printf("%-3d\t", ptr->data);
if (ptr->next == head)
break;
ptr = ptr->next;
i++;
}
printf("\n");
}
int main() {
int ch, data;
while (1) {
printf("1.Insertion\t2.Deletion\n");
printf("3.Display\t4.Exit\n");
printf("Enter ur choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data to insert:");
scanf("%d", &data);
insertNode(data);
break;
case 2:
printf("Enter data to delete:");
scanf("%d", &data);
deleteNode(data);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("please enter right option\n");
break;
}
}
return 0;
}
Output: (Insertion, Deletion, Traversal In Circular Doubly Linked List)
jp@jp-VirtualBox:$ ./a.out
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:1
Enter data to insert:10
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:1
Enter data to insert:20
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:1
Enter data to insert:30
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:3
10 30 20
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:2
Enter data to delete:20
Data deleted successfully
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:3
10 30
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:2
Enter data to delete:10
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:3
30
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:4
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:1
Enter data to insert:10
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:1
Enter data to insert:20
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:1
Enter data to insert:30
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:3
10 30 20
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:2
Enter data to delete:20
Data deleted successfully
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:3
10 30
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:2
Enter data to delete:10
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:3
30
1.Insertion 2.Deletion
3.Display 4.Exit
Enter ur choice:4
No comments:
Post a Comment