7JeY world

[자료구조 / Python] Stack 본문

Programming/Data Structure

[자료구조 / Python] Stack

7JeY 2021. 4. 11. 15:56
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Stack:
    #리스트를 이용하여 스택 생성
    def __init__ (self):
        self.top = []
    
    #스택의 크기를 출력
    def __len__(self):
        return len(self.top)
 
    #스택 내부 자료를 string으로 변환하여 반환
    def __str__(self):
        return str(self.top[::1])
    
    #스택 초기화
    def clear(self):
        self.top=[]
 
    #PUSH
    def push (self, item):
        self.top.append(item)
 
    #POP
    def pop(self):
        #if Stack is not empty
        if not self.isEmpty():
            #pop and return 
            return self.top.pop(-1)
        else:
            print("Stack underflow")
            exit()
    
    #자료가 포함되어 있는지 여부 반환
    def isContain(self, item):
        return item in self.top
    
    #스택에서 top의 값을 읽어온다
    def peek(self):
        if not self.isEmpty():
            return self.top[-1]
        else:
            print("underflow")
            exit()
 
    #스택이 비어있는지 확인
    def isEmpty(self):
        return len(self.top)==0
    
    #스택 크기 반환
    def size(self):
        return len(self.top)
    
    #iterator를 이용하여 스택 출력
    def __iter__(self):
        return _StackIterator(self.top)
 
#Iterator
class _StackIterator:
    def __init__(self, theList):
        self._items = theList
        self._curItem = 0
 
    def __iter__(self):
        return self
    
    def __next__(self):
        if self._curItem < len(self._items):
            item = self._items[self._curItem]
            self._curItem+=1
            return item
        else:
            raise StopIteration
cs
반응형
Comments