E D R , A S I H C RSS

서민관 (rev. 1.13)

서민관

내용

"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;
}

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 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;
}
  • main.cpp

#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);

	map->remove("c");

	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.0341 sec