링크드리스트 관련하여 질문드립니다..ㅠㅠ 3일째 계속 진전이 없어 글올려봅니다!

gol f@Google의 이미지

미리 배정된 메모리공간에 링크드리스트를 이용해 데이터를 추가 삭제 를 구현하는 프로그램 일부인데요 add함수를 계속해서 못구현하고 있어 질문드립니다. compare를 이용해 알파벳순으로 노드를 추가하는 함수여야 하는데 항상 동적할당으로 연결만 해주다가 이미 배정된공간에 노드를 연결하려니 너무 어려워 질문드립니다!
add 함수 에서 freenode와 newnode함수를 사용해서 sort를 이용하지 않고 compare함수를 이용해서 노드간 비교해 알파벳순으로 재현해야되는데 ... 이외 정의된 함수를 변경해서는 안되는데 ...
도와주세요 ㅠㅠ

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct record {
	char name[3];
	char number[4];
	struct record * next;
};
 
struct record pool[5];
struct record * top = pool; 
 
int compare(char key[3], struct record *);
 
 
struct record * data = NULL; 
 
 
struct record * newnode()
{
	구현
}
 
 
void freenode(struct record *r)
{
	구현
}
 
 
void add(char *name, char *number)
{
    미구현
}

shint의 이미지

변수 배열 사용하듯이. 값을 출력해서 확인해보세요. ㅇ_ㅇ;;
http://codepad.org/cWnB6V2f

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct record
{
    char name[3];
    char number[4];
    struct record * next;
};
 
#define POOL_SIZE 5
 
struct record pool[5];
struct record * top = pool;
struct record * tail = pool;
 
int compare(char key[3], struct record *);
 
struct record * data = NULL; 
 
 
void init_pool() 
{
    printf("init_pool()\n");
    int i;
    struct record *r = pool;
    struct record *s;
 
    strcpy(pool[0].name, "tt");
    strcpy(pool[1].name, "bt");
    strcpy(pool[2].name, "st");
    strcpy(pool[3].name, "wt");
 
 
    pool[POOL_SIZE - 1].next = NULL;
 
    for (i = 1; i<POOL_SIZE; i++) 
    {
        s = r++;
	s->next = r;
        printf("s:%x r:%x   s:%s r:%s\n", s, r, s->name, r->name);
    }
}
 
//pop
struct record * newnode()
{
    printf("record * newnode()\n");
    struct record* r;
    if (top == NULL) 
    {
	printf("Pool is empty. Bye!\n");
    }
    r = top;
    top = r->next;
    printf("r:%x top:%x    r:%s top:%s\n", r, top, r->name, top->name);
    return r;
}
 
// Push
void freenode(struct record *r)
{
    printf("freenode(struct record *r)\n");
    r->next = top;
    top = r;
    printf("r:%x top:%x    r:%s top:%s\n", r, top, r->name, top->name);
}
 
void add(char *name, char *number)
{
    printf("add(char *name, char *number)\n");
    tail = tail->next;
    struct record * r = tail;
    strcpy(r->name, name);
    strcpy(r->number, number);
 
    r = top;
    int i;
    struct record *s;
    for (i = 1; i<POOL_SIZE; i++) 
    {
        s = r++;
	s->next = r;
        printf("s:%x r:%x    s:%s r:%s\n", s, r, s->name, r->name);
    }
}
 
int compare(char key[3], struct record *r)
{
    if (r == NULL)
        return -1;
    else
	return strncmp(key, r->name, 3);
}
 
int main()
{
    init_pool();
    struct record * r = newnode();
    freenode(r);
    newnode();
    add("ab","123");
    add("cd","456");
    add("ef","789");
 
    return 0;
}
 
//
init_pool()
s:8049b20 r:8049b2c   s:tt r:bt
s:8049b2c r:8049b38   s:bt r:st
s:8049b38 r:8049b44   s:st r:wt
s:8049b44 r:8049b50   s:wt r:
record * newnode()
r:8049b20 top:8049b2c    r:tt top:bt
freenode(struct record *r)
r:8049b20 top:8049b20    r:tt top:tt
record * newnode()
r:8049b20 top:8049b2c    r:tt top:bt
add(char *name, char *number)
s:8049b2c r:8049b38    s:ab r:st
s:8049b38 r:8049b44    s:st r:wt
s:8049b44 r:8049b50    s:wt r:
s:8049b50 r:8049b5c    s: r:
add(char *name, char *number)
s:8049b2c r:8049b38    s:ab r:cd
s:8049b38 r:8049b44    s:cd r:wt
s:8049b44 r:8049b50    s:wt r:
s:8049b50 r:8049b5c    s: r:
add(char *name, char *number)
s:8049b2c r:8049b38    s:ab r:cd
s:8049b38 r:8049b44    s:cd r:ef
s:8049b44 r:8049b50    s:ef r:
s:8049b50 r:8049b5c    s: r:

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

gol f@Google의 이미지

안녕하세요 참고하여 제가 메인프로그램까지 수정하여 파일을 올려보았는데 미구현 부분 다시한번 질문드립니다 ㅠㅠ
add 안에서 알파벳순으로 노드가 추가되야 하는데 그부분을 도저히 모르겟습니다///

shint의 이미지

일단. 포인터와 변수에 관련된 책 예제를 확인해보세요. ㅇ_ㅇ;;

#include <stdio.h>
 
int main()
{
    char c = 10;
    char * p = NULL;
    printf("p의 주소:%x   p:%x\n", &p, p);
    printf("p의 주소의 크기:%x   p의 크기:%x\n", sizeof(&p), sizeof(p));
 
    //32비트 컴퓨터에서.
    //char == 1바이트 == 8비트
    //char * == 4바이트 == 32비트
    //int == 4바이트 == 32비트
    //int * == 4바이트 == 32비트
    //문자 c의 주소를. 문자형 포인터 주소로 형변환
    p = (char*)&c;
 
    printf("c의 주소:%x   c의 값:%d\n", &c, c);
    printf("c의 주소의 크기:%x   c의 크기:%x\n", sizeof(&c), sizeof(c));
    printf("p의 주소:%x   p:%x    p의 값:%d\n", &p, p, *p);
    printf("p의 주소의 크기:%x   p의 크기:%x    p의 값의 크기:%d\n", sizeof(&p), sizeof(p), sizeof(*p));
    return 0;
}
 
//
p의 주소:fff5ae24   p:0
p의 주소의 크기:4   p의 크기:4
c의 주소:fff5ae2b   c의 값:10
c의 주소의 크기:4   c의 크기:1
p의 주소:fff5ae24   p:fff5ae2b    p의 값:10
p의 주소의 크기:4   p의 크기:4    p의 값의 크기:1

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

gol f@Google의 이미지

안녕하세요 참고하여 제가 메인프로그램까지 수정하여 파일을 올려보았는데 미구현 부분 다시한번 질문드립니다 ㅠㅠ

세벌의 이미지

POOL_SIZE 에러

gcc -Wall -o 

해 봤더니
error: ‘POOL_SIZE’ undeclared (first use in this function)

나오네요?
gol f@Google의 이미지

헉 왜그런건가요?? 사이즈가 왜 에러지..

karkayan의 이미지

일반적인 링크드리스트와 다른점이 거의 없습니다.
새 노드 생성시 malloc대신 newnode를 사용하고, 해제시에 free 대신에 freenode를 사용하는 것만 제외하면 일반 링크드리스트와 다른점이 하나도 없습니다.
결국 add함수가 하는 것은 리스트의 삽입정렬과 동일하네요.
newnode로 새 노드를 생성하고, compare 함수를 통해 올바른 위치를 찾아 새로운 노드를 삽입하면 됩니다.

raymundo의 이미지

위에 karkayan님 말씀처럼, init_pool에서 각 원소들을 next 필드를 통해 연결시키고 있어서 현혹되기 쉽긴 한데, 저건 그저 노드 풀을 관리하기 위한 거고, 실제로 데이터가 들어 있는 리스트는 직접 연결시켜야 하는 것 같습니다. 그러면 malloc 쓸 자리에 newnode 썼다고 생각하고 malloc 쓸 때와 똑같이 하시면 되고... (오히려 과제로는 newnode와 freenode 구현하는 걸 내면 좋을 것 같은데)

다만 newnode()에서 top 이 NULL일 때는 if 블록 안에서 바로 NULL을 반환하도록 고쳐주어야지, 지금 상태에서는 top = r->next 할 때 폴트 나겠네요.

좋은 하루 되세요!

gol f@Google의 이미지

와 진짜 감사해요!!! 아 newNode freenode구현은 제가 직접 한건데 애드 부분만 설명을 들어도 직접 구현하지 못하고 있엇네요 감사합니다

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.