嵌套的单链表

时间:2014-01-11 06:29:23

标签: c linked-list

这个应该向客户添加项目的功能对我不起作用。其余的似乎工作正常。

#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 个答案:

答案 0 :(得分: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;
}