嵌套单链表c的显示功能

时间:2014-01-11 21:18:19

标签: c linked-list singly-linked-list

我正在为我的c类编写数据库程序几天,我试图找出如何处理单链表,我很难理解,至少对我而言。总的来说,我得到了我的项目所需的部分代码,基本上我得到了将客户端和项目添加到客户端的功能,感谢stackoverflow的某些人的帮助我想知道如何向1个客户端添加多个项目但是现在我很难搞清楚如何我可以打印它,尝试按照我的显示功能的方式进行操作但它对链接到客户端的项目不起作用我应该更改它以使其工作吗?

#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();
}

1 个答案:

答案 0 :(得分:0)

在这一行中:“while(ItemCurrent!= ItemLast)” - ItemLast未声明。也许你想写head-&gt; lastItem,或者更好的ItemCurrent-&gt; next!= NULL。 此外,请确保存储刚刚创建的客户端以便能够访问它们:head = AddClient();而不是简单的AddClient();