-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllist.c
172 lines (146 loc) · 3.74 KB
/
llist.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdlib.h>
#include "llist.h"
/* Private node ADT. */
typedef struct _node node;
struct _node {
node * next;
void * e;
};
static node * newNode(void * e) {
node * n = malloc(sizeof(node));
if (!n) return NULL;
n->next = NULL;
n->e = e;
return n;
}
static void deleteNode(node * n) {
free(n);
}
/* Linked list library. */
struct _llist {
node * head;
};
llist * newLList(void) {
llist * ll = malloc(sizeof(llist));
if (!ll) return NULL;
ll->head = NULL;
return ll;
}
void deleteLList(llist * ll) {
if (!ll) return;
node * n = ll->head;
while (n) {
node * next = n->next;
deleteNode(n);
n = next;
}
free(ll);
}
int isEmptyLList(llist const * ll) {
if (!ll) return 0;
return (ll->head == NULL);
}
int putLList(llist * ll, void * e) {
node * n;
if (!ll) return -1;
n = newNode(e);
if (!n) return -1;
n->next = ll->head;
ll->head = n;
return 0;
}
int getLList(llist * ll, void ** e) {
node * n;
if (!ll || !e) return -1;
if (ll->head == NULL) { /* nothing to get */
*e = NULL;
return -2;
}
n = ll->head;
*e = n->e; /* write element */
ll->head = n->next;
deleteNode(n);
return 0;
}
int peekLList(llist const * ll, void ** e) {
if (!ll || !e) return -1;
if (ll->head == NULL) {
/* Nothing to get. */
*e = NULL;
return -2;
}
*e = ll->head->e; /* write element */
return 0;
}
int printLList(llist const * ll, printFn f) {
node * n;
int cnt;
if (!ll || !f) return -1;
cnt = 0;
for (n = ll->head; n != NULL; n = n->next) {
/* Print the index of the element. */
cnt++;
printf(" %d:", cnt);
/* Call user-provided f to print the element. */
f(n->e);
}
printf("\n");
return 0;
}
//this function should combine ll1 and ll2 into ll3 where the values in ll1 and ll2 are alternating
int zip(llist * ll1, llist * ll2, llist * ll3) {
if (!ll1 || !ll2) return -1;
if (ll1 == ll3) return -1;
if (ll2 == ll3) return -1;
//ll3 should be an empty list, the code below checks for that
if (!isEmptyLList(ll3)) return -1;
//this code below should prevent the input list being the same as the output list
node * tail3 = NULL;
while(ll1->head != NULL && ll2->head != NULL){
if(!tail3){
ll3->head = tail3 = ll1->head;
} else {
tail3->next = ll1->head;
tail3 = tail3->next;
}
ll1->head = ll1->head->next;
tail3->next = ll2->head;
tail3 = tail3->next;
ll2->head = ll2->head->next;
}
//This code below was what performed the function lmost exactly correct except it was mirrored.
// node * n = ll1->head;
// node * next = n->next;
// n->next = ll3->head;
// ll3->head = n;
// ll1->head = next;
// node * x = ll2->head;
// next = x->next;
// x->next = ll3->head;
// ll3->head = x;
// ll2->head = next;
//once at least one of the llists are emoty it will autmatically exit from the while loop.
//if ll1 is not yet empty use while(n) to finish transferring it over
while (ll1->head != NULL){
node * next = ll1->head->next;
ll1->head->next = ll3->head;
ll3->head = ll1->head;
ll1->head = next;
}
//if ll2 is not yet empty use while(x) to finish transferring it over
while (ll2->head != NULL){
node * next = ll2->head->next;
ll2->head->next = ll3->head;
ll3->head = ll2->head;
ll2->head = next;
}
//possibly use exercise 8.2 to determine if it's been properly sorted?
ll1->head = NULL;
//make sure the llists are finished and empty and NULL
ll2->head = NULL;
//make sure ll1 is not equal to ll3
//the program is almost finished except that the lists are being added in reverse order. I need to start from the tail and work up.
/* Your code goes here. */
return 0;
}