More actions
imported>smksyj No edit summary |
imported>smksyj No edit summary |
||
| Line 2: | Line 2: | ||
"Test String" | "Test String" | ||
== 코드 == | == 코드 == | ||
Map in C | * Map in C | ||
Map.h | |||
#pragma once | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
typedef struct Map { | |||
void (*put)(char *key, int value); | |||
int (*get)(char *key); | |||
bool (*contains)(char *key); | |||
void (*remove)(char *key); | |||
void (*clear)(); | |||
} Map; | |||
Map *createMap(); | |||
/* | |||
// Commented for private using in Map. | |||
// To watch details, watch Map.cpp | |||
void put(char *key, int value); | |||
KeyValuePair *get(char *key); | |||
bool contains(char *key); | |||
void remove(char *key); | |||
void clear(); | |||
//*/ | |||
Map.cpp | |||
#include "Map.h" | |||
typedef struct KeyValuePair { | |||
char *key; | |||
int value; | |||
struct KeyValuePair *next; | |||
} KeyValuePair; | |||
// functions for KeyValuePair List | |||
KeyValuePair *createPair(char *key, int value) { | |||
KeyValuePair *ret = (KeyValuePair *)malloc(sizeof(KeyValuePair)); | |||
memset(ret, 0, sizeof(KeyValuePair)); | |||
ret->key = (char *)malloc(sizeof(char) * (strlen(key) + 1)); | |||
strcpy(ret->key, key); | |||
ret->value = value; | |||
return ret; | |||
} | |||
void removePair(KeyValuePair **pair) { | |||
free((*pair)->key); | |||
free(*pair); | |||
*pair = NULL; | |||
} | |||
void addPair(KeyValuePair **head, KeyValuePair *keyValuePair) { | |||
if ( *head == NULL ) { | |||
*head = keyValuePair; | |||
} else { | |||
KeyValuePair *temp = *head; | |||
while ( temp->next != NULL ) { | |||
if ( strcmp(temp->key, keyValuePair->key) == 0 ) { | |||
return; | |||
} | |||
temp = temp->next; | |||
} | |||
temp->next = keyValuePair; | |||
} | |||
} | |||
KeyValuePair *getPair(KeyValuePair **head, char *key) { | |||
if ( *head == NULL ) { | |||
return NULL; | |||
} else if ( strcmp((*head)->key, key) == 0 ) { | |||
KeyValuePair *ret = *head; | |||
*head = (*head)->next; | |||
return ret; | |||
} else { | |||
KeyValuePair *successor = *head; | |||
KeyValuePair *ret = (*head)->next; | |||
while ( ret != NULL ) { | |||
if ( strcmp(ret->key, key) == 0 ) { | |||
successor->next = ret->next; | |||
ret->next = NULL; | |||
return ret; | |||
} | |||
successor = ret; | |||
ret = ret->next; | |||
} | |||
return NULL; | |||
} | |||
} | |||
bool hasKey(KeyValuePair **head, char *key) { | |||
if ( *head != NULL ) { | |||
KeyValuePair *temp = *head; | |||
while ( temp != NULL ) { | |||
if ( strcmp(temp->key, key) == 0 ) { | |||
return true; | |||
} | |||
temp = temp->next; | |||
} | |||
} | |||
return false; | |||
} | |||
void clearList(KeyValuePair **head) { | |||
if ( *head == NULL ) { | |||
} else if ( *head != NULL && (*head)->next == NULL ) { | |||
removePair(head); | |||
} else { | |||
KeyValuePair *successor = *head; | |||
KeyValuePair *temp = (*head)->next; | |||
while ( temp != NULL ) { | |||
removePair(&successor); | |||
successor = temp; | |||
temp = temp->next; | |||
} | |||
removePair(&successor); | |||
} | |||
*head = NULL; | |||
} | |||
static KeyValuePair *head = NULL; | |||
// functions for Map | |||
void put(char *key, int value) { | |||
addPair(&head, createPair(key, value)); | |||
} | |||
int get(char *key) { | |||
KeyValuePair *temp = getPair(&head, key); | |||
if ( temp != NULL ) { | |||
return temp->value; | |||
} | |||
return 0; | |||
} | |||
bool contains(char *key) { | |||
return hasKey(&head, key); | |||
} | |||
void remove(char *key) { | |||
getPair(&head, key); | |||
} | |||
void clear() { | |||
clearList(&head); | |||
} | |||
Map *createMap() { | |||
Map *ret = (Map *)malloc(sizeof(Map)); | |||
memset(ret, 0, sizeof(Map)); | |||
ret->put = put; | |||
ret->get = get; | |||
ret->contains = contains; | |||
ret->remove = remove; | |||
ret->clear = clear; | |||
return ret; | |||
} | |||
Revision as of 18:23, 1 August 2012
내용
"Test String"
코드
- Map in C
Map.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Map {
void (*put)(char *key, int value);
int (*get)(char *key);
bool (*contains)(char *key);
void (*remove)(char *key);
void (*clear)();
} Map;
Map *createMap();
/*
// Commented for private using in Map.
// To watch details, watch Map.cpp
void put(char *key, int value);
KeyValuePair *get(char *key);
bool contains(char *key);
void remove(char *key);
void clear();
//*/
Map.cpp
#include "Map.h"
typedef struct KeyValuePair {
char *key;
int value;
struct KeyValuePair *next;
} KeyValuePair;
// functions for KeyValuePair List
KeyValuePair *createPair(char *key, int value) {
KeyValuePair *ret = (KeyValuePair *)malloc(sizeof(KeyValuePair));
memset(ret, 0, sizeof(KeyValuePair));
ret->key = (char *)malloc(sizeof(char) * (strlen(key) + 1));
strcpy(ret->key, key);
ret->value = value;
return ret;
}
void removePair(KeyValuePair **pair) {
free((*pair)->key);
free(*pair);
*pair = NULL;
}
void addPair(KeyValuePair **head, KeyValuePair *keyValuePair) {
if ( *head == NULL ) {
*head = keyValuePair;
} else {
KeyValuePair *temp = *head;
while ( temp->next != NULL ) {
if ( strcmp(temp->key, keyValuePair->key) == 0 ) {
return;
}
temp = temp->next;
}
temp->next = keyValuePair;
}
}
KeyValuePair *getPair(KeyValuePair **head, char *key) {
if ( *head == NULL ) {
return NULL;
} else if ( strcmp((*head)->key, key) == 0 ) {
KeyValuePair *ret = *head;
*head = (*head)->next;
return ret;
} else {
KeyValuePair *successor = *head;
KeyValuePair *ret = (*head)->next;
while ( ret != NULL ) {
if ( strcmp(ret->key, key) == 0 ) {
successor->next = ret->next;
ret->next = NULL;
return ret;
}
successor = ret;
ret = ret->next;
}
return NULL;
}
}
bool hasKey(KeyValuePair **head, char *key) {
if ( *head != NULL ) {
KeyValuePair *temp = *head;
while ( temp != NULL ) {
if ( strcmp(temp->key, key) == 0 ) {
return true;
}
temp = temp->next;
}
}
return false;
}
void clearList(KeyValuePair **head) {
if ( *head == NULL ) {
} else if ( *head != NULL && (*head)->next == NULL ) {
removePair(head);
} else {
KeyValuePair *successor = *head;
KeyValuePair *temp = (*head)->next;
while ( temp != NULL ) {
removePair(&successor);
successor = temp;
temp = temp->next;
}
removePair(&successor);
}
*head = NULL;
}
static KeyValuePair *head = NULL;
// functions for Map
void put(char *key, int value) {
addPair(&head, createPair(key, value));
}
int get(char *key) {
KeyValuePair *temp = getPair(&head, key);
if ( temp != NULL ) {
return temp->value;
}
return 0;
}
bool contains(char *key) {
return hasKey(&head, key);
}
void remove(char *key) {
getPair(&head, key);
}
void clear() {
clearList(&head);
}
Map *createMap() {
Map *ret = (Map *)malloc(sizeof(Map));
memset(ret, 0, sizeof(Map));
ret->put = put;
ret->get = get;
ret->contains = contains;
ret->remove = remove;
ret->clear = clear;
return ret;
}