In: Computer Science
python
class Node():
def __init__(self, value):
pass
class PostScript():
def __init__(self):
pass
def push(self, value):
pass
def pop(self):
return None
def peek(self):
pass
def is_empty(self):
pass
def stack(self):
pass
def exch(self):
pass
def index(self):
pass
def clear(self):
pass
def dup(self):
pass
def equal(self):
pass
def depth(self):
pass
stack = PostScript()
def decode(command_list):
for object in command_list:
if object == '=':
stack.equal()
elif object == 'count':
stack.depth()
elif object == 'clear':
stack.clear()
elif object == 'dup':
stack.dup()
elif object == 'exch':
stack.exch()
elif object == 'index':
stack.index()
elif object == 'pop':
stack.pop()
elif object == 'stack':
stack.stack()
else:
stack.push(object)
command = input()
command_list = command.split()
decode(command_list)
For this assignment you are to implement a PostScript command
interpreter using a Stack. You must implement the stack using a
singly-linked list. Solutions that use a data structure other than
a singly-linked list will received no
credit.
The interpreter must read and execute a string of PostScript
commands based on the following definitions:
1. =
objn objn-1 … obj1 = : objn-1 … obj1
This command removes the topmost object form the stack and
prints it.
class Node:
def __init__(self,value):
self.value = value
self.next = None
class PostScript:
def __init__(self):
self.top = None
def push(self,value):
node = Node(value)
if self.top == None:
self.top = node
else:
node.next = self.top
self.top = node
def pop(self):
if self.top != None:
self.top = self.top.next
def peek(self):
if self.top !=None:
return self.top.value
else:
return None
def is_empty(self):
return(self.top == None)
def stack(self):
if self.is_empty():
print("Empty Stack")
else:
print("\n Stack:")
node = self.top
while(node != None):
print(node.value)
node = node.next
def exch(self):
if(self.top != None and self.top.next != None):
top1 = self.top
self.top = self.top.next
top2 = self.top
self.top = self.top.next
top1.next = self.top
self.top = top1
top2.next = self.top
self.top = top2
def index(self, a):
curr = self.top
i=1
while(i < a and curr != None):
curr = curr.next
i = i + 1
if(i == a):
node = Node(curr.value)
node.next = self.top
self.top = node
def clear(self):
self.top = None
def dup(self):
if(self.top != None):
node = Node(self.top.value)
node.next = self.top
self.top = node
def equal(self):
if self.top != None:
node = self.top
self.top = self.top.next
print(node.value)
else:
print('None')
def depth(self):
if(self.is_empty()):
self.push(0)
else:
count = 0
node = self.top
while(node != None):
count = count + 1
node = node.next
self.push(count)
stack = PostScript()
def decode(command_list):
i=0
while (i < len(command_list)):
#print(command_list[i])
if command_list[i] == '=':
stack.equal()
elif command_list[i] == 'count':
stack.depth()
elif command_list[i] == 'clear':
stack.clear()
elif command_list[i] == 'dup':
stack.dup()
elif command_list[i] == 'exch':
stack.exch()
elif command_list[i] == 'index':
i = i + 1
print(command_list[i])
stack.index(int(command_list[i]))
elif command_list[i] == 'pop':
stack.pop()
elif command_list[i] == 'stack':
stack.stack()
else:
stack.push(command_list[i])
i = i + 1
command = raw_input()
command_list = command.split()
decode(command_list)
#end of program
Code Screenshot: