-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3.c
118 lines (94 loc) · 1.78 KB
/
ex3.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
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include "menu.h"
typedef struct NODE {
int value;
struct NODE *previous;
} Node;
typedef struct STACK {
Node *top;
int size;
} Stack;
void EX3_display(Stack *stack)
{
Node *p;
int index;
for (index=stack->size,p=stack->top; p != NULL;index--,p=p->previous) {
printf("%d - %d\n", index, p->value);
}
}
void EX3_push(Stack *stack, int value)
{
Node *p;
if ((p = malloc(sizeof(Node))) == NULL)
printf("Memory allocation error");
else {
p->previous = stack->top;
p->value = value;
stack->top = p;
stack->size++;
}
}
bool EX3_isEmpty(Stack *stack)
{
return (stack->size == 0);
}
int EX3_pop(Stack *stack)
{
Node *p;
int value;
if (EX3_isEmpty(stack)) {
printf("Stack empty");
return 0;
}
p = stack->top;
stack->top = stack->top->previous;
stack->size--;
value = p->value;
free(p);
return value;
}
void EX3_initialize(Stack *stack)
{
stack->top = NULL;
stack->size = 0;
}
void EX3_destroy(Stack *stack)
{
while(! EX3_isEmpty(stack)) EX3_pop(stack);
}
int ex3(void)
{
Stack stack;
int loop, value;
char *options="\n 1 - Display \n 2 - Push \n 3 - Pop \n 4 - Destroy \n 0 - Exit\0";
loop = 1;
printf("\n Exercise 3\n");
EX3_initialize(&stack);
while (loop) {
switch(menu(options)) {
case 1:
EX3_display(&stack);
break;
case 2:
printf("Number to insert: ");
scanf("%d", &value);
EX3_push(&stack, value);
break;
case 3:
value = EX3_pop(&stack);
printf("Pop value: %d", value);
break;
case 4:
EX3_destroy(&stack);
printf("Destroyed");
break;
case 0:
loop=0;
break;
}
}
return 0;
}