# Data Structure – codewindow.in

## Related Topics ## Data Structure

#### Here is an example implementation of a queue using a linked list in Python:

``````class Node:
def __init__(self, data):
self.data = data
self.next = None

class Queue:
def __init__(self):
self.tail = None

def is_empty(self):

def enqueue(self, data):
new_node = Node(data)
if self.is_empty():
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node

def dequeue(self):
if self.is_empty():
return None
self.tail = None
return data
``````

#### Here's the sample code for deleting a node from a doubly linked list in C:

``````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

// 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);
}
``````

#### Here is an example implementation of a circular linked list in Python:

``````class Node:
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def insert(self, data):
new_node = Node(data)
else:
current = current.next
current.next = new_node

def delete(self, data):
return
current = current.next
else:
prev = None
prev = current
current = current.next
if current.data == data:
prev.next = current.next
break

def print_list(self):
return
print(current.data, end=' ')
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.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
``````

4. #### Insertion and deletion time: Inserting or deleting an element in the middle of a dynamic array requires shifting all elements after the insertion or deletion point, which takes linear time O(n). In a linked list, inserting or deleting a node requires changing pointers, which takes constant time O(1). #### Questions on Chapter 4  #### Questions on Chapter 5  ## We Loveto Support you

Go through our study material. Your Job is awaiting.