kan iemand een hint geven waarom mijn linked list niet correct is?
ik zit me af te vragen of ik de pointer niet mag verzetten... maar dat lijkt niet het geval.
het lijkt er meer op dat de prev en next items niet goed gevuld worden.
Helaas kan ik het niet voor elkaar krijgen om source te zien bij het debuggen met GDB
kan iemand hier misschien zien waar de fout zit en me een paar hints geven ?
FOO.C
LIST.H
ik zit me af te vragen of ik de pointer niet mag verzetten... maar dat lijkt niet het geval.
het lijkt er meer op dat de prev en next items niet goed gevuld worden.
Helaas kan ik het niet voor elkaar krijgen om source te zien bij het debuggen met GDB
kan iemand hier misschien zien waar de fout zit en me een paar hints geven ?
FOO.C
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 "list.h" #include <stdio.h> #include <stdlib.h> #include <string.h> linkedlist * liststart(linkedlist* list) { printf("let get the list to the start\n%x %x %x\n",list->prev,list,list->next); while( list->prev != NULL ) { printf("_"); list = list->prev; printf("*"); } printf("\n"); } linkedlist * listend(linkedlist* listin) { linkedlist* list; list = listin; printf("letstt go to end of list\n%x %x %x\n",list->prev,list,list->next); while( list->next != NULL) { printf("_"); list = list->next; printf("*"); } } linkedlist * listitem(linkedlist* list, int number) { list = liststart(list); while( (list->next != NULL)&&(number > 0)) { list = list->next; number--; } } void listfree(linkedlist* list) { list = liststart(list); while( list->next != NULL) { free(list->value); list = list->next; } } int listcount(linkedlist* list) { int counter = 0; printf("lets count\n"); list = listend(list); /*should be list start ofcourse.. testing */ list = liststart(list); while( list->next != NULL ) { printf("counter %d\n",counter); list = list->next; printf("lets lift counter\n"); counter++; } return(counter); } linkedlist * buildlist(char* longline, char seperator) { linkedlist* list; char* p; char* str; char* strstart; list = NULL; str = strdup(longline); strstart = str; while(p = strchr(str,seperator)) { *p = 0x00; if(list == NULL) { list = (linkedlist *)malloc(sizeof(linkedlist)); list->prev = NULL; list->value = (char *)malloc(strlen(str)+1); strcpy(list->value,str); list->next = NULL; } else { list->next = (linkedlist *)malloc(sizeof(linkedlist)); (list->next)->prev = list; list = list->next; list->value = (char *)malloc(strlen(str)+1); strcpy(list->value,str); list->next = NULL; } *p=';'; str=p+1; } free(strstart); } main() { int counter; linkedlist* list; list = buildlist("xychix;the fool;is;here;today",';'); printf("listbuild\n"); counter = listcount(list); printf("counted at %d\n",counter); while(counter < 0) { list = listitem(list,counter); printf("%s\n",list->value); } } |
LIST.H
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
| typedef struct linkedlist_type { struct linkedlist_type* prev; char* value; struct linkedlist_type* next; }linkedlist; linkedlist * liststart (linkedlist* list); linkedlist * listend (linkedlist* list); linkedlist * listitem (linkedlist* list, int number); void listfree (linkedlist* list); int listcount (linkedlist* list); linkedlist * buildlist (char* longstring,char seperator); |
[ Voor 7% gewijzigd door xychix op 17-03-2004 18:06 ]
Every failure offers you a new opportunity! | Lokatie database|GoT - Notepad