링크드 리스트 & 포인터 질문
Sonya
질문 내용 :
#include stdio.h
#include stdlib.h //전처리 명령어 부분
#include string.h
typedef struct _node{ //링크드 리스트 노드 생성을 위한 typedef 구조체 선언
int value; //값을 저장할 변수 선언
struct _node *next; //다음 노드를 가리키기 위한 자기 참조형 포인터 변수 선언
}node;
node *head; //head라는 이름의 포인터형 구조체 생성
void addfirst(node *list, int x) // list에 첫 번째 노드로 x를 삽입
{
node *proot = list;
node *newnode = (node *)malloc(sizeof(node));
memset(newnode, 0, sizeof(node));
newnode - value = x;
newnode - next = null;
proot - next = newnode;
proot = proot - next;
}
erase(list, x) // list에서 x를 포함한 노드 삭제
{
}
search(list, x) // list가 x를 포함하는 노드를 반환하거나 null을 반환
{
}
replace(list, item1, item2) // list의 item1을 item2로 수정
{
}
display(list) // 리스트의 모든 노드 출력
{
}
void menu_print(void)
{
printf(\n입력 목록\n);
printf(1. 씻기\n);
printf(2. 휴식\n);
printf(3. 등교\n);
printf(4. 하교\n);
printf(5. 공부\n);
printf(6. 레포트\n);
printf(7. 밥\n);
printf(8. 청소\n);
printf(9. 약속\n);
}
int menuchoice(int *x)
{
int select = 0;
int list_select = 0;
do{
printf(1. 삽입\n2. 삭제\n3. 수정\n0. 종료\n);
printf(수행할 명령을 입력해 주세요 : );
scanf(%d, &select);
switch(select){
case 1:
do{
menu_print();
printf(\n);
printf(리스트에 추가할 목록을 선택해 주세요 : );
scanf(%d, &list_select);
if(list_select 1 || list_select 9){
printf(목록중에 하나를 선택하여야 합니다.\n);
}
}while(list_select 1 || list_select 9);
*x = list_select;
return 1;
break;
case 2:
return 2;
break;
case 3:
return 3;
break;
case 0:
return 0;
break;
default:
printf(잘못된 입력!!!\n\n);
break;
}
}while(select != 1 || select != 2 || select != 3 || select != 0);
return 0;
}
int main()
{
int select = 0;
int x;
node *list = head;
select = menuchoice(&x);
switch(select){
case 1:
printf(삽입연산\n);
addfirst(list, x);
break;
case 2:
printf(삭제연산\n);
break;
case 3:
printf(리스트 출력\n);
break;
case 0:
printf(프로그램을 종료합니다.\n);
system(pause);
exit(1);
default:
printf(잘못된 입력!!!\n\n);
break;
}
}
위 코드에서 빨갛게 된 부분을 어떻게 해줘야 될지 모르겠습니다.
정말 포인터만 들어가면 미치겠네요... 그래도 포인터 아주 아주 기본 개념은 알고있다고 생각했었는데...
지금보니 하나도 모르겠네요 ㅠㅠ
리스트가 헤드를 가리키고 있으니... 주소값을 넘겨줘야 되는건가요? ;; 아닌가;;
그리고 이 함수를
void addfirst(node *list, int x) // list에 첫 번째 노드로 x를 삽입
{
node *proot = list;
node *newnode = (node *)malloc(sizeof(node));
memset(newnode, 0, sizeof(node));
newnode - value = x;
newnode - next = null;
proot - next = newnode;
proot = proot - next;
}
이렇게 고쳐도 되는건가요?
void addfirst(node *list, int x) // list에 첫 번째 노드로 x를 삽입
{
node *newnode = (node *)malloc(sizeof(node));
memset(newnode, 0, sizeof(node));
newnode - value = x;
newnode - next = null;
list - next = newnode;
list =list - next;
}