数据结构之堆栈和队列习题
一、Implement Queue using Stacks
1 class QueueWithTwoStacks: 2 def __init__(self): 3 self.insertStack = [] 4 self.popStack = [] 5 def enqueue(self, e): 6 self.insertStack.append(e) 7 return e 8 def dequeue(self): 9 if len(self.insertStack) == 0 and len(self.popStack) == 0: 10 return None 11 if len(self.popStack) == 0: 12 while len(self.insertStack) != 0: 13 self.popStack.append(self.insertStack.pop()) 14 return self.popStack.pop()
二、Implement Stack using Queues
1 class StackWithQueue: 2 def __init__(self): 3 self.queue = LinkedList() 4 def push(self, x): 5 self.queue.add_last(x) 6 def pop(self): 7 size = self.queue.size() 8 for i in range(1, size): 9 self.queue.add_last(self.queue.remove_first()) 10 self.queue.remove_first() 11 def top(self): 12 size = self.queue.size() 13 for i in range(1, size): 14 self.queue.add_last(self.queue.remove_first()) 15 result = self.queue.remove_first() 16 self.queue.add_last(result) 17 return result
三、Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
getMin() -- Retrieve the minimum element in the stack.
1 class MinStack(): 2 def __init__(self): 3 self.stack = [] 4 def push(self, v): 5 if len(self.stack) == 0: 6 self.stack.append([v, v]) 7 else: 8 if v < self.stack[-1][1]: 9 self.stack.append([v, v]) 10 else: 11 self.stack.append([v, self.stack[-1][1]]) 12 def pop(self): 13 if len(self.stack) == 0: 14 return 'ValueError' 15 res = self.stack.pop() 16 return res[0] 17 def getMin(self): 18 return self.stack[-1][1]
四、TwoStackWithOneArray
Describe how you could use a single array to implement two stacks
1 class twoStacks: 2 def __init__(self, n): 3 self.size = n 4 self.arr = [None] * n 5 self.top1 = -1 6 self.top2 = self.size 7 8 def push1(self, x): 9 if self.top1 < self.top2 - 1: 10 self.top1 += 1 11 self.arr[self.top1] = x 12 else: 13 print("Stack Overflow") 14 15 def push2(self, x): 16 if self.top2 > self.top1 + 1: 17 self.top2 -= 1 18 self.arr[self.top2] = x 19 else: 20 print("Stack Overflow") 21 22 def pop1(self): 23 if self.top1 >= 0: 24 x = self.arr[self.top1] 25 self.top1 -= 1 26 return x 27 else: 28 print("Stack Underflow") 29 30 def pop2(self): 31 if self.top2 < self.size: 32 x = self.arr[self.top2] 33 self.top2 += 1 34 return x 35 else: 36 print("Stack Underflow") 37 38
五、Three Stack with One Array
Describe how you could use a single array to implement three stacks
六、Stack Sorting*****************
Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push | pop | peek | isEmpty.
1 def sortStack(s): 2 3 r = [] 4 5 while len(s) > 0: 6 tmp = s.pop() 7 8 while len(r) > 0 and r[-1] > tmp: 9 s.append(r.pop()) 10 11 r.append(tmp) 12 13 return r 14 15 s = [5, 6, 7, 8, 9] 16 print(sortStack(s))
1 def sortedInsert(s, x): 2 if len(s) == 0 or x > s[-1]: 3 s.append(x) 4 return 5 temp = s.pop() 6 sortedInsert(s, x) 7 s.append(temp) 8 9 def sortStack(s): 10 if len(s) != 0: 11 x = s.pop() 12 sortStack(s) 13 sortedInsert(s, x)