Skip to content

Commit faf6b80

Browse files
Linked list updated with new functions.
Added list_copy makes deep copy of list. Returns separate copy of the not a new pointer to the old list. Added list_print display contents of list to stdout. The list uses pointers to void so the printout only includes the addresses of the data payload and not the contents. Function list_get_num returns pointer to node by number. Number zero is the address of the head node. Function list_node_swap reverses the data payload of two nodes. Function list_reverse reverses the linked list payload. It should be noted that the addresses of the nodes themselves are modified during this operation. Hash functions have been removed from unit tests. Hash functions not complete in this revision and should not be used.
1 parent 6ff51bb commit faf6b80

File tree

4 files changed

+416
-248
lines changed

4 files changed

+416
-248
lines changed

lib_hash.c

+15-26
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,24 @@ Hash hash_new_prime(int len)
4444
return h;
4545
}
4646

47+
int hash_data(void *data, int num, size_t size)
48+
{
49+
int slot = 0;
50+
while(num >= 0)
51+
{
52+
/* slot += (void *) (data) + (num * size); */
53+
slot += (int)data + size * num;
54+
num--;
55+
}
56+
return slot;
57+
}
58+
4759
int hash_insert(Hash h, void * data, size_t length)
4860
{
61+
int slot;
4962
assert(data != NULL);
63+
/*slot = hash_data(data, length);
64+
if(h[slot]->data == NULL) *//* insert data in first node */;
5065

5166
return 0;
5267
}
@@ -86,29 +101,3 @@ void hash_delete(Hash h)
86101
{
87102
free(&h);
88103
}
89-
90-
91-
/*
92-
93-
int main()
94-
{
95-
int j, loc, distinct, key, num[N + 1];
96-
FILE * in = fopen("numbers.in", "r");
97-
for(j = 1; j <= N; j++)
98-
num[j] = Empty;
99-
distinct = 0;
100-
while(fscanf(in, "%d", &key) == 1) {
101-
loc = loc % N + 1;
102-
if(num[loc] == Empty) {
103-
if(distinct == MaxNumbers) {
104-
printf("\nTable Full: %d not added\n", key);
105-
exit(1);
106-
}
107-
num[loc] = key;
108-
distinct++;
109-
}
110-
}
111-
printf("\nThere are %d distinct numbers\n", distinct);
112-
fclose(in);
113-
}
114-
*/

lib_ll.c

+83-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <assert.h>
12
#include <stdlib.h>
23
#include <stddef.h>
34
#include <stdbool.h>
@@ -17,7 +18,10 @@ List_Head *list_new(void)
1718

1819
void list_delete(List_Head *pHead)
1920
{
20-
List_Node *pTemp = pHead->pNext;
21+
List_Node *pTemp;
22+
assert(pHead != NULL);
23+
if(pHead->count == 0) { return; }
24+
pTemp = pHead->pNext;
2125
while(pTemp->pNext != NULL)
2226
{
2327
pTemp = pTemp->pNext;
@@ -50,11 +54,6 @@ List_Node *list_tail(List_Head *pHead)
5054
return pTemp;
5155
}
5256

53-
void list_print(List_Head *pHead)
54-
{
55-
printf("Count: %d\n", pHead->count);
56-
}
57-
5857
List_Node *list_ins_head(List_Head *pHead)
5958
{
6059
List_Node *pTemp = pHead->pNext;
@@ -187,6 +186,7 @@ int list_rm_node(List_Head *pHead, List_Node *pNode)
187186
return 1;
188187
}
189188

189+
190190
int list_rm_before(List_Head *pHead, List_Node *pNode)
191191
{
192192
List_Node *pPrev = list_prev_node(pHead, pNode);
@@ -195,9 +195,86 @@ int list_rm_before(List_Head *pHead, List_Node *pNode)
195195
return list_rm_node(pHead, pPrev);
196196
}
197197

198+
int list_copy(List_Head *pDest, List_Head *pSrc)
199+
{
200+
List_Node *pNewNode = NULL, *pSrcNode = NULL;
201+
assert(pDest != NULL && pSrc != NULL);
202+
pSrcNode = pSrc->pNext; /* first node from source */
203+
if(pSrcNode == NULL) return 0; /* empty source list */
204+
while (pSrcNode->pNext != NULL)
205+
{
206+
pNewNode = list_ins_tail(pDest);
207+
if(pNewNode == NULL) { return 1; }
208+
pNewNode->pData = pSrcNode->pData; /* copy data */
209+
pSrcNode = pSrcNode->pNext; /* move to next pointer */
210+
}
211+
return 0;
212+
}
213+
214+
void list_print(List_Head *pHead)
215+
{
216+
int i = 0;
217+
List_Node *temp = NULL;
218+
assert(pHead != NULL);
219+
printf("\n");
220+
if(pHead->count == 0)
221+
printf("Empty List\n");
222+
else {
223+
temp = pHead->pNext;
224+
do {
225+
printf("List Item: %d -> Data: %p\n", i, (void *)temp);
226+
temp = temp->pNext;
227+
i++;
228+
} while (temp != NULL);
229+
}
230+
}
231+
232+
List_Node *list_get_num(List_Head *pHead, int count)
233+
{
234+
int i = 1;
235+
List_Node *pNode;
236+
assert(count >= 0);
237+
assert(pHead != NULL);
238+
if(count == 0) { return pHead->pNext; }
239+
if(pHead->count == 0) { return NULL; }
240+
pNode = pHead->pNext;
241+
if(count == 1) { return pNode; }
242+
while(i < count)
243+
{
244+
if(pNode->pNext == NULL) { return NULL; }
245+
pNode = pNode->pNext;
246+
i++;
247+
}
248+
return pNode;
249+
}
250+
251+
int list_node_swap(List_Node *pPrev, List_Node *pCurr)
252+
{
253+
List_Node* pTemp = NULL;
254+
assert(pCurr != NULL && pPrev != NULL);
255+
pTemp = pCurr->pNext;
256+
pCurr->pNext = pPrev->pNext;
257+
pPrev->pNext = pTemp;
258+
return 0;
259+
}
260+
261+
List_Head *list_reverse(List_Head *pHead)
262+
{
263+
List_Head *newList = list_new();
264+
assert(pHead != NULL);
265+
while(pHead->count > 0)
266+
{
267+
list_ins_head_data(newList, pHead->pNext->pData);
268+
list_rm_node(pHead, pHead->pNext);
269+
}
270+
list_delete(pHead);
271+
return newList;
272+
}
273+
198274
void list_clear(List_Head *pHead)
199275
{
200276
List_Node *pTemp = pHead->pNext;
277+
assert(pHead != NULL);
201278
if(pTemp != NULL)
202279
{
203280
while(pTemp->pNext != NULL)
@@ -213,7 +290,6 @@ void list_clear(List_Head *pHead)
213290
List_Node *list_prev_node(List_Head *pHead, List_Node *pNode)
214291
{
215292
List_Node *pPrev = NULL, *pTemp;
216-
217293
if(pHead == NULL || pNode == NULL)
218294
return NULL;
219295
pTemp = pHead->pNext;

lib_ll.h

+15-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ List_Node *list_search(List_Head *);
5050
/* return address of end of list */
5151
List_Node *list_tail(List_Head *);
5252

53-
/* print list contents and details */
54-
void list_print(List_Head *);
55-
5653
/* push new node on head of list */
5754
List_Node *list_ins_head(List_Head *);
5855

@@ -76,6 +73,21 @@ int list_rm_node(List_Head *pHead, List_Node *);
7673
*/
7774
int list_rm_before(List_Head *pHead, List_Node *);
7875

76+
/* make a deep copy of list */
77+
int list_copy(List_Head *pDest, List_Head *pSrc);
78+
79+
/* print out contents of list to stdout */
80+
void list_print(List_Head *pHead);
81+
82+
/* get address of node at num - first node is 1 */
83+
List_Node *list_get_num(List_Head *pHead, int count);
84+
85+
/* reverse current nodes - modify pointer to next in each */
86+
int list_node_swap(List_Node *pPrev, List_Node *pCurr);
87+
88+
/* reverse contents of list */
89+
List_Head *list_reverse(List_Head *pHead);
90+
7991
/* remove all nodes in list and free memory for each node */
8092
void list_clear(List_Head *);
8193

0 commit comments

Comments
 (0)