E D R , A S I H C RSS

서민관 (rev. 1.16)

서민관

내용

"Test String"

코드

  • Map in C
    Map.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int BOOL;
#define TRUE 1
#define FALSE 0

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);
int get(char *key);
BOOL contains(char *key);
void removePairFromMap(char *key);
void clear();
//*/

Map.c
#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;
}

KeyValuePair *head = NULL; // Real KeyValuePair List Head.

// 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 removePairFromMap(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 = removePairFromMap;
	ret->clear = clear;

	return ret;
}

  • main.c

#include <assert.h>

#include "Map.h"

int main() {
	Map *map = createMap();

	map->put("a", 10);
	map->put("b", 20);
	map->put("c", 30);
	map->put("d", 40);
	
	assert(map->contains("c") == TRUE);
	assert(map->contains("d") == TRUE);
	assert(map->get("b") == 20);

	map->remove("c");

	assert(map->contains("c") == FALSE);

	map->clear();

	assert(map->contains("a") == FALSE);
	assert(map->contains("b") == FALSE);
	assert(map->contains("c") == FALSE);
	assert(map->contains("d") == FALSE);

	map->put("b", 20);

	map->put("b", 30);

	assert(map->get("b") == 20);

	map->clear();

	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:11
Processing time 0.0220 sec