-
Notifications
You must be signed in to change notification settings - Fork 21
/
ex4.c
79 lines (62 loc) · 1.67 KB
/
ex4.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
/*
* 4. Write a function called removeEntry() to remove an entry from a linked
* list. The sole argument to the procedure should be a pointer to the list.
* Have the function remove the entry after the one pointed to by the argument.
* (Why can’t you remove the entry pointed to by the argument?) You need to use
* the special structure you set up in exercise 3 to handle the special case of
* removing the first element from the list.
*
* by Faisal Saadatamnd
*/
#include <stdio.h>
/* types */
struct entry {
int value;
struct entry *next;
};
/* functions */
void removeEntry(struct entry *);
void print_list(struct entry *);
void removeEntry(struct entry *element)
{
if (!element->next) /* if NULL */
element->next = NULL;
else
element->next = element->next->next;
}
void print_list(struct entry *p)
{
int i;
for (i = 1; p; ++i) {
printf("n%i (%p)\n", i, (void *) p);
printf(" value: %i\n", p->value);
printf(" next: %p\n", (void *) p->next);
p = p->next;
}
}
int main(void)
{
void removeEntry(struct entry *element);
struct entry n1, n2, n3, n4, n5; /* list entries */
struct entry *list_pointer = &n1; /* pointer to the beginning of the list */
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = &n4;
n4.value = 400;
n4.next = &n5;
n5.value = 500;
n5.next = NULL;
/* remove entry after n4, i.e. n5 */
removeEntry(&n4);
/* special struct to insert an element at the beginning */
struct entry n0 = {0, &n1};
/* remove entry after n0, i.e. n1 */
removeEntry(&n0);
/* update list_pointer to point to the new first element, i.e. n2 */
list_pointer = n0.next;
print_list(list_pointer);
return 0;
}