/
LinkedListStruct.c
executable file
·161 lines (143 loc) · 3.02 KB
/
LinkedListStruct.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "LinkedListStruct.h"
// Node ADT
typedef struct NodeObj{
char * block;
char startpoint;
struct NodeObj* next;
} NodeObj;
typedef NodeObj* Node;
// newNode()
// constructor of the Node type
Node newNode(char* data, char start) {
//printf("malloc Node\n");
Node N = malloc(sizeof(NodeObj));
assert(N!=NULL);
//printf("malloc block\n");
N->block = malloc(sizeof(char)*30);
assert(N->block != NULL);
memcpy(N->block,data,30);
//printf("memcpy\n");
/*int i;
for (i=0;i<30;i++){
*((N->block)+sizeof(char)*i) = *(data+sizeof(char)*i);
}*/
N->startpoint = start;
N->next = NULL;
//printf("done\n");
return(N);
}
// freeNode()
// destructor for the Node type
void freeNode(Node* pN){
if( pN!=NULL && *pN!=NULL ){
free((*pN)->block);
free(*pN);
*pN = NULL;
}
}
// emptyNode()
void emptyNode(Node N){
if(N != NULL){
emptyNode(N->next);
N->next = NULL;
//N->block = NULL;
freeNode(&N);
}
}
typedef struct LinkedListObj{
Node head;
int numItems;
} LinkedListObj;
typedef struct LinkedListObj* LinkedList;
LinkedList newLinkedList(void){
LinkedList L = (struct LinkedListObj*)malloc(sizeof(LinkedListObj));
assert(L!=NULL);
L->head = NULL;
L->numItems = 0;
return(L);
}
void emptyList(LinkedList L) {
emptyNode(L->head);
L->head=NULL;
L->numItems=0;
}
void freeLinkedList(LinkedList* pL) {
if( pL!=NULL && *pL!=NULL ){
free(*pL);
*pL = NULL;
}
}
void addNode(LinkedList L, char* data, char start){
if (L->head == NULL){
//printf("head\n");
L->head = newNode(data,start);
} else {
Node N = L->head;
while (N->next != NULL){
N=N->next;
}
N->next = newNode(data,start);
}
//printf("done addnode\n");
L->numItems++;
}
char popNode(LinkedList L, char *buf) {
Node N;
char s;
if (L->numItems>0){
N = L->head;
s = N->startpoint;
memcpy(buf,N->block,30);
/*for (i=0; i<30; i++){
*(buf+sizeof(char)*i)= *(N->block+sizeof(char)*i);
}*/
L->head=N->next;
L->numItems--;
freeNode(&N);
return s;
} else {
return -1;
}
}
/*
char popBlock(LinkedList L, char * buf) {
//char * block;
NodeObj N;
if (L->numItems > 0) {
N = popNode(L);
buf = N.block;
return N.startpoint;
} else {
return -1;
}
}*/
/*void removeNode(LinkedList L, char* k) {
if(searchList(L, k)){
Node N = L->head;
if(strcmp(N->key, k) == 0){
L->head=N->next;
freeNode(&N);
} else {
for (int i=0; i<(L->numItems-1); i++){
if (strcmp(N->next->key, k) != 0){
N=N->next;
} else {
break;
}
}
Node M = N->next;
if (M->next == NULL) {
N->next = NULL;
} else {
N->next = M->next;
M->next = NULL;
}
freeNode(&M);
}
L->numItems--;
}
}*/