Lecture 5.2 - Linked List
Lecture 5.2 - Linked List
C I R C U L A R S I N G LY
LINKED LIST
CIRCULAR LINKED LIST
• In a circular linked list, the last node contains a pointer to the first
node of the list.
temp = head;
if(head == NULL) {
head = newNode;
newNode -> next = head;
}
else {
struct Node *temp = head;
while(temp -> next != head)
temp = temp -> next;
newNode -> next = head;
head = newNode;
temp -> next = head;
}
printf("\nInsertion success!!!");
}
INSERTION: CIRCULAR SINGLY LINKED LIST AT
END
Scenario 1: Empty List The same with slide 64
//we need to traverse the list in order to reach the last node of the list
while(temp->next != head)
temp = temp->next;
if(head == NULL) {
head = newNode;
newNode -> next = head;
}
else {
struct Node *temp = head;
while(temp -> next != head)
temp = temp -> next;
temp -> next = newNode;
newNode -> next = head;
}
printf("\nInsertion success!!!");
}
DELETION: CIRCULAR SINGLY LINKED LIST AT
BEGINNING
Scenario 1: The list contains single node head → next == head
//delete the entire list and make the head pointer free.
if(head->next == head) {
head = NULL;
free(head);
}
else {
struct Node *temp = head;
if(temp -> next == head) //That means there is only one node
{
head = NULL;
free(head);
}
else
temp = temp-> next;
printf("\nDeletion success!!!");
}
}
DELETION: CIRCULAR SINGLY LINKED LIST AT
END
Scenario 1: The list contains single node head → next == head
if(head->next == head) {
head = NULL;
free(head);
}
head = ptr;
if(head==NULL) {
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else {
temp = head;
while(temp -> next != head) {
temp = temp -> next;
}
temp -> next = ptr;
ptr -> prev = temp;
head -> prev = ptr;
ptr -> next = head;
head = ptr;
}
printf("Node Inserted"); }
INSERTION: CIRCULAR DOUBLY LINKED LIST AT
END
Scenario 1: the list contains more than one element
void insertion_last(int item) {
struct node *ptr = (struct node *) malloc(sizeof(struct node));
struct node *temp;
ptr->data=item;
if(head == NULL) {
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else {
temp = head;
while(temp->next !=head) {
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
head -> prev = ptr;
ptr -> next = head;
}
printf("\nNode Inserted\n"); }
DELETION: CIRCULAR DOUBLY LINKED LIST AT
BEGINNING
Scenario 1: The node which is to be deleted can be the only node present in the linked list.
head → next == head //needs to be completely deleted
head = NULL;
free(head);
Scenario 2: The list contains more than one element in the list
temp = head;
// reach the last node of the list
while(temp -> next != head) {
temp = temp -> next;
}
temp -> next = head -> next;
head -> next -> prev = temp;
free(head);
head = temp -> next;
void deletion_beginning() {
struct node *temp;
if(head->next == head) {
head = NULL;
free(head);
printf("\nNode Deleted\n");
}
else {
temp = head;
while(temp -> next != head) {
temp = temp -> next;
}
temp -> next = head -> next;
head -> next -> prev = temp;
free(head);
head = temp -> next;
printf("\nNode Deleted\n");
}
DELETION: CIRCULAR DOUBLY LINKED LIST AT
END
The node which is to be deleted can be the The list contains more than one element in the list
only node present in the linked list head →
temp = head;
next == head
while(temp -> next != head) {
head = NULL;
temp = temp -> next;
free(head);
}
void deletion_last() {
struct node *ptr;
if(head->next == head) {
head = NULL;
free(head);
printf("\nNode Deleted\n");
}
else {
ptr = head;
if(ptr->next != head)
{
ptr = ptr -> next;
}
ptr -> prev -> next = head;
head -> prev = ptr -> prev;
free(ptr);
printf("\nNode Deleted\n");
}
}