-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack
More file actions
130 lines (117 loc) · 3.13 KB
/
Stack
File metadata and controls
130 lines (117 loc) · 3.13 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stack
{
class Stack
{
static int MAX = 13;
int[] s;
int size;
int top;
//housekeeping funcition class안에서 효율적으로 짜기위해서 만듬 바깥에서는 사용하지
// 않기때문에 private으로 정의
private void initialize()
{
for (int i = 0; i < s.Length; i++)
{
s[i] = 0;
}
}
public void Overflow()
{
Console.WriteLine("오버플로우");
Environment.Exit(-1);
}
public void emptyError()
{
Console.WriteLine("비어있음");
Environment.Exit(-1);
}
public Stack() //중복되는 것을 줄여 함수 호출시 stack함수에 MAX값이 넘어가 실행됨
:this(MAX)
{
}
public Stack(int x)
{
s = new int[x];
size = x;
top = -1;
initialize();//0을 원래 채워주지만 직접 초기화를 시켜 안정성 향상
}
public void push(int x)
{
if (top >= size-1) Overflow();
top++;
s[top] = x;
}
public int pop()
{
if (top == -1) emptyError();
int x = s[top];
top--;
return x;
}
public int peek()
{
return s[top];
}
public int getHave()
{
return top + 1;
}
public void print()
{
Console.Write("[");
for (int i = 0; i <= top; i++)
{
Console.Write(s[i] + " ");
}
Console.WriteLine("]");
}
public bool isEmpty()
{
if (top == -1) return true;
return false;
}
}
class Program
{
static void Main(string[] args)
{
Stack a = new Stack();
Stack b = new Stack();
Stack c = new Stack(100);
a.push(10);
a.push(20);
a.push(30);
a.push(40);
int x = a.pop();
a.push(50);
b.push(100);
b.push(200);
b.push(300);
int y = b.pop();
b.push(400);
b.push(500);
c.push(60);
c.push(600);
int z = c.pop();
c.push(8000);
Console.WriteLine("x={0},y={1},z={2}", x, y, z);
Console.WriteLine("top a={0} , top b={1},top c={2}", a.peek(), b.peek(), c.peek());
Console.WriteLine("a의총 개수={0},b의총 개수={1},c의총 개수={2}", a.getHave(), b.getHave(), c.getHave());
a.print();
if (b.isEmpty()) //b의 스택안이 비어있는지 확인하기 위해쓰이는 함수임. isEmpty
{
Console.WriteLine(" Empty");
}
else
{
Console.WriteLine("Not Empty");
}
}
}
}