-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmergeableStack.py
More file actions
executable file
·82 lines (64 loc) · 1.56 KB
/
Copy pathmergeableStack.py
File metadata and controls
executable file
·82 lines (64 loc) · 1.56 KB
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
#!/usr/bin/env python2.7
#node object
class Node:
def __init__(self, data):
self.data = data
self.next = None
#mergeable stack
# last mean last out
class mergeableStack:
def __init__(self):
self.last = None
self.count = 0
def push(self, n):
if self.count == 0:
self.last = n
self.last.next = n
else:
head = self.last.next
self.last.next = n
n.next = head
self.count += 1
def pop(self):
item = self.last.next
self.last.next = item.next
self.count -= 1
return item.data
def merge(self, s):
head = self.last.next
self.last.next = s.last.next
s.last.next = head
self.last = s.last
self.count += s.count
def printStack(self):
print "\nhead:" + str(self.last.next.data),
print "last:" + str(self.last.data)
i = self.count
node = self.last.next
while(i > 0):
print str(node.data),
if i > 1:
print "->",
node = node.next
i -= 1
if __name__ == "__main__":
ms = mergeableStack()
ms.push(Node(10))
ms.push(Node(20))
ms.push(Node(40))
ms.push(Node(30))
ms.push(Node(80))
ms.printStack()
ms.pop()
ms.pop()
ms.printStack()
ms2 = mergeableStack()
ms2.push(Node(100))
ms2.push(Node(200))
ms2.push(Node(400))
ms2.push(Node(300))
ms2.printStack()
ms.merge(ms2)
ms.printStack()
ms.pop()
ms.printStack()