Related Topics

Data Structure
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.head = None
self.tail = None
def is_empty(self):
return self.head is None
def enqueue(self, data):
new_node = Node(data)
if self.is_empty():
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
def dequeue(self):
if self.is_empty():
return None
data = self.head.data
self.head = self.head.next
if self.head is None:
self.tail = None
return data
In this implementation, we have a Node
class to represent a node in the linked list and a Queue
class that uses this node class to implement the queue. The enqueue
method adds a new node to the tail of the linked list, and the dequeue
method removes the node from the head of the linked list.
void deleteNode(struct Node** head_ref, struct Node* del_node)
{
if (*head_ref == NULL || del_node == NULL)
return;
// If node to be deleted is head node
if (*head_ref == del_node)
*head_ref = del_node->next;
// Update previous pointer of next node
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;
// Update next pointer of previous node
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
// Free the memory
free(del_node);
}
Note that in the above code, head_ref
is a pointer to the head node of the doubly linked list, and del_node
is a pointer to the node to be deleted. The function checks if either head_ref
or del_node
is NULL
. If either of them is NULL
, it returns without doing anything.
If the node to be deleted is the head node, the head_ref
is updated to point to the next node. The previous and next pointers of the adjacent nodes are updated to bypass the node to be deleted. Finally, the memory occupied by the node to be deleted is freed using the free()
function.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def insert(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def delete(self, data):
if not self.head:
return
if self.head.data == data:
current = self.head
while current.next != self.head:
current = current.next
current.next = self.head.next
self.head = self.head.next
else:
current = self.head
prev = None
while current.next != self.head:
prev = current
current = current.next
if current.data == data:
prev.next = current.next
break
def print_list(self):
if not self.head:
return
current = self.head
print(current.data, end=' ')
while current.next != self.head:
current = current.next
print(current.data, end=' ')
print()
With this implementation, we can create a circular linked list and perform operations on it as follows:
# create a circular linked list with three nodes
clist = CircularLinkedList()
clist.insert(1)
clist.insert(2)
clist.insert(3)
# print the list
clist.print_list() # output: 1 2 3
# delete the node with value 2
clist.delete(2)
# print the list again
clist.print_list() # output: 1 3




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.