-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedDeque.py
56 lines (45 loc) · 1.7 KB
/
LinkedDeque.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
import Node
import LinkedQueue
class LinkedDeque(LinkedQueue.LinkedQueue):
def __init__(self): # initialize
super().__init__()
def __str__(self): # string value of the Deque
return(super().__str__())
def add_front(self, item): # adds item to the front of the Deque
node = Node.Node(item)
if self.is_empty():
self.top = node
self.rear = node
self.length += 1
else:
prevTop = self.top
self.top = node
self.top.set_next(prevTop)
self.length += 1
def add_rear(self, item): # adds item to the end of the deque
super().enqueue(item)
def remove_front(self): # removes and returns the front
return(super().dequeue())
def remove_rear(self): # removes and returns the rear
checknode = self.top
previousNode = checknode
if checknode.get_next() == None:
self.rear = None
self.top = None
self.length = self.length - 1
else:
while(checknode.get_next() is not None):
previousNode = checknode
checknode = checknode.get_next()
self.length = self.length - 1
self.rear = previousNode
previousNode.set_next(None)
return checknode.get_data()
def get_front(self): # returns but does not remove the front
return(super().first())
def get_rear(self): # returns but does not remove the rear
return(self.rear.get_data())
def size(self): # returns the size of the Deque object
return(super().size())
def is_empty(self): # check to see if Deque object is empty
return(super().is_empty())