연결리스트 소스인데 자세히 설명좀 해주세요.....
/*****************************************************************************
* *
* -------------------------------- list.h -------------------------------- *
* *
*****************************************************************************/
#ifndef LIST_H
#define LIST_H
#include
/*****************************************************************************
* Define a structure for linked list elements. *
* 연결리스트 요소들 구조체 정의 *
*****************************************************************************/
typedef struct ListElmt_
{
void *data;
struct ListElmt_ *next;
} ListElmt;
/*****************************************************************************
* Define a structure for linked lists. *
* 연결리스트 구조체 정의 *
*****************************************************************************/
typedef struct List_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
ListElmt *head;
ListElmt *tail;
}List;
/*****************************************************************************
* --------------------------- Public Interface --------------------------- *
* 퍼블릭 인터패이스 *
*****************************************************************************/
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
#include
#include "list.h"
/*****************************************************************************
* *
* ------------------------------- list_init ------------------------------ *
* *
*****************************************************************************/
////////////////////////////////////Start/////////////////////////////////////
void list_init(List *list, void (*destroy)(void *data))
{
/*****************************************************************************
* Initialize the list. *
* 리스트를 초기화 하다 *
*****************************************************************************/
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
return;
}
/*****************************************************************************
* ----------------------------- list_destroy ----------------------------- *
* 리스트를 없애다. *
*****************************************************************************/
////////////////////////////////////Start/////////////////////////////////////
void list_destroy(List *list)
{
void *data;
/*****************************************************************************
* Remove each element. *
* 각 요소를 지우다 *
*****************************************************************************/
while (list_size(list) > 0)
{
if (list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL)
{
/***********************************************************************
* Call a user-defined function to free dynamically allocated data. *
* 사용자가 정의한 동적 메모리 해제 함수를 호출한다. *
***********************************************************************/
list->destroy(data);
}
}
/*****************************************************************************
* No operations are allowed now, but clear the structure as a precaution. *
* *
*****************************************************************************/
memset(list, 0, sizeof(List));
return;
}
/*****************************************************************************
* *
* ----------------------------- list_ins_next ---------------------------- *
* *
*****************************************************************************/
////////////////////////////////////Start/////////////////////////////////////
int list_ins_next(List *list, ListElmt *element, const void *data)
{
ListElmt *new_element;
/*****************************************************************************
* *
* Allocate storage for the element. *
* *
*****************************************************************************/
if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
return -1;
/*****************************************************************************
* *
* Insert the element into the list. *
* *
*****************************************************************************/
new_element->data = (void *)data;
if(element == NULL)
{
/**************************************************************************
* *
* Handle insertion at the head of the list. *
* *
**************************************************************************/
if(list_size(list) == 0)
list->tail = new_element;
new_element->next = list->head;
list->head = new_element;
}
else
{
/**************************************************************************
* *
* Handle insertion somewhere other than at the head. *
* *
**************************************************************************/
if(element->next == NULL)
list->tail = new_element;
new_element->next = element->next;
element->next = new_element;
}
/*****************************************************************************
* *
* Adjust the size of the list to account for the inserted element. *
* *
*****************************************************************************/
list->size++;
return 0;
}
/*****************************************************************************
* *
* ----------------------------- list_rem_next ---------------------------- *
* *
*****************************************************************************/
////////////////////////////////////Start/////////////////////////////////////
int list_rem_next(List *list, ListElmt *element, void **data)
{
ListElmt *old_element;
/*****************************************************************************
* *
* Do not allow removal from an empty list. *
* *
*****************************************************************************/
if(list_size(list) == 0)
return -1;
/*****************************************************************************
* *
* Remove the element from the list. *
* *
*****************************************************************************/
if (element == NULL)
{
/**************************************************************************
* *
* Handle removal from the head of the list. *
* *
**************************************************************************/
*data = list->head->data;
old_element = list->head;
list->head = list->head->next;
if(list_size(list) == 1)
list->tail = NULL;
}
else
{
/**************************************************************************
* *
* Handle removal from somewhere other than the head. *
* *
**************************************************************************/
if(element->next == NULL)
return -1;
*data = element->next->data;
old_element = element->next;
element->next = element->next->next;
if(element->next == NULL)
list->tail = element;
}
/*****************************************************************************
* *
* Free the storage allocated by the abstract data type. *
* *
*****************************************************************************/
free(old_element);
/*****************************************************************************
* *
* Adjust the size of the list to account for the removed element. *
* *
*****************************************************************************/
list->size--;
return 0;
}
어떻게 해 주기를
어떻게 해 주기를 원하시는거죠?
일단 연결리스트가 뭔지부터 공부하시면 이정도는 알수있을것같습니다만.
--------------------------
未來よどうか無限につづけ.
미래여 영원히 계속되어다오.
그리고 하나만
그리고 하나만 남기고 나머지 글은 좀 지웠으면 좋을것같네요..
--------------------------
未來よどうか無限につづけ.
미래여 영원히 계속되어다오.
연결리스트는 c랑
연결리스트는 c랑 c++로 다짜봤는데 (큐, 스택등등)
근데 포인터랑 함수포인터가 나오닌까 소스가 잘 이해가 안되요
연결리스트는 c랑
연결리스트는 c랑 c++로 다짜봤는데 (큐, 스택등등)
근데 포인터랑 함수포인터가 나오닌까 소스가 잘 이해가 안되요
댓글 달기