Question: python zyDE 4 . 1 0 . 1 : Doubly linked - list data structures and algorithms. from Node import Node from LinkedList import LinkedList
python zyDE : Doubly linkedlist data structures and algorithms.
from Node import Node
from LinkedList import LinkedList
numlist LinkedList
nodea Node
nodeb Node
nodec Node
noded Node
nodee Node
nodef Node
numlist.appendnodea # Add
numlist.appendnodeb # Add make the tail
numlist.appendnodec # Add make the tail
numlist.prependnoded # Add make the head
numlist.insertafternodeb nodee # Insert after
numlist.insertafternodec nodef # Insert after tail, becomes new tail
# Output list
printList after adding nodes: end
node numlist.head
while node None:
printnodedata, end
node node.next
print
numlist.removenodef # Remove the tail
numlist.removenoded # Remove the head
# Output final list
printList after removing nodes: end
node numlist.head
while node None:
printnodedata, end
node node.next
print
class Node:
def initself initialdata:
self.data initialdata
self.next None
self.prev None
class LinkedList:
def initself:
self.head None
self.tail None
def appendself newnode:
if self.head None:
self.head newnode
self.tail newnode
else:
self.tail.next newnode
newnode.prev self.tail
self.tail newnode
def prependself newnode:
if self.head None:
self.head newnode
self.tail newnode
else:
newnode.next self.head
self.head.prev newnode
self.head newnode
def insertafterself currentnode, newnode:
if self.head is None:
self.head newnode
self.tail newnode
elif currentnode is self.tail:
self.tail.next newnode
newnode.prev self.tail
self.tail newnode
else:
successornode currentnode.next
newnode.next successornode
newnode.prev currentnode
currentnode.next newnode
successornode.prev newnode
def removeself currentnode:
successornode currentnode.next
predecessornode currentnode.prev
if successornode is not None:
successornode.prev predecessornode
if predecessornode is not None:
predecessornode.next successornode
if currentnode is self.head:
self.head successornode
if currentnode is self.tail:
self.tail predecessornode
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
