0% found this document useful (0 votes)
20 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)
20 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

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):
[Link] = data
[Link] = None
[Link] = None

class DoublyLinkedList:
def __init__(self):
[Link] = None

def append(self, data):


new_node = Node(data)
if [Link] is None:
[Link] = new_node
return
last = [Link]
while [Link]:
last = [Link]
[Link] = new_node
new_node.prev = last

def prepend(self, data):


new_node = Node(data)
new_node.next = [Link]
if [Link]:
[Link] = new_node
[Link] = new_node

def delete(self, key):


temp = [Link]
while temp:
if [Link] == key:
if [Link]:
[Link] = [Link]
if [Link]:
[Link] = [Link]
if temp == [Link]:
[Link] = [Link]
return
temp = [Link]

def display_forward(self):
temp = [Link]
while temp:
print([Link], end=" ")
temp = [Link]

def display_backward(self):
temp = [Link]
if not temp:
return
while [Link]:
temp = [Link]
while temp:
print([Link], end=" ")
temp = [Link]

---

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