2014-01-11 1 views
-1

Im 내 며칠 동안 데이터베이스를 작성하는 Im 이제 단일 링크 목록을 사용하여 작업하는 방법을 알아 내려고 노력하고 있습니다. 이해하기가 어렵습니다. 전반적으로 내가 내 프로젝트에 필요한 코드의 일부를 얻었습니다, 기초 나는 stackoverflow에서 누군가의 도움 덕분에 클라이언트 및 클라이언트에 항목을 추가하는 기능을 가지고 내가 좀 클라이언트를 여러 항목을 추가하는 방법을 알아 냈지만 지금은 문제를 알아내는 방법에있어 정확히 내가 그것을 인쇄 할 수있는 방법을 당신이 내 디스플레이 기능을 볼 수있는 방법을 시도했지만 클라이언트에 연결된 항목에 대한 작품을 나던 내가 어떻게 작동하도록 변경해야합니까?중첩 된 단일 연결 목록에 대한 표시 기능 c

#include <stdio.h> 
#include <stdlib.h> 
struct item{ 
char item_name[30]; 
struct item *NextItem; 
}; 
struct client{ 
    struct client *NextClient; 
    char name[30]; 
    char last_name[30]; 
    struct item *firstItem; 
    struct item *lastItem; 
}; 
struct client *head = NULL; 
///////////////////////////// 
struct client* FindTailClient(struct client* head) 
{ 
    struct client *temp = head; 
    while(temp->NextClient != NULL) 
    { 

     temp = temp->NextClient; 
    } 
    return temp; 
} 
///////////////////////////// 
//////// 
struct client *GetClientData() 
    { 
     char data[30]; 
    struct client *temp; 
    temp = (struct client*)malloc(sizeof(struct client)); 

    printf("Enter the person's name--->"); 
    scanf("%s",data); 
    strcpy(temp->name,data); 
    printf("Enter the person's last name--->"); 
    scanf("%s",data); 
    strcpy(temp->last_name,data); 
    temp->NextClient = NULL; 
    return temp; 
    } 
/////////// 
struct item *GetItemData() 
    { 
    struct item *temp; 
    char data[30]; 
    temp = (struct item*)malloc(sizeof(struct item)); 

    printf("Enter the item name--->"); 
    scanf("%s",data); 
    strcpy(temp->item_name,data); 
    temp->NextItem = NULL; 
    return temp; 
    } 
/////////// 
////////////// 
struct client* AddClient() 
{ 
    struct client *temp,temp1; 
    temp=head; 
    struct client *data = GetClientData(); 



    if(head == NULL) 
    { 
     head=data; 
     head->NextClient = NULL; 
    } 
    else 
    { 
    while(temp->NextClient != NULL) 
    { 
    temp=temp->NextClient; 
    } 
    data->NextClient=NULL; 
    temp->NextClient=data; 
    } 
} 
/////////////////// 

/////////////////// 

/////////////////// 
void display() 
{ 
    struct client *CurrentClient = head; 
struct item *ItemCurrent = head->firstItem; 
     while(CurrentClient != NULL) 
     { 
     printf(" -> %s ->%s \n",CurrentClient->name,CurrentClient->last_name); 
       while(ItemCurrent != NULL) 
       { 
       printf(" -> %s\n",ItemCurrent->item_name); 
       ItemCurrent=ItemCurrent->NextItem; 
       } 
     CurrentClient=CurrentClient->NextClient; 
     } 

} 
/////////////////// 
void AddItemToClient(struct client* head, struct item *item) 
{ 
    item->NextItem = NULL; 
    if(head->firstItem == NULL) { 
     head->firstItem = item; 
    } else { 
     head->lastItem->NextItem = item; 
    } 
    head->lastItem = item; 

} 
/////////////////// 
struct client *find(struct client *head, char name[]) 
{ 
    while (head->NextClient != NULL) 
    { 
     if (strcmp(head->name,name) == 0) 
     { 
      printf("Target found: %s\n",head->name); 
      return head; 
     } 
     head = head->NextClient; 
    } 
    printf("target not found"); 
    return NULL; 
} 
////////////////// 
int main() 
{ 
    int i; 
    char data[30]; 
    char name[30]; 
    struct client *temp; 
    struct client *head; 
    struct item *data1; 
    for(i=0;i<2;i++) 
    { 
    AddClient(); 
    } 
    printf("Insert name to find:"); 
    scanf("%s",name); 
temp = find(&head,name); 
data1 = GetItemData(); 
AddItemToClient(temp,&data1); 
display(); 
} 
+0

1. 실제로 반환 값을 충족시키기 위해 AddClient를 수정하여 선언의 클레임을 약속합니다. 포인터를 기대하는'find()'에 포인터 포인터 (즉, 포인터의 주소)를 전달하고 있습니다 (그리고 관계없이 끊어짐). 'AddItemToClient (temp, & data1)'도 마찬가지입니다. 컴파일러에서 경고를 크랭크 업하십시오. – WhozCraig

+0

잘 경고없이 컴파일되는 코드 블록에 | – user3175531

+0

그래서 근본적으로 올바른 것이면 그 두 줄에서 &를 삭제해야합니다 : temp = find (& head, name); AddItemToClient (temp, & data1); 내가 dev에서 컴파일을 시도 할 때 그 사람들에게 경고가 있었고, 내가 삭제 한 후에 컴파일 할 때 아무런 경고도 내지 못했지만 기능을 수행 한 후에는 전혀 작동하지 않는 기능을 찾지 못했습니다. 포인터가 나를 그렇게 나쁘게 혼동시킨다 .. : | – user3175531

답변

0

이 줄에 : "while (ItemCurrent! = ItemLast)"- ItemLast가 선언되지 않았습니다. 어쩌면 head-> lastItem 또는 더 나은 ItemCurrent-> next! = NULL을 작성하려고했습니다. 또한 방금 작성한 클라이언트를 저장할 수 있도록 저장하십시오. head = AddClient(); 단순히 AddClient() 대신;

+0

게시하기 전에 그것을 변경하는 것을 잊어 버렸습니다. 알아 내려고했는데, ItemCurrent! = NULL로되어있었습니다. 내 코드 – user3175531