Python 使用单链表实现堆栈 (基于class, 包含迭代器)

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2015-1-28
@author: beyondzhou
@name: test_linkliststack.py
'''

def test_linkliststack():

    # import linkListStack
    from mystack import LinkListStack

    print '#Init a stack named smith using push'
    smith = LinkListStack()
    smith.push('CSCI-112')
    smith.push('MATH-121')
    smith.push('HIST-340')
    smith.push('ECON-101')

    print '\n#output smith stack'
    for element in smith:
        print element

    print '\n#pop one item'
    smith.pop()

    print '\n#output smith stack after pop'
    for element in smith:
        print element 

    print '\n#get the peek item'
    peek_item = smith.peek()
    print 'peek item is ', peek_item

    print '\n#get the length of stack'
    print 'the lenght of stack is ', len(smith)

    print '\n#check wheter the stack is empty'
    if smith.isEmpty():
        print 'stack is empty!'
    else:
        print 'stack is not empty!'

    print '\n#pop all items'
    while not smith.isEmpty():
        smith.pop()

    print '\n#check wheter the stack is empty after pop all items'
    if smith.isEmpty():
        print 'stack is empty!'
    else:
        print 'stack is not empty!'

if __name__ == "__main__":
    test_linkliststack()
# Implementation of the Stack ADT using a singly linked list
class LinkListStack:
    # Creates an empty stack
    def __init__(self):
        self._top = None
        self._size = 0

    # Returns True if the stack is empty or False otherwise
    def isEmpty(self):
        return self._top is None

    # Returns the number of items in the stack
    def __len__(self):
        return self._size

    # Returns the top item on the stack without removing it
    def peek(self):
        assert not self.isEmpty(), "Cannot peek at an empty stack"
        return self._top.item

    # Removes and returns the top item on the stack
    def pop(self):
        assert not self.isEmpty(), "Cannot pop from an empty stack"
        node = self._top
        self._top = self._top.next
        self._size -= 1
        print 'pop item:', node.item
        return node.item

    # Pushes an item onto the top of the stack
    def push(self, item):
        self._top = _StackNode(item, self._top)
        self._size += 1

    def __iter__(self):
        return _LinkStackIterator(self._top)

# The private storage class for creating stack nodes
class _StackNode:
    def __init__(self, item, link):
        self.item = item
        self.next = link

# Implementation of linked list stack iter
class _LinkStackIterator:
    def __init__(self, listHead):
        self._curNode = listHead  

    def __iter__(self):
        return self  

    def next(self):
        if self._curNode is None:
            raise StopIteration
        else:
            item = self._curNode.item
            self._curNode = self._curNode.next
            return item  
#Init a stack named smith using push

#output smith stack
ECON-101
HIST-340
MATH-121
CSCI-112

#pop one item
pop item: ECON-101

#output smith stack after pop
HIST-340
MATH-121
CSCI-112

#get the peek item
peek item is  HIST-340

#get the length of stack
the lenght of stack is  3

#check wheter the stack is empty
stack is not empty!

#pop all items
pop item: HIST-340
pop item: MATH-121
pop item: CSCI-112

#check wheter the stack is empty after pop all items
stack is empty!

时间: 2024-11-05 18:33:29

Python 使用单链表实现堆栈 (基于class, 包含迭代器)的相关文章

Python 使用单链表实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-3 @author: beyondzhou @name: test_linklistqueue.py ''' def test_linklistqueue(): # import pyListQueue from myqueue import linkListQueue print '#Init a queue named smith using enqueue' sm

Python 使用list实现堆栈 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-27 @author: beyondzhou @name: test_liststack.py ''' def test_liststack(): # import listStack from mystack import listStack print '#Init a stack named smith using push' smith = listStack(

Python 使用循环数组实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-2 @author: beyondzhou @name: test_arryqueue.py ''' def test_arryqueue(): # import pyListQueue from myqueue import arrayQueue print '#Init a queue named smith using enqueue' smith = array

Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-6 @author: beyondzhou @name: test_bpriorityqueue.py ''' def test_bpriorityqueue(): # import pyListQueue from myqueue import BPriorityQueue print '#Init a queue named smith using enqueue'

python实现单链表的反转

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env python #coding = utf-8 class Node:     def __init__(self,data=None,next = None):         self.data = data         self.next = next def rev(link):     pre = link     cur = l

python 实现单链表

#! /usr/bin/env python ### ### Linked List python implementation ### ### @reference Data Structures and Algorithm Analysis in C:Second Edition : Mark Allen Weiss ### @date Tue Sep 29 20:51:55 CST 2015 #node structure class Node(object): def __init__(

python实现单链表

#encoding:utf-8 import sys class Lnode():     def __init__(self,elem,next=None):         self.elem = elem    #节点的值         self.next = next    #指向下一个节点     def getelem(self):         return self.elem     def getnext(self):         return self.next cl

Python 使用list实现无边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-4 @author: beyondzhou @name: test_listpriorityqueue.py ''' def test_listpriorityqueue(): # import pyListQueue from myqueue import ListPriorityQueue print '#Init a queue named smith using

Python 使用list实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-27 @author: beyondzhou @name: test_listqueue.py ''' def test_listqueue(): # import pyListQueue from myqueue import pyListQueue print '#Init a queue named smith using enqueue' smith = pyL