2014-01-11 139 views
0

這個應該添加項目到客戶端的功能不適用於我。其餘的似乎工作正常。嵌套單連接列表

#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 *temp=head; 
    if(temp==NULL) 
    { 
      printf("\nList is Empty"); 
    } 
    else 
    { 
      printf("\nElements in the List: "); 
      while(temp!=NULL) 
      { 
       printf(" -> %s ->%s ",temp->name,temp->last_name); 
       temp=temp->NextClient; 
      } 
     printf("\n"); 
     } 
} 
/////////////////// 
void AddItemToClient(struct client* head, struct item *item) 
{ 
    item->NextItem = NULL; 
    if(head->firstItem == NULL) { 
     head->firstItem = item; 
    } else { 
     head->firstItem->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(); 
    } 
    display(); 
    printf("Insert name to find:"); 
    scanf("%s",name); 
temp = find(&head,name); 
data1 = GetItemData(); 
AddItemToClient(temp,&data1); 
display(); 
} 

回答

1

這是錯誤的

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

} 

應該

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; 
} 
+0

由於其現在的工作:X。 – user3175531