python实现栈、队列、链表的插入、删除以及遍历

栈:先进后出

队列:先进先出

单向链表:先进先出(自己可以设置)

代码如下:


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()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值