SHOW:
|
|
- or go back to the newest paste.
1 | 1 #include <stdio.h> | |
2 | 2 #include <string.h> | |
3 | 3 #include <stdlib.h> | |
4 | 4 | |
5 | 5 typedef struct LinkedNode | |
6 | 6 { | |
7 | 7 char * name; | |
8 | 8 int totalCost; | |
9 | 9 struct LinkedNode * next; | |
10 | 10 struct LinkedNode * prev; | |
11 | 11 } Node; | |
12 | 12 | |
13 | 13 Node * constructList(Node *, char *, int); | |
14 | 14 Node * addNode(Node *, char *, int); | |
15 | 15 void printList(Node *); | |
16 | 16 void tokenize(); | |
17 | 17 void destroyList(Node *); | |
18 | 18 | |
19 | 19 void freeNode(Node * temp) | |
20 | 20 { free(temp->name); free(temp); } | |
21 | 21 | |
22 | 22 int main(void) | |
23 | 23 { | |
24 | 24 Node * head = NULL; | |
25 | 25 char buffer[51], * name; | |
26 | 26 int cost; | |
27 | 27 | |
28 | 28 while (fgets(buffer, sizeof(buffer), stdin) != NULL) | |
29 | 29 { | |
30 | 30 tokenize(buffer, &name, &cost); | |
31 | 31 head = addNode(head, name, cost); | |
32 | 32 } | |
33 | 33 | |
34 | 34 printList(head); | |
35 | 35 destroyList(head); | |
36 | 36 | |
37 | 37 return EXIT_SUCCESS; | |
38 | 38 } | |
39 | 39 | |
40 | 40 Node * addNode(Node * head, char * name, int cost) | |
41 | 41 { | |
42 | 42 Node * temp, * prev, * new; | |
43 | 43 | |
44 | 44 if (head == NULL) /* if list does not yet exit */ | |
45 | 45 return constructList(head, name, cost); | |
46 | 46 | |
47 | 47 /* If a list DOES exist, set up the new node */ | |
48 | 48 new = malloc(sizeof(Node)); | |
49 | 49 new->name = malloc(strlen(name) + 1); | |
50 | 50 strcpy(new->name, name); | |
51 | 51 new->totalCost = cost; | |
52 | 52 | |
53 | 53 for (temp = head, prev = head; temp != NULL; prev = temp, temp = temp->next) | |
54 | 54 { | |
55 | 55 if (strcmp(new->name, temp->name) < 0) /* if new node goes BEFORE node */ | |
56 | 56 { /* currently pointed to by temp */ | |
57 | 57 if (temp == head) | |
58 | 58 { /* new node becomes head */ | |
59 | 59 new->next = head; | |
60 | 60 head->prev = new; | |
61 | 61 head = new; | |
62 | 62 return head; | |
63 | 63 } | |
64 | 64 else | |
65 | 65 { /* inserting in middle of list */ | |
66 | 66 new->next = temp; | |
67 | 67 new->prev = prev; | |
68 | 68 prev->next = new; | |
69 | 69 temp->prev = new; | |
70 | 70 return head; | |
71 | 71 } | |
72 | 72 } | |
73 | 73 else if (strcmp(new->name, temp->name) == 0) /* updating node */ | |
74 | 74 { | |
75 | 75 freeNode(new); | |
76 | 76 temp->totalCost += cost; | |
77 | 77 return head; | |
78 | 78 } | |
79 | 79 } | |
80 | 80 /*end of list */ | |
81 | 81 new->prev = prev; | |
82 | 82 prev->next = new; | |
83 | 83 new->next = NULL; | |
84 | 84 return head; | |
85 | 85 } | |
86 | 86 | |
87 | 87 Node * constructList(Node * head, char * name, int cost) | |
88 | 88 { | |
89 | 88.5 head = malloc(sizeof(Node)); | |
90 | 89 head->name = malloc(strlen(name) + 1); | |
91 | 90 head->next = NULL; | |
92 | 91 head->prev = NULL; | |
93 | 92 strcpy(head->name, name); | |
94 | 93 head->totalCost = cost; | |
95 | 94 return head; | |
96 | 95 } | |
97 | 96 | |
98 | 97 void tokenize(char * line, char ** name, int * cost) | |
99 | 98 { | |
100 | 99 char * token, * delim = " $."; | |
101 | 100 | |
102 | 101 *name = strtok(line, delim); | |
103 | 102 token = strtok(NULL, delim); | |
104 | 103 while (token != NULL) | |
105 | 104 { | |
106 | 105 if (token[0] >= '1' && token[0] <= '9') | |
107 | 106 { | |
108 | 107 *cost = atoi(token); | |
109 | 108 return; | |
110 | 109 } | |
111 | 110 token = strtok(NULL, delim); | |
112 | 111 } | |
113 | 112 } | |
114 | 113 | |
115 | 114 void printList(Node * head) | |
116 | 115 { | |
117 | 116 Node * temp; | |
118 | 117 for (temp = head; temp != NULL; temp = temp->next) | |
119 | 118 printf("%s, %d\n", temp->name, temp->totalCost); | |
120 | 119 } | |
121 | 120 | |
122 | 121 void destroyList(Node * head) | |
123 | 122 { | |
124 | 123 Node * temp, * next; | |
125 | 124 for (temp = head; temp != NULL; temp = next) | |
126 | 125 { | |
127 | 126 next = temp->next; | |
128 | 127 freeNode(temp); | |
129 | 128 } | |
130 | 129 } |