0% found this document useful (0 votes)
0 views1 page

linked_lists_compact (1)

The document provides an overview of various linked list data structures, including singly linked lists, doubly linked lists, circularly linked lists, and circular doubly linked lists. It outlines their characteristics, advantages, and basic code implementations for insertion, deletion, and traversal. Each type of linked list has unique properties that make them suitable for different applications in data management.

Uploaded by

rptclown143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views1 page

linked_lists_compact (1)

The document provides an overview of various linked list data structures, including singly linked lists, doubly linked lists, circularly linked lists, and circular doubly linked lists. It outlines their characteristics, advantages, and basic code implementations for insertion, deletion, and traversal. Each type of linked list has unique properties that make them suitable for different applications in data management.

Uploaded by

rptclown143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

print(current.

data, end=" -> ") return


1. Singly Linked List current = current.next current = self.head
A singly linked list is a data structure where each node contains: print("None") while current.next:
- A data field to store the value. current = current.next
- A next pointer to the next node in the list. sll = SinglyLinkedList() current.next = new_node
The last node's next pointer is None, indicating the end of the list. sll.insert_at_beginning(10) new_node.prev = current
sll.insert_at_end(20)
Characteristics: sll.insert_at_end(30) def delete_from_beginning(self):
- Traversal is only forward. sll.display() if self.head:
- Insertion and deletion are efficient compared to arrays, as no shifting of elements is required. sll.delete_from_beginning() self.head = self.head.next
sll.display() if self.head:
Code Implementation: self.head.prev = None
class Node: 2. Doubly Linked List
def __init__(self, data): A doubly linked list is a data structure where each node contains: def display_forward(self):
self.data = data - A data field to store the value. current = self.head
self.next = None - A next pointer to the next node. while current:
- A prev pointer to the previous node. print(current.data, end=" -> ")
class SinglyLinkedList: current = current.next
def __init__(self): Characteristics: print("None")
self.head = None - Traversal can be both forward and backward.
- More memory is required compared to a singly linked list due to the additional pointer. def display_backward(self):
def insert_at_beginning(self, data): current = self.head
new_node = Node(data) Code Implementation: if not current:
new_node.next = self.head class Node: print("None")
self.head = new_node def __init__(self, data): return
self.data = data while current.next:
def insert_at_end(self, data): self.next = None current = current.next
new_node = Node(data) self.prev = None while current:
if not self.head: print(current.data, end=" -> ")
self.head = new_node class DoublyLinkedList: current = current.prev
return def __init__(self): print("None")
current = self.head self.head = None
while current.next: dll = DoublyLinkedList()
current = current.next def insert_at_beginning(self, data): dll.insert_at_beginning(10)
current.next = new_node new_node = Node(data) dll.insert_at_end(20)
new_node.next = self.head dll.insert_at_end(30)
def delete_from_beginning(self): if self.head: dll.display_forward()
if self.head: self.head.prev = new_node dll.display_backward()
self.head = self.head.next self.head = new_node dll.delete_from_beginning()
dll.display_forward()
def display(self): def insert_at_end(self, data):
current = self.head new_node = Node(data) 3. Circularly Linked List
while current: if not self.head: A circularly linked list is a variation of a singly linked list where the last node points back to the fir
self.head = new_node

st node, forming a circle. if not self.head: head:


print("List is empty.") self.head = new_node
Characteristics: return new_node.next = new_node
- Traversal can continue indefinitely unless explicitly stopped. current = self.head new_node.prev = new_node
- Efficient for implementing queues and buffers. while True: else:
print(current.data, end=" -> ") tail = self.head.prev
Code Implementation: current = current.next new_node.next = self.head
class Node: if current == self.head: new_node.prev = tail
def __init__(self, data): break self.head.prev = new_node
self.data = data print("(back to head)") tail.next = new_node
self.next = None self.head = new_node
cll = CircularLinkedList()
class CircularLinkedList: cll.insert_at_beginning(10) def delete_from_beginning(self):
def __init__(self): cll.insert_at_beginning(20) if self.head:
self.head = None cll.insert_at_beginning(30) if self.head.next == self.head:
cll.display() self.head = None
def insert_at_beginning(self, data): cll.delete_from_beginning() else:
new_node = Node(data) cll.display() tail = self.head.prev
if not self.head: self.head = self.head.next
self.head = new_node 4. Circular Doubly Linked List self.head.prev = tail
new_node.next = self.head A circular doubly linked list combines the properties of both circular and doubly linked lists. The last node's next points to the first node, and the first tail.next = self.head
else: node's prev points to the last node.
current = self.head def display(self):
while current.next != self.head: Characteristics: if not self.head:
current = current.next - Allows traversal in both directions indefinitely. print("List is empty.")
new_node.next = self.head - Useful in applications like the implementation of advanced data structures. return
current.next = new_node current = self.head
self.head = new_node Code Implementation: while True:
class Node: print(current.data, end=" <-> ")
def delete_from_beginning(self): def __init__(self, data): current = current.next
if self.head: self.data = data if current == self.head:
if self.head.next == self.head: self.next = None break
self.head = None self.prev = None print("(back to head)")
else:
current = self.head class CircularDoublyLinkedList: cdll = CircularDoublyLinkedList()
while current.next != self.head: def __init__(self): cdll.insert_at_beginning(10)
current = current.next self.head = None cdll.insert_at_beginning(20)
current.next = self.head.next cdll.insert_at_beginning(30)
self.head = self.head.next def insert_at_beginning(self, data): cdll.display()
new_node = Node(data) cdll.delete_from_beginning()
def display(self): if not self. cdll.display()

You might also like