DS Assignment
DS Assignment
The circular linked list is a linked list where all nodes are connected to
form a circle. In a circular linked list, the first node and the last node are
connected to each other which forms a circle. There is no NULL at the end.
In a circular Singly linked list, the last node of the list contains a pointer to
the first node of the list. We traverse the circular singly linked list until we
reach the same node where we started. The circular singly linked list has
no beginning or end. No null value is present in the next part of any of the
nodes.
Circular Doubly Linked List has properties of both doubly linked list and
circular linked list in which two consecutive elements are linked or
connected by the previous and next pointer and the last node points to the
first node by the next pointer and also the first node points to the last
node by the previous pointer.
In the above program one, two, and three are the node with values 3, 5,
and 9 respectively which are connected in a circular manner as:
● For Node One: The Next pointer stores the address of Node two.
● For Node Two: The Next stores the address of Node three
2. Deletion
And then,
2) Insertion at the end of the list: To insert a node at the end of the list,
follow these steps:
● Create a node, say T.
● last = T.
Before insertion,
Circular linked list before insertion of node at the end
After insertion,
● Search for the node after which T needs to be inserted, say that
node is P.
● P -> next = T.
Suppose 12 needs to be inserted after the node has the value 10,
Circular linked list before insertion
1) Delete the node only if it is the only node in the circular linked list:
● Free the node’s memory
3) Delete any node from the circular linked list: We will be given a node
and our task is to delete that node from the circular linked list.
Algorithm:
Case 1: List is empty.
● If the list is empty we will simply return.
#include <stdio.h>
#include <stdlib.h>
*head_ref = ptr1;
}
// If head is to be deleted
if ((*head)->data == key) {
deleteNode(&head, 7);
return 0;
}
DOUBLY LINKED LIST:
A doubly linked list (DLL) is a special type of linked list in which each node contains
a pointer to the previous node as well as the next node of the linked list.
struct Node {
int data;
3. Make the next of new_node point to the current head of the doubly
linked list.
Below is the implementation of the 5 steps to insert a node at the front of the
linked list:
// 1. allocate node
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
We are given a pointer to a node as prev_node, and the new node is inserted
after the given node. This can be done using the following 6 steps:
new_node.
if (prev_node == NULL) {
return;
new_node->data = new_data;
new_node->next = prev_node->next;
// 4. Make the next of prev_node as new_node
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
Let the pointer to this given node be next_node. This can be done using the
following 6 steps.
the next_node.
head node.
Implementation
if (next_node == NULL) {
return;
}
// 1. Allocate new node
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
head = new_node;
list.
// 1. allocate node
new_node->data = new_data;
new_node->next = NULL;
// node as head
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return;
last = last->next;
last->next = new_node;
new_node->prev = last;
return;