Home Stack
Post
Cancel

Stack

๐Ÿ“š Stack ๊ตฌํ˜„

๊ธฐ๋Šฅ

EventDescription
push(data)๋งจ ์•ž์— ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ
pop()๋งจ ์•ž์˜ ๋ฐ์ดํ„ฐ ๋ฝ‘๊ธฐ
peek()๋งจ ์•ž์˜ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ
isEmpty()์Šคํƒ์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€ ๋ฐ˜ํ™˜

LIFO


๊ตฌํ˜„

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
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class Stack:
    def __init__(self):
        self.head = None

    def push(self, item):
        new_head = Node(item)
        new_head.next = self.head
        self.head = new_head

    def pop(self):
        if self.isEmpty():
            return "Stack is Empty"
        item = self.head
        self.head = self.head.next
        return item

    def peek(self):
        if self.isEmpty():
            return "Stack is Empty"
        return self.head.data

    def isEmpty(self):
        return self.head is None



This post is licensed under CC BY 4.0 by the author.