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
|
#include<iostream>
using namespace std;
struct Stack
{
int size;
int top;
int* s;
};
void create(Stack* st)
{
cout << "enter size";
cin >> st->size;
st->top = -1;
st->s = new int[st->size];
}
void Display(Stack st)
{
for (int i = st.top; i>=0; i--)
{
cout << st.s[i]<< " ";
}
}
void Push(Stack* St, int x)
{
if (St->top == St->size - 1)
cout << "overflow";
else
{
St->top++;
St->s[St->top] = x;
}
}
int pop(Stack* st)
{
int x = -1;
if (st->top == -1)
cout <<"";
else
{
x = st->s[st->top--];
}
return x;
}
int peek(Stack st, int index)
{
int x = -1;
if (st.top - index + 1 < 0)
cout << "z";
x = st.s[st.top - index + 1];
return x;
}
int Empty(Stack st)
{
if (st.top == -1)
return 1;
return 0;
}
int isfuul(Stack st)
{
return st.top == st.size - 1;
}
int stackTop(Stack st)
{
if (!Empty(st))
return st.s[st.top];
return -1;
}
int main()
{
Stack st;
create(&st);
Push(&st,1);
Push(&st, 2);
Push(&st, 3);
pop(&st);
//Display(st);
cout << peek(st, 3);
return 0;
}
|
cs |
'알고리즘' 카테고리의 다른 글
Link stack 최종 (0) | 2022.10.30 |
---|---|
Link stack C++ (0) | 2022.10.30 |
c언어 stack (0) | 2022.10.29 |
double link (0) | 2022.10.28 |
싸이클 리스트 (0) | 2022.10.28 |