栈:先进后出
队列:先进先出
单向链表:先进先出(自己可以设置)
代码如下:
class Stack():
stack = []
def stackPush(self):
self.stack.append(input("enter your string:").strip())
def stackIsEmpty(self):
if self.stack[0] == None:
return True
else:
return False
def stackPop(self):
if self.stackIsEmpty() == True:
print("The stack is empty,can not pop")
else:
return self.stack.pop()
def viewStack(self):
print(self.stack)
def printHelpMessage(self):
print("stack help message###############\n \
p:Push\n \
o:Pop\n \
v:view\n \
q:Quit")
def sqlMain(self):
selectFunc = {"p":self.stackPush, "o":self.stackPop, "v":self.viewStack}
while True:
while True:
try:
yourChoice = input("enter your choice:").strip()[0].lower()
except(EOFError, IndentationError, KeyboardInterrupt):
yourChoice = "q"
print("your choice is %s"%(yourChoice))
if yourChoice not in "povq":
print("wrong picked, try again")
else:
break
if yourChoice == "q":
break
else:
selectFunc[yourChoice]()
class Queue():
queue = []
def queuePush(self):
self.queue.append(input("enter your queue string:").strip())
def queueIsEmpty(self):
if self.queue[-1] == None:
return True
else:
return False
def queuePop(self):
if self.queueIsEmpty() == True:
print("queue is already empty")
else:
return Queue.queue.pop(0)
def viewQueue(self):
print(Queue.queue)
def printHelpMessage(self):
print("Queue Help Message##############\n \
p:queuePush\n \
o:queuePop\n \
v:viewQueue\n \
q:Quie")
def sqlMain(self):
selectFunc = {"p":self.queuePush, "o":self.queuePop, "v":self.viewQueue}
print("Using Queue Tool Now")
while True:
while True:
try:
yourChoice = input("enter your choice:").strip()[0].lower()
except(EOFError, KeyboardInterrupt, IndentationError):
choice == "q"
print("your choice is :%s"%(yourChoice))
if yourChoice not in "povq":
print("Invalid option, try again")
else:
break
if yourChoice == "q":
break
else:
selectFunc[yourChoice]()
class Line():
line = []
def linePush(self):
self.line.append(input("enter your line string:").strip())
def lineIsEmpty(self):
if self.line[0] == None:
return True
else:
return False
def linePop(self):
return self.line.pop(0)
def viewLine(self):
print(self.line)
def printHelpMessage(self):
print("Line Help Message#############\n \
p:linePush\n \
o:linePop\n \
v:viewLine\n \
q:Quit")
def sqlMain(self):
selectFunc = {"p":self.linePush, "o":self.linePop, "v":self.viewLine}
while True:
while True:
try:
yourChoice = input("enter your choice:").strip()[0].lower()
except(EOFError, KeyboardInterrupt, IndentationError):
choice == "q"
print("your choice is :%s"%(yourChoice))
if yourChoice not in "povq":
print("Invalid option, try again")
else:
break
if yourChoice == "q":
break
else:
selectFunc[yourChoice]()
def HelpMessage():
print("Tool Help Message##########\n \
s:Stack()\n \
q:Queue()\n \
l:Line()\n \
x:Quit")
def main():
selectClass = {"s":Stack, "q":Queue, "l":Line}
while True:
while True:
try:
yourChoice = input("enter your choice:").strip()[0].lower()
except(EOFError, KeyboardInterrupt, IndentationError):
choice == "x"
print("your choice is :%s"%(yourChoice))
if yourChoice not in "sqlx":
print("Invalid option, try again")
else:
break
if yourChoice == "x":
break
else:
return selectClass[yourChoice]()
if __name__ == "__main__":
HelpMessage()
test = main()
test.printHelpMessage()
test.sqlMain()