Stack - Python

생성일: 2021년 9월 29일 오후 4:53

#StackType.py
MAX_ITEMS = 100

class StackType:
    def __init__(self):
        self.info = [0] * MAX_ITEMS
	      self.sp = -1    #stack pointer

    def is_empty(self):
        '''[1]'''
        return (self.sp == -1)
        
    def is_full(self):
        '''[2]'''
        return (self.sp == MAX_ITEMS - 1)
        
    def push(self, item):
        '''[3]'''
        if (self.is_full()):
            return "Stack is Full"
        self.sp += 1
        self.info[self.sp] = item

    def pop(self):
        '''[4]'''
        if(self.is_empty()):
            return "Stack is Empty"
        self.sp -= 1

    def top(self):
        '''[5]'''
        if(self.is_empty()):
            return "Stack is Empty"
        return self.info[self.sp]
#testStack.py
import os
from StackType import *

if __name__ == '__main__':
    my_stack = StackType()
    
    for i in range(5):
        number = int(input("enter the number: "))
        my_stack.push(number)
    
    print()
    print(my_stack.is_full())
    print()
    
    while (True):
        if (my_stack.is_empty() == True):
            break
        else:
            print(my_stack.top())
            my_stack.pop()

    print()
    print(my_stack.top())

좋은 웹페이지 즐겨찾기