ÃÑ ÆäÀÌÁö ¼ö : 3224
![]()
|
Facebook Joinc ±×·ì
Joinc QA »çÀÌÆ®
![]()
Tweet
joinc´Â Firefox¿Í chrome¿¡¼ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù. 1 ÀڷᱸÁ¶¸¦ ÀÍÈ÷ÀÚ!!
ÀÛ¼ºÀÚ: mwyun(¸Û)
ÇÁ·Î±×·¡¹Ö ºÐ¾ß¿¡¼ ÀڷᱸÁ¶ÀÇ Áß¿äÇÔÀº ´©±¸³ª ¾Æ´Â »ç½ÇÀÔ´Ï´Ù.
½ÇÁ¦ ¸¹ÀÌ È°¿ëÇÏ´Â ²À ÇÊ¿äÇÑ ÀÚ·á ±¸Á¶ ¸î°¡ÁöÀÇ ¼Ò°³¿Í ÇÔ²² Ȱ¿ëºÐ¾ß¿¡ ´ëÇØ ¼³¸íµå¸®°Ú½À´Ï´Ù. 2 ¸µÅ©µå ¸®½ºÆ®
½ºÅðú Å¥¸¦ ¼³¸íÇϱâ Àü¿¡ ¸µÅ©µå ¸®½ºÆ®¸¦ ¸ÕÀú ¼³¸íÇϰڽÀ´Ï´Ù.
ÀÌ ¸µÅ©µå ¸®½ºÆ®¸¦ ±â¹ÝÀ¸·Î ³ªÁß¿¡ ³ª¿Ã ½ºÅðú Å¥¿¡ Ȱ¿ëÇÏ°Ô µÇ¹Ç·Î Àß ÀÌÇØÇϰí ÀÖ¾î¾ß ÇÕ´Ï´Ù.
¸µÅ©µå ¸®½ºÆ®´Â ¸»±×´ë·Î °¢ ¿ø¼Ò°¡ ¿¬°áµÈ ¸®½ºÆ® ÇüÅ·Π°ü¸®µÇ´Â ÀڷᱸÁ¶ ÀÔ´Ï´Ù. °¢ ¿ø¼Ò´Â ´ÙÀ½ ¿¬°áµÈ ¿ø¼ÒÀÇ À§Ä¡¸¦ ¾Ë°í ÀÖ¾î¾ß Çϴµ¥, Æ÷ÀÎÅ͸¦ ÅëÇØ¼ ´ÙÀ½ ¿ø¼ÒÀÇ À§Ä¡¸¦ ¾Ë¾Æ³»°Ô µË´Ï´Ù.
¸µÅ©µå ¸®½ºÆ®´Â ¾î¶»°Ô ¿¬°áµÇ¾ú´ÂÁö¿¡ µû¶ó¼, ¼±Çü ¸µÅ©µå ¸®½ºÆ®, ´õºí(ÀÌÁß)¸µÅ©µå ¸®½ºÆ®, ȯÇü ¸µÅ©µå ¸®½ºÆ®°¡ ÀÖ½À´Ï´Ù.
2.1 ¼Ò ½º
list.h #ifndef _LIST_H
#define _LIST_H
#include <stdlib.h>
// List Element ±¸Á¶Ã¼
typedef struct ListElmt_ {
void *data; // µ¥ÀÌÅÍ: ŸÀÔ¿¡ Á¦ÇÑÀÌ ¾ø´Â void *»ç¿ë
struct ListElmt_ *next; // ´ÙÀ½ ³ëµå¸¦ °¡¸®Å°´Â Æ÷ÀÎÅÍ(´ÜÀÏ ¿¬°á ¸®½ºÆ®´Â ¸µÅ© Æ÷ÀÎÅͰ¡ ÇѰ³ÀÌ´Ù)
} ListElmt;
// List ±¸Á¶Ã¼
typedef struct List_ {
int size;
int (*match)(const void *key1, const void *key2); // ÇÔ¼öÆ÷ÀÎÅÍ
void (*destroy)(void *data); // ÇÔ¼öÆ÷ÀÎÅÍ
ListElmt *head; // ¸®½ºÆ®ÀÇ Ã³À½ ³ëµå¸¦ °¡¸®Å°´Â Æ÷ÀÎÅÍ
ListElmt *tail; // ¸®½ºÆ®ÀÇ ¸¶Áö¸· ³ëµå¸¦ °¡¸®Å°´Â Æ÷ÀÎÅÍ
} List;
void list_init(List *list, void (*destroy)(void *data));
void list_destroy(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data);
#define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define list_data(element) ((element)->data)
#define list_next(element) ((element)->next)
#endif
list.c #include <stdlib.h> #include <string.h> #include "list.h" // List ÃʱâÈ void list_init(List *list, void (*destroy)(void *data)) { list->size = 0; list->destroy = destroy; list->head = 0; list->tail = 0; return; } // List ÆÄ±â(¸Þ¸ð¸® ÇØÁ¦) void list_destroy(List *list) { void *data; // ListÀÇ size°¡ 0º¸´Ù Å©¸é element°¡ ³²¾ÆÀÖÀ¸¹Ç·Î »èÁ¦ÀÛ¾÷ ÁøÇà while (list_size(list) > 0) { // 2¹øÂ° ÀÎÀÚ°¡ NULLÀ̸é head element »èÁ¦ if (list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) { // free(data); list->destroy(data); } } memset(list, 0, sizeof(List)); return; } // ListÀÇ element ´ÙÀ½¿¡ »õ·Î¿î element Ãß°¡ int list_ins_next(List *list, ListElmt *element, const void *data) { ListElmt *new_element; if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL) return -1; new_element->data = (void *)data; // element°¡ NULLÀ̸é if (element == NULL) { // List¿¡ element°¡ ÇÑ °³µµ ¾øÀ¸¸é if (list_size(list) == 0) list->tail = new_element; new_element->next = list->head; // head element ¾Õ¿¡ Ãß°¡ list->head = new_element; } else // element ´ÙÀ½¿¡ »õ·Î¿î element¸¦ Ãß°¡ { if (element->next == NULL) list->tail = new_element; // new_elementÀÇ ´ÙÀ½ element·Î element->next¸¦ ÀúÀå new_element->next = element->next; // element ´ÙÀ½¿¡ new_element Ãß°¡ element->next = new_element; } list->size++; // ListÀÇ size Áõ°¡ return 0; } // ListÀÇ element ´ÙÀ½ÀÇ element¸¦ »èÁ¦ int list_rem_next(List *list, ListElmt *element, void **data) { ListElmt *old_element; // ListÀÇ size°¡ 0À̸é(Áï, element°¡ ÇѰ³µµ ¾øÀ¸¸é...) if (list_size(list) == 0) return -1; // element°¡ NULLÀ̸é ListÀÇ head element »èÁ¦ if (element == NULL) { *data = list->head->data; old_element = list->head; list->head = list->head->next; // head element¿¡ headÀÇ ´ÙÀ½ element¸¦ ÀúÀå if (list_size(list) == 1) list->tail = NULL; } // ±×·¸Áö ¾ÊÀ¸¸é else { // »èÁ¦ÇÒ element°¡ ¾øÀ¸¸é if (element->next == NULL) return -1; *data = element->next->data; // »èÁ¦ÇÒ elementÀÎ element->next¸¦ ÀúÀå old_element = element->next; // element->nextÀº »èÁ¦ÇÒ element°¡ °¡¸®Å°´Â ´ÙÀ½ element¸¦ ÀúÀå element->next = element->next->next; // »èÁ¦ÇÒ elementÀÇ ´ÙÀ½ element°¡ ¾ø´Â °æ¿ì if (element->next == NULL) list->tail = element; } // element »èÁ¦ free(old_element); list->size--; // ListÀÇ size °¨¼Ò return 0; } 2.2 ¿¹ Á¦
linkedlist.c #include <stdio.h> #include <stdlib.h> #include "list.h" // ListÀÇ ¸ðµç element Ãâ·Â static void print_list(const List *list) { ListElmt *element; int *data, i; fprintf(stdout, "List size is %d\n", list_size(list)); i = 0; // ListÀÇ head ÀúÀå element = list_head(list); while (1) { // elementÀÇ data ÀúÀå data = list_data(element); fprintf(stdout, "list[%03d] = %03d\n", i, *data); i++; if (list_is_tail(element)) // element°¡ tailÀ̸é break break; else element = list_next(element); // ±×·¸Áö ¾ÊÀ¸¸é ´ÙÀ½ element ÀúÀå } return; } // ¸ÞÀÎÇÔ¼ö int main(int argc, char *argv[]) { List list; ListElmt *element; int *data, i; // List ÃʱâÈ list_init(&list, free); element = list_head(&list); // List¿¡ element Ãß°¡(1~10) for (i = 10; i > 0; i--) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i; // List¿¡ element Ãß°¡ // 2¹øÂ° ÀÎÀÚ°¡ NULLÀ̹ǷΠ»õ·Î¿î element´Â head ¾Õ¿¡ Ãß°¡ if (list_ins_next(&list, NULL, data) != 0) return 1; } // List´Â data=1ÀÌ head, data=10ÀÌ tailÀÌ µÈ´Ù. print_list(&list); // head element ÀúÀå element = list_head(&list); for (i = 0; i < 7; i++) element = list_next(element); // data ÀúÀå data = list_data(element); fprintf(stdout, "Removing an element after the one containing %03d\n", *data); // 8¹øÂ° ´ÙÀ½ element »èÁ¦ if (list_rem_next(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 011 at the tail of the list\n"); *data = 11; // ListÀÇ tail element ´ÙÀ½¿¡ element¿¡ Ãß°¡ if (list_ins_next(&list, list_tail(&list), data) != 0) return 1; print_list(&list); // head element ´ÙÀ½ element »èÁ¦ fprintf(stdout, "Removing an element after the first element\n"); element = list_head(&list); if (list_rem_next(&list, element, (void **)&data) != 0) return 1; print_list(&list); // head element ¾Õ¿¡ element Ãß°¡ fprintf(stdout, "Inserting 012 at the head of the list\n"); *data = 12; if (list_ins_next(&list, NULL, data) != 0) return 1; print_list(&list); // 4¹øÂ° element »èÁ¦ fprintf(stdout, "Inserting and removing the fourth element\n"); element = list_head(&list); element = list_next(element); element = list_next(element); // elementÀÇ ´ÙÀ½ element »èÁ¦ if (list_rem_next(&list, element, (void **)&data) != 0) return 1; print_list(&list); // head element ´ÙÀ½¿¡ element Ãß°¡ fprintf(stdout, "Inserting 013 after the first element\n"); *data = 13; if (list_ins_next(&list, list_head(&list), data) != 0) return 1; print_list(&list); // ListÀÇ head, tail element°¡ ¿Ã¹Ù¸¥Áö ºñ±³ i = list_is_head(&list, list_head(&list)); fprintf(stdout, "Testing list is head value = %d (1=OK)\n", i); i = list_is_head(&list, list_tail(&list)); fprintf(stdout, "Testing list is head value = %d (0=OK)\n", i); i = list_is_tail(list_tail(&list)); fprintf(stdout, "Testing list is tail value = %d (1=OK)\n", i); i = list_is_tail(list_head(&list)); fprintf(stdout, "Testing list is tail value = %d (0=OK)\n", i); fprintf(stdout, "Destroying the list\n"); list_destroy(&list); return 0; } 2.3 ½Ç Çà[0058][mwyun@mwyun:~/Project/linkedlist_ex]$ gcc -o linkedlist linkedlist.c list.c [0058][mwyun@mwyun:~/Project/linkedlist_ex]$ ./linkedlist List size is 10 list[000] = 001 <=== head list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 009 list[009] = 010 <=== tail Removing an element after the one containing 008 List size is 9 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 Inserting 011 at the tail of the list List size is 10 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 list[009] = 011 Removing an element after the first element List size is 9 list[000] = 001 list[001] = 003 list[002] = 004 list[003] = 005 list[004] = 006 list[005] = 007 list[006] = 008 list[007] = 010 list[008] = 011 Inserting 012 at the head of the list List size is 10 list[000] = 012 list[001] = 001 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 list[009] = 011 Inserting and removing the fourth element List size is 9 list[000] = 012 list[001] = 001 list[002] = 003 list[003] = 005 list[004] = 006 list[005] = 007 list[006] = 008 list[007] = 010 list[008] = 011 Inserting 013 after the first element List size is 10 list[000] = 012 list[001] = 013 list[002] = 001 list[003] = 003 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 list[009] = 011 Testing list is head value = 1 (1=OK) Testing list is head value = 0 (0=OK) Testing list is tail value = 1 (1=OK) Testing list is tail value = 0 (0=OK) Destroying the list [0059][mwyun@mwyun:~/Project/linkedlist_ex]$ 3 ¸µÅ©µå ¸®½ºÆ®¸¦ ÀÌ¿ëÇÑ ½ºÅÃ
´ÙÀ½Àº ¸µÅ©µå ¸®½ºÆ®¸¦ ±â¹ÝÀ¸·Î ½ºÅÃÀ» ¸¸µé¾î Ȱ¿ëÇÏ´Â ¿¹ÀÔ´Ï´Ù. 3.1 ¼Ò ½º
stack.h #ifndef STACK_H #define STACK_H #include <stdlib.h> #include "list.h" typedef List Stack; #define stack_init list_init #define stack_destroy list_destroy int stack_push(Stack *stack, const void *data); int stack_pop(Stack *stack, void **data); // ½ºÅÿ¡ top element¿¡ µ¥ÀÌÅͰ¡ ÀÖ´ÂÁö °Ë»ç #define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data) #define stack_size list_size #endif
stack.c #include <stdlib.h> #include "list.h" #include "stack.h" // ½ºÅÿ¡ ÀúÀå(¹Ð¾î³Ö±â) int stack_push(Stack *stack, const void *data) { return list_ins_next(stack, NULL, data); // head element ¾Õ¿¡ Ãß°¡ } // ½ºÅÿ¡¼ ²¨³»±â int stack_pop(Stack *stack, void **data) { return list_rem_next(stack, NULL, data); // head element »èÁ¦ } 3.2 ¿¹ Á¦
stack_ex.c #include <stdio.h> #include <stdlib.h> #include "list.h" #include "stack.h" static void print_stack(const Stack *stack) { ListElmt *element; int *data, size, i; fprintf(stdout, "Stack size is %d\n", size = stack_size(stack)); i = 0; element = list_head(stack); while (i < size) { data = list_data(element); fprintf(stdout, "stack[%03d] = %03d\n", i, *data); element = list_next(element); i++; } return; } int main(int argc, char *argv[]) { // ½ºÅà º¯¼ö Á¤ÀÇ Stack stack; int *data, i; // ½ºÅà ÃʱâÈ stack_init(&stack, free); // 10°³ÀÇ element ÀúÀå fprintf(stdout, "Pushing 10 elements\n"); for (i = 0; i < 10; i++) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i + 1; // ½ºÅÿ¡ ÀúÀå if (stack_push(&stack, data) != 0) return 1; } // ½ºÅà Ãâ·Â print_stack(&stack); // 5°³ element ²¨³»±â fprintf(stdout, "Popping 5 elements\n"); for (i = 0; i < 5; i++) { if (stack_pop(&stack, (void **)&data) == 0) free(data); else return 1; } // ½ºÅà Ãâ·Â print_stack(&stack); // 100, 200À» ½ºÅÿ¡ ÀúÀå fprintf(stdout, "Pushing 100 and 200\n"); // 100À» ½ºÅÿ¡ ÀúÀå if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 100; if (stack_push(&stack, data) != 0) return 1; // 200À» ½ºÅÿ¡ ÀúÀå if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 200; if (stack_push(&stack, data) != 0) return 1; // ½ºÅà Ãâ·Â print_stack(&stack); // ½ºÅÃÀÇ top element¿¡ µ¥ÀÌÅͰ¡ ÀÖ´ÂÁö °Ë»ç if ((data = stack_peek(&stack)) != NULL) // ÀÖÀ¸¸é data Ãâ·Â fprintf(stdout, "Peeking at the top element... value = %03d\n", *data); else // ¾øÀ¸¸é fprintf(stdout, "Peeking at the top element... value = NULL\n"); // ½ºÅà Ãâ·Â print_stack(&stack); // ¸ðµç element ²¨³»±â fprintf(stdout, "Popping all elements\n"); // stack size°¡ 0ÀÏ ¶§±îÁö ¹Ýº¹ ½ÇÇà while (stack_size(&stack) > 0) { // ½ºÅÃÀÇ element ²¨³»±â if (stack_pop(&stack, (void **)&data) == 0) free(data); } // ½ºÅà Ãâ·Â print_stack(&stack); // ½ºÅà ¿³º¸±â if ((data = stack_peek(&stack)) != NULL) // ½ºÅÃÀÌ empty°¡ ¾Æ´Ï¸é data Ãâ·Â fprintf(stdout, "Peeking at an empty stack... value = %03d\n", *data); else // ½ºÅÃÀÌ empty(ºñ¾ú´Ù)¶ó°í Ãâ·Â fprintf(stdout, "Peeking at an empty stack... value = NULL\n"); // ½ºÅà Ãâ·Â print_stack(&stack); // ½ºÅà Á¦°Å fprintf(stdout, "Destroying the stack\n"); stack_destroy(&stack); return 0; } 3.3 ½Ç Çà[2340][mwyun@mwyun:~/Project/stack]$ gcc -c stack_ex.c stack.c ../linkedlist_ex/list.c -I../linkedlist_ex [2340][mwyun@mwyun:~/Project/stack]$ gcc -o stack_ex stack_ex.o stack.o list.o [2341][mwyun@mwyun:~/Project/stack]$ ./stack_ex Pushing 10 elements Stack size is 10 stack[000] = 010 <=== head(top) stack[001] = 009 stack[002] = 008 stack[003] = 007 stack[004] = 006 stack[005] = 005 stack[006] = 004 stack[007] = 003 stack[008] = 002 stack[009] = 001 <== tail(bottom) Popping 5 elements Stack size is 5 stack[000] = 005 stack[001] = 004 stack[002] = 003 stack[003] = 002 stack[004] = 001 Pushing 100 and 200 Stack size is 7 stack[000] = 200 stack[001] = 100 stack[002] = 005 stack[003] = 004 stack[004] = 003 stack[005] = 002 stack[006] = 001 Peeking at the top element... value = 200 Stack size is 7 stack[000] = 200 stack[001] = 100 stack[002] = 005 stack[003] = 004 stack[004] = 003 stack[005] = 002 stack[006] = 001 Popping all elements Stack size is 0 Peeking at an empty stack... value = NULL Stack size is 0 Destroying the stack 4 ¹è¿À» ÀÌ¿ëÇÑ ½ºÅÃ
¸µÅ©µå ¸®½ºÆ®¸¦ ÀÌ¿ëÇÑ ½ºÅú¸´Ù ¹è¿À» ÀÌ¿ëÇÑ ½ºÅÃÀÇ ±¸ÇöÀº À¯¿¬¼ºÀ̳ª È¿¿ë¼º¸é¿¡¼´Â ¸µÅ©µå ¸®½ºÆ®º¸´Ù ¶³¾îÁöÁö¸¸ ºü¸¥ ½ÇÇà¼Óµµ¿Í ±¸ÇöÀÇ ´Ü¼øÇÔ µîÀÇ ÀåÁ¡ÀÌ ÀÖ½À´Ï´Ù.
½ºÅÃÀº ÀÏ¹Ý »ýȰ¿¡¼ ½±°Ô º¼ ¼ö ÀÖ´Â ±¸Á¶ÀÔ´Ï´Ù. ¼·ù´õ¹Ì Á¤µµ·Î »ý°¢ÇÏ¸é µÇ°Ú±º¿ä. 󸮸¦ À§ÇÑ ¼·ù´õ¹Ì(½ºÅÃ)°¡ ÀÖÀ» ¶§ ¿ì¸®´Â °¡Àå À§¿¡ ÀÖ´Â ¼·ù ºÎÅÍ ²¨³»¾î¼ ó¸®ÇÒ °Ì´Ï´Ù. ÈçÈ÷ LIFO(ÈÄÀÔ¼±Ãâ)ÀÇ ÀڷᱸÁ¶¶ó°í ÇÕ´Ï´Ù.
½ºÅÿ¡ ´ëÇÑ Á»´õ Çй®?ÀûÀÎ ³»¿ëÀº stack µ¥ÀÌŸ Ãß»óÀ» Âü°íÇϱ⠹ٶõ´Ù. 4.1 ¼Ò ½º
stack2.h #ifndef STACK2_H
#define STACK2_H
#define STACKSIZE 10
typedef struct _stack {
int top;
int items[STACKSIZE];
} stack;
int push(stack *s, int value);
void print_stack(stack *s);
void stack_init(stack *s);
int auto_push(stack *s, int begin, int end);
int auto_pop(stack *s, int count);
#endif
stack2.c #include "stack2.h" // ½ºÅà ÃʱâÈ void stack_init(stack *s) { int i; s->top = -1; for (i = 0; i < STACKSIZE; i++) s->items[i] = 0; } // ½ºÅÿ¡ ÀúÀå int push(stack *s, int value) { if (s->top == STACKSIZE-1) { printf("stack overflow!!\n"); return -1; } s->top++; // top element¸¦ Çϳª Áõ°¡½ÃŲ´Ù. s->items[s->top] = value; // »õ·Î¿î top element¿¡ value ÀúÀå // printf("s->top = %d\n", s->top); return value; } // ½ºÅÿ¡¼ ²¨³»±â int pop(stack *s) { int value; //printf("s->top = %d\n", s->top); if (s->top == -1) { printf("stack empty!!\n"); return -1; } value = s->items[s->top]; // top elementÀÇ value¸¦ ²¨³½´Ù. // s->items[s->top] = 0; s->top--; // top element¸¦ ²¨³ÂÀ¸¹Ç·Î topÀ» -1½ÃŲ´Ù. return value; } 4.2 ¿¹ Á¦
stack2_ex.c #incldue "stack2.h" // ÁöÁ¤ÇÑ ¹üÀ§ ³»ÀÇ ¼ýÀÚ¸¦ ½ºÅÿ¡ Push int auto_push(stack *s, int begin, int end) { int i; int value; for (i = begin; i <= end; i++) // push 1~5 { value = push(s, i); if (value == -1) return -1; else printf("pushed value = %3d\n", value); } // ½ºÅà Ãâ·Â print_stack(s); return 1; } // ÁöÁ¤ÇÑ °³¼ö¸¸Å element¸¦ ½ºÅÿ¡¼ Pop int auto_pop(stack *s, int count) { int i; int value; for (i = 1; i <= count; i++) { value = pop(s); if (value == -1) return -1; else printf("poped value = %3d\n", value); } // ½ºÅà Ãâ·Â print_stack(s); return 1; } int main(int argc, char *argv[]) { // ½ºÅà º¯¼ö Á¤ÀÇ stack s; int i, value; // ½ºÅà ÃʱâÈ stack_init(&s); // ½ºÅà Å×½ºÆ® auto_push(&s, 1, 5); // push 1~5 auto_pop(&s, 2); // pop 2 element auto_push(&s, 6, 10); // push 6~10 auto_pop(&s, 6); // pop 6 element auto_push(&s, 11, 14); // push 11~14 return 0; } 4.3 ½Ç Çà[2351][mwyun@mwyun:~/Project/stack2]$ gcc -c stack2_ex.c stack2.c [2351][mwyun@mwyun:~/Project/stack2]$ gcc -o stack2_ex stack2_ex.o stack2.o [2351][mwyun@mwyun:~/Project/stack2]$ ./stack2_ex pushed value = 1 pushed value = 2 pushed value = 3 pushed value = 4 pushed value = 5 stack size = 5 stack[ 4] = 5 <=== top stack[ 3] = 4 stack[ 2] = 3 stack[ 1] = 2 stack[ 0] = 1 <== bottom poped value = 5 poped value = 4 stack size = 3 stack[ 2] = 3 stack[ 1] = 2 stack[ 0] = 1 pushed value = 6 pushed value = 7 pushed value = 8 pushed value = 9 pushed value = 10 stack size = 8 stack[ 7] = 10 stack[ 6] = 9 stack[ 5] = 8 stack[ 4] = 7 stack[ 3] = 6 stack[ 2] = 3 stack[ 1] = 2 stack[ 0] = 1 poped value = 10 poped value = 9 poped value = 8 poped value = 7 poped value = 6 poped value = 3 stack size = 2 stack[ 1] = 2 stack[ 0] = 1 pushed value = 11 pushed value = 12 pushed value = 13 pushed value = 14 stack size = 6 stack[ 5] = 14 <=== top stack[ 4] = 13 stack[ 3] = 12 stack[ 2] = 11 stack[ 1] = 2 stack[ 0] = 1 <=== bottom [2351][mwyun@mwyun:~/Project/stack2]$ 5 templateÀ» ÀÌ¿ëÇÑ ½ºÅÃ
template class¸¦ ±¸ÇöÇØº¸¾Ò½À´Ï´Ù.
templateÀº ÄÄÆÄÀÏ Å¸ÀÓ¿¡ Á¤ÀûÀ¸·Î ŸÀÔÀÌ °áÁ¤µÇ¾î ¹ÙÀεùµË´Ï´Ù.
±×·¯¹Ç·Î templateÀ¸·Î ±¸ÇöµÈ Ŭ·¡½ºÀÇ ¸ðµç ¸Þ¼ÒµåÀÇ ±¸ÇöÀº Çì´õÆÄÀÏ(.h)¿¡ Á¤ÀǵǾî¾ß ÇÕ´Ï´Ù.
g++ȯ°æ¿¡¼ ÄÄÆÄÀÏÀ» ÇÏ½Ã¸é µÇ±¸¿ä VC++¿¡¼µµ Á¤»óÀûÀ¸·Î µ¿ÀÛÇÏ´Â °ÍÀ» È®ÀÎÇÏ¿´½À´Ï´Ù. 5.1 ¼Ò ½º
tstack.h #ifndef _TSTACK_H_
#define _TSTACK_H_
// ½ºÅà Ŭ·¡½ºÀÇ ±¸Á¶ Á¤ÀÇ¹× ¸â¹öÇÔ¼öÀÇ ±¸Çö
#include <iostream.h>
const int DefaultSize = 80;
enum Boolean { FALSE = 0, TRUE = 1 };
template <class KeyType>
class Stack
{
private:
int top; // ½ºÅÃÀÇ topÀ» ³ªÅ¸³»´Â À妽º
KeyType *stack; // ½ºÅÃÀ» °¡¸®Å°´Â Æ÷ÀÎÅÍ
int MaxSize; // ½ºÅÃÀÇ Å©±â
public:
Stack(int MaxStackSize = DefaultSize); // °´Ã¼»ý¼ºÀÚ
~Stack(); // °´Ã¼ÆÄ±«ÀÚ
KeyType* Add(const KeyType& x); // ½ºÅÃÀÇ top¿¡ ÇØ´ç °ªÀ» ÀúÀå
KeyType* Delete(KeyType& x); // ½ºÅÃÀÇ top¿¡¼ ÇØ´ç °ªÀ» »©³¿
Boolean IsFull(); // ½ºÅÃÀÌ °¡µæÃ¡´Â°¡¸¦ üũ
Boolean IsEmpty(); // ½ºÅÃÀÌ ºñ¾ú´Â°¡¸¦ üũ
KeyType* GetStackTop(KeyType& x);
int GetTop() { return top; };
void Print(void);
};
template <class KeyType>
Stack<KeyType>::Stack(int MaxStackSize):MaxSize(MaxStackSize)
{
stack = new KeyType[MaxSize];
top = -1;
}
template <class KeyType>
Stack<KeyType>::~Stack()
{
delete stack;
}
template <class KeyType>
Boolean Stack<KeyType>::IsFull()
{
if (top == MaxSize-1) return TRUE;
else return FALSE;
}
template <class KeyType>
Boolean Stack<KeyType>::IsEmpty()
{
if (top == -1) return TRUE;
else return FALSE;
}
template <class KeyType>
KeyType *Stack<KeyType>::Add(const KeyType& x)
{
if (IsFull()) return;
stack[++top] = x;
return (KeyType *)&x;
}
template <class KeyType>
KeyType* Stack<KeyType>::Delete(KeyType& x)
{
if (IsEmpty()) return 0;
x = stack[top--];
return &x;
}
template <class KeyType>
KeyType* Stack<KeyType>::GetStackTop(KeyType& x);
{
if (IsEmpty()) return 0;
x = stack[top];
return &x;
}
#endif
5.2 ¿¹ Á¦
stack3_ex.cpp #include "tstack.h"
template <class KeyType> int auto_push(Stack<KeyType> &s, int begin = 0, int end = 0)
{
int i;
int value;
for (i = begin; i <= end; i++)
{
value = *s.Add(i);
if (value)
{
cout << "pushed value = " << value << endl;
}
else
{
cout << "stack full" << endl;
return 0;
}
}
s.Print();
return 1;
}
template <class KeyType> int auto_pop(Stack<KeyType> &s, int count = 0)
{
int i;
int value;
for (i = 1; i <= count; i++)
{
value = i;
value = *s.Delete(value);
if (value)
{
cout << "poped value = " << value << endl;
}
else
{
cout << "stack empty" << endl;
return 0;
}
}
s.Print();
return 1;
}
int main(int argc, char *argv[])
{
Stack<int> stack;
if (!auto_push(stack, 1, 5)) return -1;
if (!auto_pop(stack, 2)) return -1;
if (!auto_push(stack, 6, 10)) return -1;
if (!auto_pop(stack, 6)) return -1;
if (!auto_push(stack, 11, 14)) return -1;
return 0;
}
5.3 ½Ç Çà[2358][mwyun@mwyun:~/Project/stack3]$ g++ -c -g -Wno-deprecated stack3_ex.cpp [2358][mwyun@mwyun:~/Project/stack3]$ g++ -o stack3_ex stack3_ex.o [2358][mwyun@mwyun:~/Project/stack3]$ ./stack3_ex pushed value = 1 pushed value = 2 pushed value = 3 pushed value = 4 pushed value = 5 stack size = 5 stack[4] = 5 stack[3] = 4 stack[2] = 3 stack[1] = 2 stack[0] = 1 poped value = 5 poped value = 4 stack size = 3 stack[2] = 3 stack[1] = 2 stack[0] = 1 pushed value = 6 pushed value = 7 pushed value = 8 pushed value = 9 pushed value = 10 stack size = 8 stack[7] = 10 stack[6] = 9 stack[5] = 8 stack[4] = 7 stack[3] = 6 stack[2] = 3 stack[1] = 2 stack[0] = 1 poped value = 10 poped value = 9 poped value = 8 poped value = 7 poped value = 6 poped value = 3 stack size = 2 stack[1] = 2 stack[0] = 1 pushed value = 11 pushed value = 12 pushed value = 13 pushed value = 14 stack size = 6 stack[5] = 14 stack[4] = 13 stack[3] = 12 stack[2] = 11 stack[1] = 2 stack[0] = 1 [2358][mwyun@mwyun:~/Project/stack3]$ 6 ¸µÅ©µå ¸®½ºÆ®¸¦ ÀÌ¿ëÇÑ Å¥
´ÙÀ½Àº ¸µÅ©µå ¸®½ºÆ®¸¦ ±â¹ÝÀ¸·Î Å¥¸¦ ¸¸µé¾î Ȱ¿ëÇÏ´Â ¿¹ÀÔ´Ï´Ù. 6.1 ¼Ò ½º
queue.h #ifndef QUEUE_H #define QUEUE_H #include "list.h" typedef List Queue; #define queue_init list_init #define queue_destroy list_destroy int queue_enqueue(Queue *queue, const void *data); int queue_dequeue(Queue *queue, void **data); // Å¥ÀÌ head element¿¡ µ¥ÀÌÅͰ¡ ÀÖ´ÂÁö °Ë»ç #define queue_peek(queue) ((queue)->head == NULL ? NULL : (queue)->head->data) #define queue_size list_size #endif
queue.c #include <stdlib.h> #include "list.h" #include "queue.h" // Å¥ÀÇ tail ´ÙÀ½¿¡ element ÀúÀå int queue_enqueue(Queue *queue, const void *data) { return list_ins_next(queue, list_tail(queue), data); } // Å¥ÀÇ head element ²¨³»±â int queue_dequeue(Queue *queue, void **data) { return list_rem_next(queue, NULL, data); } 6.2 ¿¹ Á¦
queue_ex.c #include <stdio.h> #include <stdlib.h> #include "list.h" #include "queue.h" static void print_queue(const Queue *queue) { ListElmt *element; int *data, size, i; fprintf(stdout, "Queue size is %d\n", size = queue_size(queue)); i = 0; element = list_head(queue); while (i < size) { data = list_data(element); fprintf(stdout, "queue[%03d] = %03d\n", i, *data); element = list_next(element); i++; } return; } int main(int argc, char *argv[]) { // Å¥ º¯¼ö Á¤ÀÇ Queue queue; int *data, i; // Å¥ ÃʱâÈ queue_init(&queue, free); // 10°³ÀÇ element ÀúÀå fprintf(stdout, "Enqueuing 10 elements\n"); for (i = 0; i < 10; i++) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i + 1; // Å¥¿¡ ÀúÀå if (queue_enqueue(&queue, data) != 0) return 1; } // Å¥ Ãâ·Â print_queue(&queue); // 5°³ element ²¨³»±â fprintf(stdout, "Dequeuing 5 elements\n"); for (i = 0; i < 5; i++) { if (queue_dequeue(&queue, (void **)&data) == 0) free(data); else return 1; } // 100, 200À» Å¥¿¡ ÀúÀå fprintf(stdout, "Enqueuing 100 and 200\n"); // 100À» Å¥¿¡ ÀúÀå if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 100; if (queue_enqueue(&queue, data) != 0) return 1; // Å¥ Ãâ·Â print_queue(&queue); // 200À» Å¥¿¡ ÀúÀå if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 200; if (queue_enqueue(&queue, data) != 0) return 1; // Å¥ Ãâ·Â print_queue(&queue); // Å¥ÀÇ head element¿¡ µ¥ÀÌÅͰ¡ ÀÖ´ÂÁö °Ë»ç if ((data = queue_peek(&queue)) != NULL) fprintf(stdout, "Peeking at the head element... value = %03d\n", *data); else fprintf(stdout, "Peeking at the head element... value = NULL\n"); // Å¥ Ãâ·Â print_queue(&queue); // ¸ðµç element ²¨³»±â fprintf(stdout, "Dequeuing all elements\n"); // queue size°¡ 0ÀÏ ¶§±îÁö ¹Ýº¹ ½ÇÇà while (queue_size(&queue) > 0) { // Å¥ÀÇ element ²¨³»±â if (queue_dequeue(&queue, (void **)&data) == 0) free(data); } // Å¥ ¿³º¸±â if ((data = queue_peek(&queue)) != NULL) // Å¥°¡ empty°¡ ¾Æ´Ï¸é data Ãâ·Â fprintf(stdout, "Peeking at an empty queue... value = %03d\n", *data); else // Å¥°¡ empty(ºñ¾ú´Ù)¶ó°í Ãâ·Â fprintf(stdout, "Peeking at an empty queue... value = NULL\n"); // Å¥ Á¦°Å fprintf(stdout, "Destroying the queue\n"); queue_destroy(&queue); return 0; } 6.3 ½Ç Çà[0046][mwyun@mwyun:~/Project/queue]$ gcc -c queue_ex.c queue.c ../linkedlist_ex/list.c -I../linkedlist_ex [0046][mwyun@mwyun:~/Project/queue]$ gcc -o queue_ex queue_ex.o queue.o list.o [0046][mwyun@mwyun:~/Project/queue]$ ./queue_ex Enqueuing 10 elements Queue size is 10 queue[000] = 001 <=== head(top) queue[001] = 002 queue[002] = 003 queue[003] = 004 queue[004] = 005 queue[005] = 006 queue[006] = 007 queue[007] = 008 queue[008] = 009 queue[009] = 010 <=== tail(bottom) Dequeuing 5 elements Enqueuing 100 and 200 Queue size is 6 queue[000] = 006 queue[001] = 007 queue[002] = 008 queue[003] = 009 queue[004] = 010 queue[005] = 100 Queue size is 7 queue[000] = 006 queue[001] = 007 queue[002] = 008 queue[003] = 009 queue[004] = 010 queue[005] = 100 queue[006] = 200 Peeking at the head element... value = 006 Queue size is 7 queue[000] = 006 queue[001] = 007 queue[002] = 008 queue[003] = 009 queue[004] = 010 queue[005] = 100 queue[006] = 200 Dequeuing all elements Peeking at an empty queue... value = NULL Destroying the queue [0046][mwyun@mwyun:~/Project/queue]$ 7 ¹è¿À» ÀÌ¿ëÇÑ Å¥
¸µÅ©µå ¸®½ºÆ®¸¦ ÀÌ¿ëÇÑ Å¥º¸´Ù ¹è¿À» ÀÌ¿ëÇÑ Å¥ÀÇ ±¸ÇöÀº À¯¿¬¼ºÀ̳ª È¿¿ë¼º¸é¿¡¼´Â ¸µÅ©µå ¸®½ºÆ®º¸´Ù ¶³¾îÁöÁö¸¸ ºü¸¥ ½ÇÇà¼Óµµ¿Í ±¸ÇöÀÇ ´Ü¼øÇÔ µîÀÇ ÀåÁ¡ÀÌ ÀÖ½À´Ï´Ù.
º» Àå¿¡¼ÀÇ ¼Ò½º´Â ȯÇüÅ¥ÀÇ Á¦ÀÏ °£´ÜÇÑ ¿¹¸¦ µç °ÍÀÔ´Ï´Ù. 7.1 ¼Ò ½º
queue2.h #ifndef QUEUE2_H
#define QUEUE2_H
#define MAXQUEUE 10
typedef struct _queue {
int front;
int rear;
int items[MAXQUEUE];
} queue;
int queue_enqueue(queue *q, int value);
int queue_dequeue(queue *q);
void print_queue(queue *q);
void queue_init(queue *q);
int auto_enqueue(queue *q, int begin, int end);
int auto_dequeue(queue *q, int count);
#endif
queue2.c #include "queue2.h" // Å¥ ÃʱâÈ void queue_init(queue *q) { int i; q->front = -1; q->rear = -1; for (i = 0; i < MAXQUEUE; i++) q->items[i] = 0; } // Å¥¿¡ rear¿¡ element Ãß°¡ int queue_enqueue(queue *q, int value) { // Å¥°¡ °¡µæÂù °æ¿ì if ((q->front == 0 && q->rear == MAXQUEUE - 1) || (q->rear + 1 == q->front)) { printf("queue full!!\n"); return -1; } // rear°ªÀÌ Å¥ ¹öÆÛÀÇ ÃÖ´ë Å©±âÀ̸é // Å¥ ¹öÆÛÀÇ ³²Àº °ø°£À» Ȱ¿ëÇϱâ À§ÇØ rear¸¦ -1·Î ÃʱâÈ(ȯÇüÅ¥) if (q->rear == MAXQUEUE - 1) q->rear = -1; // 1Áõ°¡(rear´Â Ç×»ó 1Áõ°¡ÇÏ¸é¼ ¹öÆÛ¸¦ Ȱ¿ëÈÄ ÃÖ´ë Å©±â(MAXQUEUE-1)°¡ µÇ¸é 0ºÎÅÍ ´Ù½Ã ½ÃÀÛÇÑ´Ù. q->rear++; q->items[q->rear] = value; printf("front = %d, rear = %d ---> ", q->front, q->rear); return value; } // Å¥ÀÇ front element ¿ø¼Ò »èÁ¦ int queue_dequeue(queue *q) { int value; // Å¥°¡ ºó °æ¿ì if (q->front == q->rear) { printf("queue empty!!\n"); return -1; } // front°ªÀÌ Å¥ ¹öÆÛÀÇ ÃÖ´ë Å©±âÀ̸é // Å¥ ¹öÆÛÀÇ ³²Àº °ø°£À» Ȱ¿ëÇϱâ À§ÇØ front¸¦ -1·Î ÃʱâÈ(ȯÇüÅ¥) if (q->front == MAXQUEUE - 1) q->front = -1; // 1Áõ°¡(front´Â Ç×»ó 1Áõ°¡ÇÏ¸é¼ ¹öÆÛ¸¦ Ȱ¿ëÈÄ ÃÖ´ë Å©±â(MAXQUEUE-1)°¡ µÇ¸é 0ºÎÅÍ ´Ù½Ã ½ÃÀÛÇÑ´Ù. q->front++; // 1Áõ°¡ value = q->items[q->front]; q->items[q->front] = 0; printf("front = %d, rear = %d ---> ", q->front, q->rear); return value; }
front¿Í rear´Â Ç×»ó À妽º°ªÀÌ Áõ°¡ÇÏ´Ù°¡(°°Àº ¹æÇâÀ¸·Î À妽º À̵¿) Å¥ÀÇ ÃÖ´ë Å©±â(MAXQUEUE-1)°¡ µÇ¸é ´Ù½Ã óÀ½ À妽º(0¹ø)ºÎÅÍ ´Ù½Ã ½ÃÀÛÇÑ´Â °ÍÀ» º¸¿©ÁÖ¸ç À̰ÍÀº ÀüÇüÀûÀΠȯÇüÅ¥ÀÇ ¿¹ÀÔ´Ï´Ù.
ȯÇü´Â ÀÌ¹Ì »ç¿ëÇÑ ³²¾ÆÀÖ´Â ºó °ø°£Àº Ȱ¿äÇÒ ¼ö ÀÖ´Ù´Â ÀåÁ¡ÀÌ ÀÖ½À´Ï´Ù.
Å¥º¸´Ù ´õ¿í È¿À²ÀûÀÔ´Ï´Ù. 7.2 ¿¹ Á¦
queue2_ex.c #include "queue2.h" void print_queue(queue *q) { int i; for (i = 0; i < MAXQUEUE; i++) { printf("queue[%3d] = %3d\n", i, q->items[i]); } printf("\n"); } // ÁöÁ¤ÇÑ ¹üÀ§ ³»ÀÇ ¼ýÀÚ¸¦ Å¥¿¡ Ãß°¡ int auto_enqueue(queue *q, int begin, int end) { int i; int value; for (i = begin; i <= end; i++) // enqueue 1~5 { value = queue_enqueue(q, i); if (value == -1) return -1; else printf("enqueued value = %3d\n", value); } // Å¥ Ãâ·Â print_queue(q); return 1; } // ÁöÁ¤ÇÑ °³¼ö¸¸Å element¸¦ Å¥¿¡¼ »èÁ¦ int auto_dequeue(queue *q, int count) { int i; int value; for (i = 1; i <= count; i++) { value = queue_dequeue(q); if (value == -1) return -1; else printf("dequeued value = %3d\n", value); } // Å¥ Ãâ·Â print_queue(q); return 1; } int main(int argc, char *argv[]) { // Å¥ º¯¼ö Á¤ÀÇ queue q; // Å¥ ÃʱâÈ queue_init(&q); // Å¥ Å×½ºÆ® auto_enqueue(&q, 1, 5); auto_dequeue(&q, 4); auto_enqueue(&q, 6, 12); auto_dequeue(&q, 5); auto_enqueue(&q, 13, 14); return 0; } 7.3 ½Ç Çà[0022][mwyun@mwyun:~/Project/queue2]$ gcc -c queue2_ex.c queue2.c [0022][mwyun@mwyun:~/Project/queue2]$ gcc -o queue2_ex queue2_ex.o queue2.o [0022][mwyun@mwyun:~/Project/queue2]$ ./queue2_ex front = -1, rear = 0 ---> enqueued value = 1 front = -1, rear = 1 ---> enqueued value = 2 front = -1, rear = 2 ---> enqueued value = 3 front = -1, rear = 3 ---> enqueued value = 4 front = -1, rear = 4 ---> enqueued value = 5 queue[ 0] = 1 queue[ 1] = 2 queue[ 2] = 3 queue[ 3] = 4 queue[ 4] = 5 <=== rear queue[ 5] = 0 <--------------+- unused buffer queue[ 6] = 0 | queue[ 7] = 0 | queue[ 8] = 0 | queue[ 9] = 0 <--------------+ front = 0, rear = 4 ---> dequeued value = 1 front = 1, rear = 4 ---> dequeued value = 2 front = 2, rear = 4 ---> dequeued value = 3 front = 3, rear = 4 ---> dequeued value = 4 queue[ 0] = 0 <--------------+- unused buffer queue[ 1] = 0 | queue[ 2] = 0 | queue[ 3] = 0 <=== front <---+ queue[ 4] = 5 <=== rear queue[ 5] = 0 <--------------+-unused buffer queue[ 6] = 0 | queue[ 7] = 0 | queue[ 8] = 0 | queue[ 9] = 0 <--------------+ front = 3, rear = 5 ---> enqueued value = 6 front = 3, rear = 6 ---> enqueued value = 7 front = 3, rear = 7 ---> enqueued value = 8 front = 3, rear = 8 ---> enqueued value = 9 front = 3, rear = 9 ---> enqueued value = 10 front = 3, rear = 0 ---> enqueued value = 11 front = 3, rear = 1 ---> enqueued value = 12 queue[ 0] = 11 queue[ 1] = 12 <=== rear queue[ 2] = 0 <--------------+- unused buffer queue[ 3] = 0 <=== front <---+ queue[ 4] = 5 queue[ 5] = 6 queue[ 6] = 7 queue[ 7] = 8 queue[ 8] = 9 queue[ 9] = 10 front = 4, rear = 1 ---> dequeued value = 5 front = 5, rear = 1 ---> dequeued value = 6 front = 6, rear = 1 ---> dequeued value = 7 front = 7, rear = 1 ---> dequeued value = 8 front = 8, rear = 1 ---> dequeued value = 9 queue[ 0] = 11 queue[ 1] = 12 <=== rear queue[ 2] = 0 <--------------+- unused buffer queue[ 3] = 0 | queue[ 4] = 0 | queue[ 5] = 0 | queue[ 6] = 0 | queue[ 7] = 0 | queue[ 8] = 0 <=== front <---+ queue[ 9] = 10 front = 8, rear = 2 ---> enqueued value = 13 front = 8, rear = 3 ---> enqueued value = 14 queue[ 0] = 11 queue[ 1] = 12 queue[ 2] = 13 queue[ 3] = 14 <=== rear queue[ 4] = 0 <---------------+- unused buffer queue[ 5] = 0 | queue[ 6] = 0 | queue[ 7] = 0 | queue[ 8] = 0 <=== front <----+ queue[ 9] = 10 [0022][mwyun@mwyun:~/Project/queue2]$
¹è¿ÀÇ »ç¿ëÇÏ¿© Å¥¸¦ ±¸ÇöÇÏ´Â °æ¿ì ¸µÅ©µå ¸®½ºÆ®¿Í´Â ´Þ¸® ºó °ø°£À» ³¶ºñÇÒ ¼ö ÀÖ½À´Ï´Ù.
±×·¡¼ ÀϹÝÀûÀΠť°¡ ¾Æ´Ñ ȯÇüÅ¥¸¦ ¿¹¸¦ µç °ÍÀÔ´Ï´Ù.
¹°·Ð ¸µÅ©µå ¸®½ºÆ®·Îµµ ȯÇüÅ¥¸¦ ±¸ÇöÇÒ ¼ö ÀÖ½À´Ï´Ù.
front¿Í rearÀÇ Áõ°¡ ¹æÇâÀ» Àߺ¸¸é ºó °ø°£ÀÇ È°¿ëÀ» ¾î¶»°Ô ÇÏ´ÂÁö ¾Ë ¼ö ÀÖÀ¸¸ç ȯÇüÅ¥ÀÇ ÀÛµ¿¿ø¸®¸¦ ÀÌÇØÇÒ ¼ö ÀÖ½À´Ï´Ù. 8 ´õºí ¸µÅ©µå ¸®½ºÆ®
´ÜÀÏ ¸µÅ©µå ¸®½ºÆ®´Â ¸µÅ©°¡ ÇѰ³¸¸ Á¸ÀçÇϳª ´õºí ¸µÅ©µå ¸®½ºÆ®´Â ¸» ±×´ë·Î ¸µÅ©°¡ µÎ°³°¡ Á¸ÀçÇÕ´Ï´Ù.
Áï ³ëµå´Â ´ÙÀ½ ³ëµå¸¦ ¸µÅ©ÇÏ´Â Æ÷ÀÎÅÍ¿Í ÀÌÀü ³ëµå¸¦ ¸µÅ©ÇÏ´Â Æ÷ÀÎÅ͸¦ °¡Áö°Ô µË´Ï´Ù.
´ÜÀÏ ¸µÅ©µå ¸®½ºÆ®´Â ´ÙÀ½ ³ëµå¸¦ ¸µÅ©ÇÏ´Â Æ÷ÀÎÅ͸¸ °¡Áö¹Ç·Î ´Ü¹æÇâÀÌÁö¸¸, ´õºí ¸µÅ©µå ¸®½ºÆ®´Â ¾ç¹æÇâÀ̶ó°í º¸½Ã¸é µË´Ï´Ù.
³ëµå Ž»öÀÌ ÈÙ¾À À¯¸®ÇÕ´Ï´Ù. 8.1 ¼Ò ½º
dlist.h #ifndef _DLIST_H
#define _DLIST_H
#include <stdlib.h>
typedef struct DListElmt_ {
void *data;
struct DListElmt_ *prev; // ÀÌÀü elementÀÇ Æ÷ÀÎÅÍ
struct DListElmt_ *next; // ´ÙÀ½ elementÀÇ Æ÷ÀÎÅÍ
} DListElmt;
typedef struct DList_ {
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
DListElmt *head; // head element
DListElmt *tail; // tail element
} DList;
void dlist_init(DList *list, void (*destroy)(void *data));
void dlist_destroy(DList *list);
int dlist_ins_next(DList *list, DListElmt *element, const void *data);
int dlist_ins_prev(DList *list, DListElmt *element, const void *data);
int dlist_remove(DList *list, DListElmt *element, void **data);
#define dlist_size(list) ((list)->size)
#define dlist_head(list) ((list)->head)
#define dlist_tail(list) ((list)->tail)
#define dlist_is_head(list, element) ((element) == (list)->head ? 1 : 0)
#define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define dlist_data(element) ((element)->data)
#define dlist_prev(element) ((element)->prev)
#define dlist_next(element) ((element)->next)
#endif
dlist.c #include <stdlib.h> #include <string.h> #include "dlist.h" // DList ÃʱâÈ void dlist_init(DList *list, void (*destroy)(void *data)) { list->size = 0; list->destroy = destroy; list->head = 0; list->tail = 0; return; } // DList ÆÄ±â(¸Þ¸ð¸® ÇØÁ¦) void dlist_destroy(DList *list) { void *data; while (dlist_size(list) > 0) { if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->destroy != NULL) { list->destroy(data); } } memset(list, 0, sizeof(DList)); return; } // DListÀÇ element ´ÙÀ½¿¡ »õ·Î¿î element Ãß°¡ int dlist_ins_next(DList *list, DListElmt *element, const void *data) { DListElmt *new_element; if (element == NULL && dlist_size(list) != 0) return -1; // »õ·Î¿î element »ý¼º if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) return -1; new_element->data = (void *)data; // DList¿¡ element°¡ ÇÑ °³µµ ¾øÀ¸¸é if (dlist_size(list) == 0) // ù element Ãß°¡ { list->head = new_element; // head¿¡ ÀúÀå list->head->prev = NULL; list->head->next = NULL; list->tail = new_element; // tail¿¡ ÀúÀå } // element ´ÙÀ½¿¡ »õ·Î¿î element Ãß°¡ else { // ¾ç¹æÇâ ¿¬°áÀ̱⠶§¹®¿¡ next, prev °ªÀ» Á¤È®È÷ ÀúÀåÇØ¾ß ¸µÅ©°¡ ²÷±âÁö ¾Ê´Â´Ù. new_element->next = element->next; new_element->prev = element; // element°¡ tailÀÌ¸é »õ·Î¿î element¸¦ tail¿¡ ÀúÀå if (element->next == NULL) list->tail = new_element; else element->next->prev = new_element; // element->nextÀÇ elementÀÇ prev¿¡ »õ·Î¿î element ÀúÀå // element->nextÀº »õ·Î¿î element¸¦ ÀúÀå element->next = new_element; } list->size++; // DListÀÇ size Áõ°¡ return 0; } // DListÀÇ element ÀÌÀü¿¡ »õ·Î¿î element Ãß°¡ int dlist_ins_prev(DList *list, DListElmt *element, const void *data) { DListElmt *new_element; if (element == NULL && dlist_size(list) != 0) return -1; // »õ·Î¿î element »ý¼º if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) return -1; new_element->data = (void *)data; // DList¿¡ element°¡ ÇÑ °³µµ ¾øÀ¸¸é if (dlist_size(list) == 0) // ù element Ãß°¡ { list->head = new_element;// head¿¡ ÀúÀå list->head->prev = NULL; list->head->next = NULL; list->tail = new_element; // tail¿¡ ÀúÀå } // element ÀÌÀü¿¡ »õ·Î¿î element Ãß°¡ else { // ¾ç¹æÇâ ¿¬°áÀ̱⠶§¹®¿¡ next, prev °ªÀ» Á¤È®È÷ ÀúÀåÇØ¾ß ¸µÅ©°¡ ²÷±âÁö ¾Ê´Â´Ù. new_element->next = element; new_element->prev = element->prev; // element°¡ headÀÌ¸é »õ·Î¿î element¸¦ head¿¡ ÀúÀå if (element->prev== NULL) list->head = new_element; else // element->prevÀÇ elementÀÇ next¿¡ »õ·Î¿î element ÀúÀå element->prev->next = new_element; // element->prev´Â »õ·Î¿î element¸¦ ÀúÀå element->prev = new_element; } list->size++; // DListÀÇ size Áõ°¡ return 0; } // DListÀÇ element »èÁ¦ int dlist_remove(DList *list, DListElmt *element, void **data) { DListElmt *old_element; if (element == NULL|| dlist_size(list) == 0) return -1; *data = element->data; // element°¡ headÀ̸é if (element == list->head) { list->head = element->next; // element ´ÙÀ½ element¸¦ head¿¡ ÀúÀå if (list->head == NULL) // element°¡ ÇÑ °³µµ ¾ø´Â °æ¿ì list->tail = NULL; // tailµµ NULL else // element->next°¡ ÀÖ´Ù¸é ±× elementÀÇ prev¸¦ NULL·Î ÀúÀå element->next->prev = NULL; } // ±×·¸Áö ¾ÊÀ¸¸é else { // ÀÌÀü elementÀÇ next´Â elementÀÇ next¸¦ ÀúÀå element->prev->next = element->next; // element->next°¡ NULLÀ̸é if (element->next == NULL) list->tail = element->prev; // element->prev¸¦ tail¿¡ ÀúÀå else element->next->prev = element->prev; } free(element); // element ¸Þ¸ð¸® ÇØÁ¦ list->size--; // DListÀÇ size °¨¼Ò return 0; } 8.2 ¿¹ Á¦
dlist_ex.c #include <stdio.h> #include <stdlib.h> #include "dlist.h" // DListÀÇ ¸ðµç element Ãâ·Â static void print_list(const DList *list) { DListElmt *element; int *data, i; fprintf(stdout, "DList size is %d\n", dlist_size(list)); i = 0; // DListÀÇ head ÀúÀå element = dlist_head(list); while (1) { // elementÀÇ data ÀúÀå data = dlist_data(element); fprintf(stdout, "list[%03d] = %03d\n", i, *data); i++; if (dlist_is_tail(element)) // element°¡ tailÀ̸é break break; else element = dlist_next(element); // ±×·¸Áö ¾ÊÀ¸¸é ´ÙÀ½ element ÀúÀå } return; } // ¸ÞÀÎ ÇÔ¼ö int main(int argc, char *argv[]) { DList list; DListElmt *element; int *data, i; // DList ÃʱâÈ dlist_init(&list, free); element = dlist_head(&list); // DList¿¡ element Ãß°¡(10~1) for (i = 10; i > 0; i--) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i; // DList¿¡ element Ãß°¡ // 2¹øÂ° ÀÎÀÚ°¡ headÀ̹ǷΠ»õ·Î¿î element´Â head ¾Õ¿¡ Ãß°¡ if (dlist_ins_prev(&list, dlist_head(&list), data) != 0) return 1; } // DList Ãâ·Â print_list(&list); element = dlist_head(&list); // 9¹øÂ° element »èÁ¦ for (i = 0; i < 8; i++) element = dlist_next(element); data = dlist_data(element); fprintf(stdout, "Removing an element after the one containing %03d\n", *data); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; // DList Ãâ·Â print_list(&list); // tail ´ÙÀ½¿¡ »õ·Î¿î element Ãß°¡ fprintf(stdout, "Inserting 011 at the tail of the list\n"); *data = 11; if (dlist_ins_next(&list, dlist_tail(&list), data) != 0) return 1; // DList Ãâ·Â print_list(&list); // tail element »èÁ¦ fprintf(stdout, "Removing an element at the tail of the list\n"); element = dlist_tail(&list); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; // DList Ãâ·Â print_list(&list); // tail ÀÌÀü¿¡ »õ·Î¿î element Ãß°¡ fprintf(stdout, "Inserting 012 just before the tail of list\n"); *data = 12; if (dlist_ins_prev(&list, dlist_tail(&list), data) != 0) return 1; // DList Ãâ·Â print_list(&list); // ³×¹øÂ° element »èÁ¦ fprintf(stdout, "Interatin and removing the fourth element\n"); // prev, next¸¦ ÀÌ¿ëÇØ¼ element À̵¿ element = dlist_head(&list); element = dlist_next(element); element = dlist_prev(element); element = dlist_next(element); element = dlist_prev(element); element = dlist_next(element); element = dlist_next(element); element = dlist_next(element); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; // DList Ãâ·Â print_list(&list); // ù¹øÂ° element(head) ¾Õ¿¡ »õ·Î¿î element Ãß°¡ fprintf(stdout, "Inserting 013 before the first element\n"); *data = 13; if (dlist_ins_prev(&list, dlist_head(&list), data) != 0) return 1; // DList Ãâ·Â print_list(&list); // head element »èÁ¦ fprintf(stdout, "Removing an element at the head of the list\n"); if (dlist_remove(&list, dlist_head(&list), (void **)&data) != 0) return 1; // DList Ãâ·Â print_list(&list); // head ´ÙÀ½¿¡ »õ·Î¿î element Ãß°¡ fprintf(stdout, "Inserting 014 just after the head of the list\n"); *data = 14; if (dlist_ins_next(&list, dlist_head(&list), data) != 0) return 1; // DList Ãâ·Â print_list(&list); // 2¹øÂ° element ´ÙÀ½¿¡ »õ·Î¿î element Ãß°¡ fprintf(stdout, "Inserting 015 two element after the head of the list\n"); if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 15; element = dlist_head(&list); element = dlist_next(element); if (dlist_ins_next(&list, element, data) != 0) return 1; // DList Ãâ·Â print_list(&list); // DListÀÇ head, tail element°¡ ¿Ã¹Ù¸¥Áö ºñ±³ i = dlist_is_head(&list, dlist_head(&list)); fprintf(stdout, "Testing list is head value = %d (1=OK)\n", i); i = dlist_is_head(&list, dlist_tail(&list)); fprintf(stdout, "Testing dlist is head value = %d (0=OK)\n", i); i = dlist_is_tail(dlist_tail(&list)); fprintf(stdout, "Testing dlist is tail value = %d (1=OK)\n", i); i = dlist_is_tail(dlist_head(&list)); fprintf(stdout, "Testing dlist is tail value = %d (0=OK)\n", i); // DList Á¦°Å fprintf(stdout, "Destroying the list\n"); dlist_destroy(&list); return 0; } 8.3 ½Ç Çà[0120][mwyun@mwyun:~/Project/double_linked_list_ex]$ gcc -c dlist.c dlist_ex.c [0120][mwyun@mwyun:~/Project/double_linked_list_ex]$ gcc -o dlist_ex dlist_ex.o dlist.o [0120][mwyun@mwyun:~/Project/double_linked_list_ex]$ ./dlist_ex DList size is 10 list[000] = 001 <=== head list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 009 list[009] = 010 <=== tail Removing an element after the one containing 009 DList size is 9 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 Inserting 011 at the tail of the list DList size is 10 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 list[009] = 011 Removing an element at the tail of the list DList size is 9 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 010 Inserting 012 just before the tail of list DList size is 10 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 004 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 012 list[009] = 010 Interatin and removing the fourth element DList size is 9 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 005 list[004] = 006 list[005] = 007 list[006] = 008 list[007] = 012 list[008] = 010 Inserting 013 before the first element DList size is 10 list[000] = 013 list[001] = 001 list[002] = 002 list[003] = 003 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 012 list[009] = 010 Removing an element at the head of the list DList size is 9 list[000] = 001 list[001] = 002 list[002] = 003 list[003] = 005 list[004] = 006 list[005] = 007 list[006] = 008 list[007] = 012 list[008] = 010 Inserting 014 just after the head of the list DList size is 10 list[000] = 001 list[001] = 014 list[002] = 002 list[003] = 003 list[004] = 005 list[005] = 006 list[006] = 007 list[007] = 008 list[008] = 012 list[009] = 010 Inserting 015 two element after the head of the list DList size is 11 list[000] = 001 list[001] = 014 list[002] = 015 list[003] = 002 list[004] = 003 list[005] = 005 list[006] = 006 list[007] = 007 list[008] = 008 list[009] = 012 list[010] = 010 Testing list is head value = 1 (1=OK) Testing dlist is head value = 0 (0=OK) Testing dlist is tail value = 1 (1=OK) Testing dlist is tail value = 0 (0=OK) Destroying the list [0120][mwyun@mwyun:~/Project/double_linked_list_ex]$ 8.4 ±× ¸²
±×¸²À¸·Î º¸¸é ´õ¿í ÀÌÇØ°¡ ºü¸£¹Ç·Î ´ÙÀ½ 3°³ÀÇ ÇÔ¼ö¿¡ ´ëÇØ µµ½ÄÈÇÏ¿´½À´Ï´Ù.
dlist_ins_next ÇÔ¼ö
dlist_ins_prev ÇÔ¼ö
dlist_remove ÇÔ¼ö
9 ÇÁ·ÎÁ§Æ®
´ÙÀ½ °Á¸¦ ÂüÁ¶ÇÏ½Ã¸é µË´Ï´Ù. 10 Âü°í¹®Çå
°Á·Π¿Ã¸° ¼Ò½º´Â ´ÙÀ½ ¼Àû¿¡ ½Ç¸° ¿¹Á¦·Î ¾Ë°í ÀÖ½À´Ï´Ù.
C·Î ±¸ÇöÇÑ ¾Ë°í¸®Áò, Ä«ÀÏ ·çµç(Kyle Loudon) Àú/Çã¿í ¿ª, ÇѺû¹Ìµð¾î |
|
|
|
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù. |
|