-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1.c
169 lines (138 loc) · 3.12 KB
/
ex1.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include "menu.h"
typedef struct NODE {
int value;
struct NODE *next;
} Node;
typedef struct LIST {
Node *last;
Node *first;
int top;
} List;
void EX1_display(List *list)
{
Node *p;
int index;
for (index=0,p=list->first->next; p != NULL;index++,p=p->next) {
printf("%d - %d\n", index, p->value);
}
}
Node *EX1_find(List *list, int x)
{
Node *p;
int index;
for (index=0,p=list->first; p != NULL;index++,p=p->next) {
if(index==x) return p;
}
return NULL;
}
void EX1_insert(List *list, int value, int position)
{
Node *p;
Node *old;
Node *aux;
if ((p = malloc(sizeof(Node))) == NULL)
printf("Memory allocation error");
else {
p->value = value;
old = (position == -1) ? EX1_find(list, list->top) : EX1_find(list, position);
aux = old->next;
old->next = p;
p->next = aux;
list->top++;
if (aux == NULL) list->last = p;
}
}
void EX1_change(Node *node, int new)
{
if (node != NULL) node->value = new;
}
bool isEmpty(List *list)
{
return (list->first == list->last);
}
bool delete(List *list, Node *p)
{
Node *pdel;
int value;
if (isEmpty(list) || p == NULL || (pdel = p->next) == NULL) return false;
p->next = p->next->next;
value = pdel->value;
if (p->next == NULL) list->last = p;
list->top--;
free(pdel);
return value;
}
void initialize(List *list)
{
if ((list->first = malloc(sizeof(Node))) == NULL)
printf("Memory allocation error");
else {
list->last = list->first;
list->last->value = 0;
list->last->next = NULL;
list->top = 0;
}
}
void destroy(List *list)
{
while(! isEmpty(list)) delete(list, list->first);
}
int ex1(void)
{
List list;
int loop, value, valueNew;
char *options="\n 1 - Display \n 2 - insert \n 3 - insert in position \n 4 - Change \n 5 - Find \n 6 - Remove \n 7 - Destroy \n 0 - Exit\0";
loop = 1;
printf("\n Exercise 1\n");
initialize(&list);
while (loop) {
switch(menu(options)) {
case 1:
EX1_display(&list);
break;
case 2:
printf("Number to insert: ");
scanf("%d", &value);
EX1_insert(&list, value, -1);
break;
case 3:
printf("Number to insert: ");
scanf("%d", &value);
printf("Index to insert: ");
scanf("%d", &valueNew);
EX1_insert(&list, value, valueNew);
break;
case 4:
printf("Element to Change: ");
scanf("%d", &value);
printf("New number: ");
scanf("%d", &valueNew);
EX1_change(EX1_find(&list, value), valueNew);
printf("Element Changed");
break;
case 5:
printf("Element index: ");
scanf("%d", &value);
printf("Element Value is: %d ", (EX1_find(&list, value))->value);
break;
case 6:
printf("Element index: ");
scanf("%d", &value);
delete(&list, EX1_find(&list, value));
printf("Element Removed");
break;
case 7:
destroy(&list);
printf("Destroyed");
break;
case 0:
loop=0;
break;
}
}
return 0;
}