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

Bai

The document defines a DoublyLinkedList class with methods to add nodes to the front and back, insert nodes, and print the list forward and backward. It demonstrates adding nodes and printing the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Bai

The document defines a DoublyLinkedList class with methods to add nodes to the front and back, insert nodes, and print the list forward and backward. It demonstrates adding nodes and printing the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

class DoublyNode:

def __init__(self, data):


self.data = data
self.next = None
self.previous = None

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

def append(self, data):


new_node = DoublyNode(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
new_node.previous = current
def push(self, data):
new_node = DoublyNode(data)
new_node.next = self.head
new_node.prev = None
if self.head is not None:
self.head.prev = new_node
self.head = new_node
def insert_after(self, data_after, data):
if self.head is None:
print("List is empty")
return

current = self.head
while current:
if current.data == data_after:
new_node = DoublyNode(data)
new_node.next = current.next
current.next = new_node
new_node.previous = current
if new_node.next:
new_node.next.previous = new_node
return
current = current.next
def print_forward(self):
current = self.head
while current:
print(current.data, end=" ")
current = current.next
print()

def print_backward(self):
current = self.head
while current and current.next:
current = current.next
while current:
print(current.data, end=" ")
current = current.previous
print()
Mylist = DoublyLinkedList()
Mylist.push(1)
Mylist.append(2)
Mylist.push(3)
print(Mylist.head.data)
Mylist.print_forward()
Mylist.insert_after(1,100)
Mylist.print_forward()
class Node:
def __init__(self, data=None):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def insert_at_position(self, data, position):
new_node = Node(data)
if position == 1:
push(self, data)
return
else:
current = self.head
count = 1
while count < position - 1 and current is not None:
current = current.next
count +=1
new_node.next = current.next
current.next = new_node
def inra(self, so):
dem = 1
current = self.head
while dem < so:
current = current.next
dem +=1
print (current.data)
def display(self):
current = self.head
while current:
print(current.data, end= "->")
current = current.next
print("None")
my_list = LinkedList()
my_list.append(1)
my_list.append(2)
my_list.append(3)
my_list.insert_at_position(100,3)
my_list.inra(3)

You might also like