2013-03-11 2 views
0

링크 된리스트를 생성하고 표시하는 프로그램을 만들려고합니다.함수로 링크 된리스트

지금 난 내 create_list에 문제가있어() 함수, 어떤 목록을 작성하지 않습니다.

내가 뭘 잘못하고있어? 영어 나쁜 죄송

:/

CODE :

#include <stdio.h> 
#include <stdlib.h> 

typedef struct node { 
    int data; 
    struct node *next; 
} node; 

int main(){ 

    node *start; 
    start = NULL; 
    int a,n,on = 1; 

    while(on == 1){ 
     printf(" \n choose: \n 1 --- create list \n 2 --- display list \n"); 
     scanf("%d",&n); 
     switch(n){ 
      case 1: 
       printf("-------------------------------------------- \n"); 
       printf(" Enter the elements. The last element is 0 \n"); 
       printf("-------------------------------------------- \n"); 

       Create_list(); 
       Display_list(start); 
       break; 

      case 2: 
       Display_list(start); 
       break; 
     } 
    } 

    system("pause"); 
    return 0; 
} 

void Display_list(node *curr){ 
    if(curr){ 
     while (curr->next != NULL){ 
        printf("%d \n",curr->data); 
        curr=curr->next; 
     } 
    } else { 
     printf(" \n The list is not created ! \n"); 
    } 
} 

void Create_list(node *curr){ 

    int i; 
    node *start = NULL; 



    if (start == NULL){ 
     curr = (node *)malloc(sizeof(node)); 
     start=curr; 

     while (i != 0){ 
      scanf("%d",&i); 
      if(i == 0){ 
       curr->next=NULL; 
       curr=start; 
      } else { 
       curr->data=i; 
       curr->next=(node *)malloc(sizeof(node)); 
       curr=curr->next; 
      } 
     } 

    } else { 
      printf(" \n list already exists ! \n"); 
    } 
}      
+0

당신의 들여 쓰기는 내 눈을 아프게! –

답변

1

함수 Create_List (node ​​* curr)에는 몇 가지 인수가 필요합니다. main()에서 인수를 전달하지 않습니다. 코드가 컴파일 되었습니까?

Create_List (node ​​* curr) 함수에는 몇 가지 인수가 필요합니다. main()에서 인수를 전달하지 않습니다. 코드가 컴파일 되었습니까? 당신이 무엇을해야

는 연결리스트의 첫 번째 노드의 위치를 ​​저장합니다 주에서 노드를 가지고있다.

void Insert(struct node **q, int num) //Num is the data to be added and **q is the pointer to the first node of the list. 
{ 
struct node *temp, *r; 
temp = *q; 
if (*q == NULL) { 
    temp = ((struct node *)malloc(sizeof(struct node))); 
    temp->data = num; 
    temp->link = NULL; 
    *q = temp; 
} 
else { 
    while (temp->link != NULL) 
     temp = temp->link; 

    r = ((struct node *)malloc(sizeof(struct node))); 
    r->data = num; 
    r->link = NULL; 
    temp->link = r; 
} 
} 
+0

네, 1,2,3,0을 입력했을 때 걸었습니다. 그것은 나에게 "목록이 만들어지지 않았습니다!"라는 것을 보여줍니다. –

+0

"main에서 Create_list로 패스 앤드 스타트 ​​(노드 **)를하고 * start를 수정하여리스트 헤드를 설정하십시오." 고 말했다. – SureshS

0

Create_liststartmainstart 관련이 없습니다. 둘 다 각각의 기능에 국한되어 있기 때문에 다른 기능을 볼 수 없습니다. 따라서 start을 설정하면 실제로는 start이 설정되지 않습니다. : P

당신은 기능의 start 외부를 가져다가 글로벌하게, 또는 (A node**로) &start을 통과 main에서 Create_list로하고 목록 헤드를 설정하는 *start을 수정하거나해야합니다.