연결리스트 소스인데 자세히 설명좀 해주세요.....

chopisal의 이미지

/*****************************************************************************
* *
* -------------------------------- 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;
}

零Rei의 이미지

어떻게 해 주기를 원하시는거죠?

일단 연결리스트가 뭔지부터 공부하시면 이정도는 알수있을것같습니다만.

--------------------------
未來よどうか無限につづけ.
미래여 영원히 계속되어다오.

零Rei의 이미지

그리고 하나만 남기고 나머지 글은 좀 지웠으면 좋을것같네요..

--------------------------
未來よどうか無限につづけ.
미래여 영원히 계속되어다오.

chopisal의 이미지

연결리스트는 c랑 c++로 다짜봤는데 (큐, 스택등등)

근데 포인터랑 함수포인터가 나오닌까 소스가 잘 이해가 안되요

chopisal의 이미지

연결리스트는 c랑 c++로 다짜봤는데 (큐, 스택등등)

근데 포인터랑 함수포인터가 나오닌까 소스가 잘 이해가 안되요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.