Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views2 pages

Doubly Linked List Implementation

A doubly linked list consists of nodes with data, a pointer to the next node, and a pointer to the previous node, allowing bidirectional traversal. The document provides a Python implementation with methods to append, prepend, delete nodes, and display the list in both forward and backward directions. Key operations include adding nodes at both ends, removing nodes by value, and displaying the list in either direction.

Uploaded by

Pavani Ankala
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)
15 views2 pages

Doubly Linked List Implementation

A doubly linked list consists of nodes with data, a pointer to the next node, and a pointer to the previous node, allowing bidirectional traversal. The document provides a Python implementation with methods to append, prepend, delete nodes, and display the list in both forward and backward directions. Key operations include adding nodes at both ends, removing nodes by value, and displaying the list in either direction.

Uploaded by

Pavani Ankala
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/ 2

Doubly Linked List Operations and Implementation in Python

A doubly linked list is a type of linked list in which each node contains three parts:
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)

This allows traversal in both directions.

---

Implementation in Python:

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

class DoublyLinkedList:
def __init__(self):
self.head = None

def append(self, data):


new_node = Node(data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
new_node.prev = last

def prepend(self, data):


new_node = Node(data)
new_node.next = self.head
if self.head:
self.head.prev = new_node
self.head = new_node

def delete(self, key):


temp = self.head
while temp:
if temp.data == key:
if temp.prev:
temp.prev.next = temp.next
if temp.next:
temp.next.prev = temp.prev
if temp == self.head:
self.head = temp.next
return
temp = temp.next

def display_forward(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next

def display_backward(self):
temp = self.head
if not temp:
return
while temp.next:
temp = temp.next
while temp:
print(temp.data, end=" ")
temp = temp.prev

---

Operations:
1. Append - Adds node at the end
2. Prepend - Adds node at the beginning
3. Delete - Removes node by value
4. Display Forward - Prints list from head to tail
5. Display Backward - Prints list from tail to head

You might also like