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
|
#include "Dictionary.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct NodeObj {
char *key;
char *value;
struct NodeObj *next;
} NodeObj;
typedef NodeObj *Node;
Node newNode(char *x, char *y) {
Node N = malloc(sizeof(NodeObj));
assert(N != NULL);
N->key = x;
N->value = y;
N->next = NULL;
return (N);
}
void freeNode(Node *pN) {
if(pN != NULL && *pN != NULL) {
{
free(*pN);
*pN = NULL;
}
}
typedef struct DictionaryObj {
Node top;
int numItems;
} DictionaryObj;
// functions
Node findKey(Dictionary D, char *key) {
if(D == NULL) {
fprintf(stderr, "Dictionary Error: calling isEmpty() on NULL "
"Dictionary reference\n");
exit(EXIT_FAILURE);
}
Node N = D->top;
while(strcmp(N->key, key)) {
N = N->next;
}
return N;
}
}
Dictionary newDictionary(void) {
Dictionary D = malloc(sizeof(DictionaryObj));
assert(D != NULL);
D->top = NULL;
D->numItems = 0;
return D;
}
void freeDictionary(Dictionary *pD) {
if(pD != NULL && *pD != NULL) {
if(!isEmpty(*pD)) {
makeEmpty(*pD);
}
free(*pD);
*pD = NULL;
}
}
int isEmpty(Dictionary D) {
if(D == NULL) {
fprintf(stderr, "Dictionary Error: calling isEmpty() on NULL "
"Dictionary reference\n");
exit(EXIT_FAILURE);
}
return D->numItems == 0;
}
int size(Dictionary D) {
return D->numItems;
}
char *lookup(Dictionary D, char *k) {
Node N = D->top;
if(D == NULL) {
fprintf(
stderr, "Dictionary Error: calling lookup() on NULL Dictionary\n");
exit(EXIT_FAILURE);
}
while(N != NULL) {
if(N->key == k) {
return N->value;
N = N->next;
}
}
return NULL;
}
void insert(Dictionary D, char *k, char *v) {
if(D == NULL) {
fprintf(stderr, "Dictionary Error: calling insert() on NULL Dictionary "
"reference\n");
exit(EXIT_FAILURE);
} else if(D->numItems == 0) {
D->top = newNode(k, v);
} else {
Node N;
N = newNode(k, v);
D->top->next = N;
D->top = N;
D->numItems++;
}
}
void makeEmpty(Dictionary D) {
if(D == NULL) {
fprintf(stderr, "Dictionary Error: calling delete() on NULL Dictionary "
"reference\n");
exit(EXIT_FAILURE);
} else {
D->top = NULL;
D->numItems = 0;
freeNode(&D->top);
}
}
void delete(Dictionary D, char *k) {
Node N = D->top;
if(D == NULL) {
fprintf(stderr, "Dictionary Error: calling delete() on Null Dictionary "
"reference\n");
exit(EXIT_FAILURE);
}
if(lookup(D, k) == NULL) {
fprintf(stderr, "Dictionary Error:\n");
exit(EXIT_FAILURE);
}
if(strcmp(N->key, k) == 0) {
Node A = D->top;
Node B = A->next;
D->top = B;
freeNode(&A);
} else {
while(N != NULL && N->next != NULL) {
if(strcmp(N->next->key, k) == 0) {
Node B = N;
Node C = N->next;
B->next = C->next;
freeNode(&C);
}
N = N->next;
}
}
D->numItems--;
}
void printDictionary(FILE *out, Dictionary D) {
Node N;
if(D == NULL) {
fprintf(stderr, "Dictionary Error: calling printDictionary() on NULL "
"Dictionary reference\n");
exit(EXIT_FAILURE);
}
for(N = D->top; N != NULL; N = N->next)
fprintf(out, "%s %s \n", N->key, N->value);
fprintf(out, "\n");
}
|