-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacklastunion.c
94 lines (85 loc) · 1.58 KB
/
stacklastunion.c
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
#include <iostream>
#define INT 1
#define FLOAT 2
#define STRING 3
#define MAXSIZE 100
using namespace std;
struct stackelement {
int etype;
union
{
int ival;
float fval;
char* pval;
}element;
};
struct stack {
int top;
stackelement* item = new stackelement[MAXSIZE];
};
bool empty(stack* s) {
if (s->top == -1) return 1;
return 0;
}
void push(stack* s, int data) {
if (s->top == MAXSIZE - 1) {
cout << "stack overflow" << endl;
}
else {
s->item[++s->top].etype = 1;
s->item[s->top].element.ival = data;
}
}
void push(stack* s, float data) {
if (s->top == MAXSIZE - 1) {
cout << "stack overflow" << endl;
}
else {
s->item[++s->top].etype = 2;
s->item[s->top].element.fval = data;
}
}
void push(stack* s, char* data) {
if (s->top == MAXSIZE - 1) {
cout << "stack overflow" << endl;
}
else {
s->item[++s->top].etype = 3;
s->item[s->top].element.pval = data;
}
}
stackelement pop(stack* s) {
if (empty(s)) {
cout << "stack underflow" << endl;
exit(1);
}
else (s->top -= 1);
cout << "last index has been deleted" << endl;
return s->item[s->top];
}
void print(stack* s) {
for(int i =0;i<s->top+1;i++){
if (s->item[i].etype == 1) {
cout << s->item[i].element.ival<<endl;
}
if (s->item[i].etype == 2) {
cout << s->item[i].element.fval << endl;
}
if (s->item[i].etype == 3) {
cout << s->item[i].element.pval << endl;
}
}
}
int main() {
stack nihat;
char x[6] = "nihat";
nihat.top = -1;
push(&nihat, 10);
push(&nihat, x);
push(&nihat, 20.4f);
push(&nihat, 45);
print(&nihat);
pop(&nihat);
pop(&nihat);
print(&nihat);
}