SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node:
  2.     def __init__(self, data):
  3.        self.data = data
  4.        self.next = None
  5.  
  6. class Stack:
  7.     def __init__(self):
  8.         self.head = None
  9.  
  10.     def push(self, data):
  11.         if self.head is None:
  12.             self.head = Node(data)
  13.         else:
  14.             new_node = Node(data)
  15.             new_node.next = self.head
  16.             self.head = new_node
  17.  
  18.     def pop(self):
  19.         if self.head is None:
  20.             return None
  21.         else:
  22.             popped = self.head.data
  23.             self.head = self.head.next
  24.             return popped
  25.  
  26. a_stack = Stack()
  27. while True:
  28.     print('push <value>')
  29.     print('pop')
  30.     print('quit')
  31.     do = input('What would you like to do? ').split()
  32.  
  33.     operation = do[0].strip().lower()
  34.     if operation == 'push':
  35.         a_stack.push(int(do[1]))
  36.     elif operation == 'pop':
  37.         popped = a_stack.pop()
  38.         if popped is None:
  39.             print('Stack is empty.')
  40.         else:
  41.             print('Popped value: ', int(popped))
  42.     elif operation == 'quit':
  43.         break
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top