In [1]:
class Node:
def __init__(self, data, prev = None, next = None):
self.data = data
self.prev = prev
self.next = next
class List:
def __init__(self, head = None):
self.head = head
In [15]:
def listSearch(L, k):
"""A simple linear search through a List L for a key k"""
cur = L.head
while cur is not None and cur.data != k.data:
cur = cur.next
return cur
In [3]:
def insert(L, k):
if L.head is not None:
k.next = L.head
L.head.prev = k
L.head = k
In [21]:
def delete(L, k):
if k.prev is not None:
k.prev.next = k.next
else:
L.head = k.next
if k.next is not None:
k.next.prev = k.prev
In [4]:
def printList(L):
cur = L.head
while cur is not None:
print(cur.data)
cur = cur.next
In [22]:
myList = List(Node("First"))
printList(myList)
insert(myList, Node("Second"))
printList(myList)
nodeToDelete = listSearch(myList, Node("Second"))
print("To delete: ", nodeToDelete.data)
delete(myList, nodeToDelete)
printList(myList)
In [ ]: