-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.py
50 lines (35 loc) · 1.27 KB
/
LinkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pygame, sys, math
from Node import *
class LinkedList:
head = None
def __init__(self):
self.head = None
def add(self, data):
newNode = Node(data, None, None)
# If list is empty, make new node the head
if self.head == None:
self.head = newNode
# Otherwise, add to front
else:
temp = self.head.next
self.head = newNode
temp.prev = newNode
newNode.next = temp
def remove(self, nodeData):
current = self.head
while (current != None):
if (current.data) == nodeData:
break
current = current.next
# Remove current if it is non-null
if current != None:
if current == self.head:
self.head = current.next
elif current.next == None:
current.prev = None
else:
temp = current.next
current.prev.next = temp
temp.prev = current.prev
else:
print "Cannot find node to remove", nodeData