-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes35.c
123 lines (98 loc) · 2.02 KB
/
es35.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
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
#include <stdio.h>
#include <stdbool.h>
//const int DIM_STACK = 10;
#define DIM_STACK 10
typedef struct{
int elem[DIM_STACK];
int testa;
} tStack;
void InizializzaStack(tStack* stack);
bool StackVuoto(tStack stack);
bool StackPieno(tStack stack);
bool Push(tStack* stack, int elem);
bool Pop(tStack* stack, int* elem);
void Menu();
int main()
{
Menu();
return 0;
}
//Resettiamo la testa dello stack
void InizializzaStack(tStack* stack)
{
(*stack).testa = -1;
return;
}
bool StackVuoto(tStack stack)
{
if (stack.testa == -1)
return true;
else
return false;
}
bool StackPieno(tStack stack)
{
if(stack.testa == (DIM_STACK - 1))
return true;
else
return false;
}
bool Push(tStack* stack, int elem)
{
if(!StackPieno(*stack))
{
(*stack).testa++;
(*stack).elem[(*stack).testa] = elem;
return true;
}
else
return false;
}
bool Pop(tStack* stack, int* elem)
{
if(!StackVuoto(*stack))
{
(*elem) = (*stack).elem[(*stack).testa];
(*stack).testa--;
return true;
}
else
return false;
}
void Menu()
{
int operazione;
int elem;
tStack stack;
InizializzaStack(&stack);
do{
printf("1 - Pop\n");
printf("2 - Push\n");
printf("0 - Esci\n");
scanf("%d", &operazione);
switch(operazione)
{
case 1:
if(Pop(&stack, &elem))
printf("Valore estratto: %d\n", elem);
else
printf("Stack vuoto\n");
break;
case 2:
printf("Inserisci il valore da inserire: ");
scanf("%d", &elem);
if(Push(&stack, elem))
printf("Valore inserito\n");
else
printf("Stack pieno\n");
break;
case 0:
printf("Uscita\n");
break;
default:
printf("Operazione errata");
break;
}
}while(operazione != 0);
return;
}