-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch for a node in a linked list
93 lines (84 loc) · 2.14 KB
/
Search for a node in a linked list
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
#include <stdio.h>
#include <stdlib.h>
struct digit {
int num;
struct digit *next;
};
struct digit * createDigit(int);
struct digit * append(struct digit * end, struct digit * newDigptr);
void printNumber(struct digit *);
void freeNumber(struct digit *start);
struct digit * readNumber();
struct digit * searchNumber(struct digit * start, int number);
int main(void) {
//! stack = showMemory(start=65520)
struct digit *start, *ptr;
int searchNum = 5;
printf("Please enter a number: ");
start = readNumber();
printNumber(start);
ptr = searchNumber(start, searchNum);
if (ptr!=NULL) {
printf("Found digit %d at location %p.\n", searchNum, ptr);
} else {
printf("Digit %d not found.\n", searchNum);
}
freeNumber(start);
return 0;
}
struct digit *createDigit(int dig) {
struct digit *ptr;
ptr = (struct digit *) malloc(sizeof(struct digit));
ptr->num = dig;
ptr->next = NULL;
return ptr;
}
struct digit * append(struct digit * end, struct digit * newDigptr) {
end->next = newDigptr;
return(end->next);
}
void printNumber(struct digit *start) {
struct digit * ptr = start;
while (ptr!=NULL) {
printf("%d", ptr->num);
ptr = ptr->next;
}
printf("\n");
}
void freeNumber(struct digit *start) {
struct digit * ptr = start;
struct digit * tmp;
while (ptr!=NULL) {
tmp = ptr->next;
free(ptr);
ptr = tmp;
}
}
struct digit * readNumber() {
//! heap=showMemory(start=309, cursors=[start, end, newptr])
char c;
int d;
struct digit *start, *end, *newptr;
start = NULL;
scanf("%c", &c);
while (c!='\n') {
d = c - 48;
newptr = createDigit(d);
if (start==NULL) {
start = newptr;
end = start;
} else {
end = append(end, newptr);
}
scanf("%c", &c);
}
return start;
}
struct digit * searchNumber(struct digit * start, int number) {
//! heap=showMemory(start=348, cursors=[ptr,start])
struct digit * ptr = start;
while ((ptr!=NULL) && (ptr->num!=number)) {
ptr = ptr->next;
}
return(ptr);
}