3일이 지나갑니다 단 한줄 s->key값을 절실히.. 모르겟습니다

kldpgk의 이미지

죄송합니다 소스통째로 들고와서 .. 정말 한가지만 알려주십시오..ㅠㅠ

//단순 연결 리스트

#include
#include // free() malloc() 사용

typedef struct _node
{
int key;
struct _node *next;
}node;

node *head, *tail;

void init_list(void)
{
head=(node*)malloc(sizeof(node)); // 메모리 할당
tail=(node*)malloc(sizeof(node));
head->next=tail; // 머리->꼬리
tail->next=tail; // 꼬리->꼬리
}

int delete_next(node *t) // t 다음 노드를 삭제
{
node *s;
if(t->next==tail)
return 0; // 꼬리를 지울 수 없다
s=t->next; // 삭제할 대상 노드
t->next=t->next->next; // 다음 다음 노드
free(s);
return 1;
}

node *insert_after(int k, node* t) // t노드 뒤에 노드 삽입
{
node *s;
s=(node*)malloc(sizeof(node));
s->key=k;
s->next=t->next; // t->next가 가리키던 노드를 s->next에 넣고,
t->next=s; // t->next에 s를 대입한다.
return s;
}

node *find_node(int k) // 인수 k와 키(key)값이 같은 것을 찾는다.
{
node *s;
s=head->next; // 제일 처음부터(head 제외)
while(s->key != k && s != tail) // k가 key값과 같지 않고, 노드의 끝이 아니면
s=s->next; // 다음 노드로 이동,
return s; // key값이거나 노드의 끝이면 리턴(tail)
}

int delete_node(int k)
{
node *s; // 진행 노드
node *p; // 진행 노드의 앞(전) 노드
p=head;
s=p->next;

while(s->key != k && s != tail) // s->key 값이 k와 같지 않고, 노드의 끝이 아니면
{
p=p->next;
s=p->next;
}

if(s!=tail) // (같은 값을 찾았다면)노드의 끝인지 검사
{
p->next=s->next;
free(s);
return 1;
}
else
return 0; // 노드의 끝이라면 리턴
}

node *insert_node(int t, int k) // k의 앞에 t 노드를 삽입
{
node *s;
node *p;
node *r;
p=head;
s=p->next;
while(s->key != k && s != tail)
{
p=p->next;
s=p->next;
}
if(s!=tail) // 만약 찾았으면
{
r=(node*)malloc(sizeof(node));
r->key=t;
p->next=r; // p->r->s 의 순서로 변경
r->next=s;
}
return p->next;
}

node *ordered_insert(int k)
{
node *s;
node *p;
node *r;
p=head;
s=p->next;
while(s->key {
p=p->next;
s=p->next;
}
r=(node*)malloc(sizeof(node));
r->key=k;
p->next=r;
r->next=s;
return r;
}

void print_list(node* t)
{
printf("\n");
while(t!=tail)
{
printf("%-8d", t->key);
t=t->next;
}
}

node *delete_all(void)
{
node *s;
node *t;
t=head->next;
while(t!=tail)
{
s=t;
t=t->next;
free(s);
}
head->next=tail;
return head;
}

int main(void)
{
node *t;

init_list();

ordered_insert(10); // 순서를 유지하면서 삽입
ordered_insert(5);
ordered_insert(8);
ordered_insert(3);
ordered_insert(1);
ordered_insert(7);
ordered_insert(8);

printf("\nInitial Linked list is ");
print_list(head->next);

printf("\nFinding 4 is %s successful", find_node(4) == tail ? "un" : "");

t=find_node(5);
printf("\nFinding 5 is %s successful", t==tail ? "un" : "");

printf("\nInserting 9 after 5");
insert_after(9,t);

print_list(head->next);

t=find_node(10);
printf("\nDeleting next last node");
delete_next(t);
print_list(head->next);

t=find_node(3);
printf("\nDeleting next 3");
delete_next(t);
print_list(head->next);

printf("\nInsert node 2 bofore 3");
insert_node(2,3);
print_list(head->next);

printf("\nDeleting node 2");
if(!delete_node(2))
printf("\n deleting 2 is unsuccessful");
print_list(head->next);

printf("\nDeleting node 1");
delete_node(1);
print_list(head->next);

printf("\nDeleting all node");
delete_all();
print_list(head->next);
}

예 여기서..밑부분에 main이 있습니다

int main (void)
init_list();

ordered_insert(10)

init_list();에서는 머리와 꼬리를 선언해주고
ordered_insert(10) 에서는 10을 인자로 하여 k에 넘겨주고 있습니다
넘겨 받은함수에는..

node *ordered_insert(int k)
{
node *s;
node *p;
node *r;
p=head;
s=p->next;
while(s->key {
p=p->next;
s=p->next;
}

대략 이런 함수가 진행되는거 같은데 여기서
while문에 있는 이뜻을 모르겟습니다.. 이겁니다
s->key

여기서 뭘 s->key는 처음에 초보자의 눈으로 볼때는 어디에 선언 해준게
없어보입니다만 즉 제가볼땐 구조체에 선언된 정수형 s->key값만 선언된걸루
압니다.. 그래서 질문이 있습니다..

"s->key값은 도데체 무엇인가요.." 이걸 몰라 3일을 해매고 있습니다

타 싸이트에 가서도 짧게 글을 올렸는데 (*s).key값이다,비교해서 값에 접근한다

이런말만 해주시니.제가 원하는 답은..
"저것이 가장처음 정수몇수의 값을 같느냐나"
는 질문입니다 이것만 알고 싶습니다

다시 한번 "s->key" 가장처음의 정수값은 무엇을 가르키나요"

좀 도와주세요.. 정말 미치겟습니다..ㅠㅠㅠㅠㅠㅠ

pool007의 이미지

질문을 좀 정성을 갖고 해보세요.. 답답한 마음은 이해하지만.

while(s->keynext;
-> 여기서 닫는 괄호는요?

"while문에 있는 이뜻을 모르겟습니다.. 이겁니다
s->keykey는 처음에 초보자의 눈으로 볼때는 어디에 선언 해준게
없어보입니다."
-> 네.. keynext와 key만 있죠.

"s->key값은 도데체 무엇인가요"
-> 초기값은 garbage죠..

해석을 해보려고 하는데, 제가 보기엔 소스코드가 완전하지 않아보이는데 제대로 다시 올려보세요. 아마 while 조건 부분에 뭔가 중요한게 빠진듯 하네요.

--
Passion is like genius; a miracle.

--
Passion is like genius; a miracle.

kldpgk의 이미지

이제 보이실껍니다 .. ㅠㅠ 어제부터 짤리던데 방법을 몰라서

ed.netdiver의 이미지

s는 node type이네요. 내부 구성이 node의 형태가 될거란걸 명시했습니다.
얘는 integer key와 자기 자신과 똑같이 생긴 넘(아직 실체가 명확하지 않은)을
가리키는 포인터로 구성되어 있군요.

init과정에서 node에 mem alloc을 통해 실체가 규정됩니다.

head는 node의 첫번째를 가리키는 포인터가 되고, s는 head의 pointer를 넘겨받으니, 이제 s도 head를 가리키게 됩니다.
그 s의 key란건 이제 실체를 가지는 head의 key값이 되고, s는 loop를 돌면서 그 다음 node, 그 다음 node 이런 식으로 가리키는 넘을 바꿔가면서 해당 node의 key값을 볼수 있게 됩니다.

과연 헛갈려하는 부분을 언급한건지나 모르겠군요. :)

대신 담버텀은 code 같은걸 이용해보심이 어떨지 싶군요.
사용법:

#include <cstring>
 
int main(void)
...

대체 while이 어디 붙어있는거야? 하면서 눈돌아갔다네요~ㅋㅋ
(이 얘기만 딸랑 할래다 그러면 삐지실까봐 헛소릴 주절^^; )

\(´∇`)ノ \(´∇`)ノ \(´∇`)ノ \(´∇`)ノ
def ed():neTdiVeR in range(thEeArTh)

--------------------------------------------------------------------------------
\(´∇`)ノ \(´∇`)ノ \(´∇`)ノ \(´∇`)ノ
def ed():neTdiVeR in range(thEeArTh)

unipro의 이미지

대충 살펴보니... ordered_insert(10)의 호출을 통해서 들어오는 while(s->key<=k && s!=tail) 내의 s->key 값은 몰라도 됩니다. 처음에 while구문을 만나면, s와 tail의 값이 같아서 종료조건과 일치하기 때문에 while 구문을 빠져나오게 됩니다.

노드가 head와 tail를 가르키면, 이것은 데이타를 저장하는 용도로 쓰이지 않기때문에 key값을 알 필요가 없습니다. (또는 다른 노드와 다르게 다는 용도로 사용하게 될 것입니다.)

내 블로그: http://unipro.tistory.com

kldpgk의 이미지

저게 처음에는 필요없고 증가할마다 마다 다르게 변하는군여..

감사합니다.. 아직 확실히는 이해 하지 못했으나..

정말 감사합니다.... 복받으실꺼예여..

댓글 달기

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