배열로 스택 구현하기

Algorithm 2019. 8. 28. 16:02
반응형

Stack 자료구조 

스택(Stack)은 가장 최근에 넣은 데이터를 먼저 꺼내서 사용하는 선형적인 자료구조(Linear Data Structure)이다.

스택은 흔히 LIFO(Last In First Out)라고 불리는 자료구조로 나중에 저장된 것이 먼저 꺼내지는 구조를 가지고있다.

 

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
 
namespace Application
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("배열로 스택 구현하기");
            var stack = new StackUsingArray();
            stack.Push(1);
            stack.Push(2);
            stack.Push(3);
            stack.PrintStack();
            Console.WriteLine("----------------");
            var pop = stack.Pop();
            Console.WriteLine("pop: {0}", pop);
            Console.WriteLine("----------------");
            stack.PrintStack();
            Console.WriteLine("----------------");
            var peek = stack.Peek();
            Console.WriteLine("peek: {0}", peek);
            Console.WriteLine("----------------");
            stack.PrintStack();
        }
    }
 
    public class StackUsingArray
    {
        private object[] arr;
        private int top;
        public bool IsEmpty {
            get { return this.top == -1; }
        }
 
        public int Capacity {
            get { return this.arr.Length; }
        }
 
        //생성자
        public StackUsingArray(int capacity = 16) {
            arr = new object[capacity];
            top = -1;
        }
 
        public void Push(object data)
        {
            if (top >= arr.Length - 1) {
                //throw하거나 배열확장
                this.ResizeStack();
            }
 
            this.arr[++this.top] = data;
        }
 
        public object Pop() {
            if (this.IsEmpty) {
                throw new ApplicationException("Empty");
            }
 
            var obj = this.arr[top];
            this.arr[top] = null;
            this.top--;
 
            return obj;
        }
 
        public object Peek() {
            if (this.IsEmpty) {
                throw new ApplicationException("Empty");
            }
            return this.arr[this.top];
        }
 
        public void ResizeStack() {
            int capacity = 2 * this.arr.Length;
            var tempArray = new object[capacity];
            Array.Copy(this.arr, tempArray, this.arr.Length);
            this.arr = tempArray;
        }
 
        public void PrintStack() {
            if (this.IsEmpty) {
                Console.WriteLine("Empty");
            }
            else {
                for (int i = arr.Length - 1; i >= 0; i--) {
                    var obj = arr[i];
                    if (obj != null)
                    {
                        Console.WriteLine("{0} ", obj);
                    }
                }
            }
        }
    }
 
 
}
 
 
 

 

반응형
: