Created
August 5, 2014 13:45
-
-
Save selfboot/32b0c8747b8098e1d657 to your computer and use it in GitHub Desktop.
Stack的简单实现。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# This is a Stack implementing in list. | |
class Stack(): | |
def __init__(self): | |
self._content = list() | |
def __len__(self): | |
return len(self._content) | |
def _is_empty(self): | |
return len(self) == 0 | |
def __str__(self): | |
return str(self._content) | |
def push(self, element): | |
self._content.append(element) | |
def pop(self): | |
assert not self._is_empty() | |
self._content.pop() | |
def peek(self): | |
assert not self._is_empty() | |
return self._content[-1] | |
# The following shows how to use class Stack | |
if __name__ == "__main__": | |
test_stack = Stack() | |
for i in range(5): | |
test_stack.push(i ** 2) | |
print test_stack._is_empty | |
print "Operation peek: ", test_stack.peek() | |
test_stack.pop() | |
print "After Operation pop: ", test_stack | |
''' | |
for i in range(5): | |
test_stack.pop() | |
print test_stack | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment