View difference between Paste ID: nuqm2CRX and r7YRMrEX
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
 89         head->name = malloc(strlen(name) + 1);
90
 90         head->next = NULL;
91
 91         head->prev = NULL;
92
 92         strcpy(head->name, name);
93
 93         head->totalCost = cost;
94
 94         return head;
95
 95 }
96
 96 
97
 97 void tokenize(char * line, char ** name, int * cost)
98
 98 {
99
 99     char * token, * delim = " $.";
100
100 
101
101     *name = strtok(line, delim);
102
102     token = strtok(NULL, delim);
103
103     while (token != NULL)
104
104     {
105
105         if (token[0] >= '1' && token[0] <= '9')
106
106         {
107
107             *cost = atoi(token);
108
108             return;
109
109         }
110
110         token = strtok(NULL, delim);
111
111     }
112
112 }
113
113 
114
114 void printList(Node * head)
115
115 {
116
116     Node * temp;
117
117     for (temp = head; temp != NULL; temp = temp->next)
118
118         printf("%s, %d\n", temp->name, temp->totalCost);
119
119 }
120
120 
121
121 void destroyList(Node * head)
122
122 {
123-
123     Node * temp;
123+
123     Node * temp, * next;
124-
124     for (temp = head; temp != NULL; temp = temp->next)
124+
124     for (temp = head; temp != NULL; temp = next)
125-
125         freeNode(temp);
125+
125     {
126-
126 }
126+
126			next = temp->next;
127
127         freeNode(temp);
128
128     }
129
129 }