-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSC162_Lab06_SBK.py
149 lines (116 loc) · 3.57 KB
/
CSC162_Lab06_SBK.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# sarah kingan
# CSC162
# summer 2015
# LAB06
#
# some code from Miller and Ranum (2014)
# and from lecture 3 notes
#
# http://www.codeskulptor.org/#user40_m929ZLvYFY_24.py
#
# INSTRUCTIONS: Using your own doubly linked list class
# rather than a Python list implement a Queue.
#
# Test your stack by running the hotpotato simulation
# from the text.
#
# def hotPotato(namelist, num):
class DNode:
def __init__(self):
self.data = None
self.next = None
self.prev = None
def getData(self):
return self.data
def getNext(self):
return self.next
def getPrev(self):
return self.prev
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
def setPrev(self,newprev):
self.prev = newprev
class DLinkedList:
def __init__(self):
self.head = DNode()
self.tail = DNode()
self.head.setNext(self.tail)
self.tail.setPrev(self.head)
def printForward(self):
current = self.head.getNext()
while current != self.tail:
print(current.getData())
current = current.getNext()
def printReverse(self):
current = self.tail.getPrev()
while current != self.head:
print current.getData()
current = current.getPrev()
def addAtHead(self,item):
tmp = DNode()
tmp.setData(item)
tmp.setPrev(self.head)
tmp.setNext(self.head.getNext())
self.head.getNext().setPrev(tmp)
self.head.setNext(tmp)
def addAtTail(self,item):
tmp = DNode()
tmp.setData(item)
tmp.setNext(self.tail)
tmp.setPrev(self.tail.getPrev())
self.tail.getPrev().setNext(tmp)
self.tail.setPrev(tmp)
def popHead(self):
if self.head.getNext() == self.tail:
return None
tmp = self.head.getNext()
self.head.setNext(tmp.getNext())
tmp.getNext().setPrev(self.head)
return tmp.getData()
def popTail(self):
if self.tail.getPrev() == self.head:
return None
tmp = self.tail.getPrev()
self.tail.setPrev(tmp.getPrev())
tmp.getPrev().setNext(self.tail)
return tmp.getData()
def rmNode(self,node):
node.getNext().setPrev(node.getPrev())
node.getPrev().setNext(node.getNext())
def remove(self,item):
current = self.head.getNext()
while current != self.tail:
if current.getData() == item:
self.rmNode(current)
return current.getData()
else:
current = current.getNext()
return None
class Queue:
def __init__(self):
self.dlist = DLinkedList()
def isEmpty(self):
return self.dlist.head.getNext() == self.dlist.tail
def enqueue(self, item):
self.dlist.addAtHead(item)
def dequeue(self):
return self.dlist.popTail()
def size(self):
size = 0
current = self.dlist.head.getNext()
while current != self.dlist.tail:
size = size +1
current = current.getNext()
return size
def hotPotato(namelist, num):
simqueue = Queue()
for name in namelist:
simqueue.enqueue(name)
while simqueue.size() > 1:
for i in range(num):
simqueue.enqueue(simqueue.dequeue())
simqueue.dequeue()
return simqueue.dequeue()
print(hotPotato(["Bill","David","Susan","Jane","Kent","Brad"],7))