Python learning Data Structure instance code, python Data Structure instance

Source: Internet
Author: User

Python learning Data Structure instance code, python Data Structure instance

When learning python, it is used to practice code and review the data structure

#coding:utf-8#author:Elvis class Stack(object):   def __init__(self, size=8):    self.stack = []    self.size = size    self.top = -1   def is_empty(self):    if self.top == -1:      return True    else:      return False   def is_full(self):    if self.top +1 == self.size:      return True    else:      return False   def push(self, data):    if self.is_full():      raise Exception('stackOverFlow')    else:      self.top += 1      self.stack.append(data)   def stack_pop(self):    if self.is_empty():      raise Exception('stackIsEmpty')    else:      self.top -= 1      return self.stack.pop()    def stack_top(self):    if self.is_empty():      raise Exception('stackIsEmpty')    else:      return self.stack[self.top]   def show(self):    print self.stack stack = Stack()stack.push(1)stack.push(2)stack.push('a')stack.push('b')stack.push(5)stack.push(6)stack.stack_pop()stack.stack_pop()stack.stack_top()stack.is_empty()stack.is_full()stack.show()

The above is all the content shared in this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.