연결리스트 공부중인 학생입니다 . segmentation fault 뜬걸보니 메모리 관련 문제같은데, 코드보시고 조언좀 부탁드려요

yaeho12의 이미지

#include
#include
static int a=0;
struct address{
int b;
struct address *next;
};

void print_list(struct address *h);
struct address* add_linkedlist(struct address *h);
struct address* first_adr(void);
int main()
{
int i,res=0;
struct address *head;
struct address *new;
head=first_adr();

for(i=0;i<10;i++){
new=add_linkedlist(head);
}
while(head!=NULL){
printf("%d\n",new->b);
}

}
struct address* first_adr(void)
{
struct address *new;
new=(struct address*)malloc(sizeof(struct address));
new->next=NULL;
return new;
}
struct address * add_linkedlist(struct address *h)
{
struct address *t;
struct address *new;
t=h;//head gojung
new=(struct address*)malloc(sizeof(struct address));
new->b=a;
a++;
new->next=NULL;
while(t !=NULL)
t=t->next;
t->next=new;
return new;
}/*
void print_list(struct address *h)
{
struct address *p;
p=h->next;
while(p!=NULL){
printf("%d",p->b);
p=p->next;
}
}*/

익명 사용자의 이미지

while(t !=NULL)
    t=t->next;
t->next=new;

while문이 종료되고 나면 t는 NULL이겠군요. NULL을 역참조하여 next필드를 접근하려 하니 에러가 날 수밖에요.
linked list의 마지막 원소를 찾는 코드인 것 같은데, while문의 조건을 t가 아니라 t->next로 바꾸면 정상 동작할 겁니다.

단, add_linkedlist으로 넘겨주는 매개변수 h가 NULL이어서는 안 됩니다. 그렇게 문서화하던가, 아니면 함수 안에서 에러 처리를 하던가...

shint의 이미지

★C로 연결리스트 구현하기 ★연결리스트 reference 참조로 구현하기★이중연결리스트 구현하기
http://kldp.org/node/153885

전 예전에 온라인 3D 슈팅 게임 만들기' 책 보고 공부했어요.

----------------------------------------------------------
이렇게 재귀함수로 호출하니. 1000개 이상해보니. 오류가 발생하네요.
포인터를 next 가 아니라. prev 로 부모를 향하도록 구현해야. 편할거 같습니다.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
 
 
using namespace std;
 
#define new pnew
 
 
static int a = 0;
struct address
{
	int b;
	struct address *next;
};
 
void print_list(struct address *h);
struct address* add_linkedlist(struct address *h);
struct address* first_adr(void);
 
struct address* first_adr(void)
{
	struct address *new;
	int size = sizeof(struct address);
	new = (struct address*)malloc(size);
	new->b = a;
	a++;
	new->next = NULL;
	return new;
}
 
struct address * add_linkedlist(struct address *h)
{
	struct address *new;
	int size = sizeof(struct address);
	new = (struct address*)malloc( size );
	new->b = a;
	a++;
	new->next = NULL;
 
	h->next = new;
	return new;
}
 
void print_list(struct address *h)
{
	 struct address *p;
	 p=h;
	 while(p!=NULL)
	 {
		 printf("%x %d\n", p, p->b);
		 p=p->next;
	 }
	 printf("--------------\n");
}
 
void print_list_callback(struct address *h)
{
	struct address *p;
	p = h;
	if (p != NULL)
	{
		printf("[PREV] %x %d\n", p, p->b);
		p = p->next;
		print_list_callback(p);
		printf("--------------\n");
		printf("[NEXT] %x %d\n", h, h->b);
		free(h);
	}
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
 
	int i, res = 0;
	struct address *head;
	struct address *new;
	head = first_adr();
 
	new = head;
	for (i = 0; i<4; i++)
	{
		new = add_linkedlist(new);
	}
 
	print_list(head);
 
	print_list_callback(head);    //여러개 해보니. 오류가 있습니다.
 
	return 0;
}
 
 
 
 
출력
3bae68 0
3c0df0 1
3be5e8 2
3be620 3
3be658 4
--------------
[PREV] 3bae68 0
[PREV] 3c0df0 1
[PREV] 3be5e8 2
[PREV] 3be620 3
[PREV] 3be658 4
--------------
[NEXT] 3be658 4
--------------
[NEXT] 3be620 3
--------------
[NEXT] 3be5e8 2
--------------
[NEXT] 3c0df0 1
--------------
[NEXT] 3bae68 0
계속하려면 아무 키나 누르십시오 . . .

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

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

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

shint의 이미지

void print_list_callback(struct address *h)
{
	struct address *p;
	p = h;
	while (1)
	{
		if (p != NULL)
		{
			struct address *pp;
			pp = p;
 
			printf("[PREV] %x %d\n", p, p->b);
			p = p->next;
 
 
			printf("[NEXT] %x %d\n", pp, pp->b);
 
			free(pp);
			pp = NULL;
		}
		else
		{
			break;
		}
	}
}
 
[PREV] 1eae68 0
[NEXT] 1eae68 0
[PREV] 1f0df0 1
[NEXT] 1f0df0 1
[PREV] 1ee5e8 2
[NEXT] 1ee5e8 2
[PREV] 1ee620 3
[NEXT] 1ee620 3
[PREV] 1ee658 4
[NEXT] 1ee658 4
end
계속하려면 아무 키나 누르십시오 . . .

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

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

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