【问题标题】:Nested linked-list in cc中的嵌套链表
【发布时间】:2014-01-18 17:25:09
【问题描述】:

我的函数 AddItemToClient 似乎不起作用我不确定是因为 find 函数还是因为 AddItemToClient 函数,但输出很奇怪。当我尝试将项目添加到另一个客户而不是第一个客户时,它不起作用,但仅适用于第一个客户,但也向每个现有客户添加相同的项目,有人可以解释为什么吗?

#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
struct date
{
    int day;
    int month;
    int year;
    struct date* next;
};
struct item
{
    char item_name[30];
    char item_state[30];
    double item_price;
    char item_status[30];
    double item_price_if_not;
    struct date *issue_date;
    struct item *next;
};
struct client
{
    char client_name[30];
    char client_last_name[30];
    struct item *item_data;
    struct client *next;
};
//ADD CLIENT//
void AddClient(struct client **head, char name[30], char last_name[30])
{
    if((*head) == NULL)
    {
        *head = malloc(sizeof(struct client));
        strcpy((*head)->client_name,name);
        strcpy((*head)->client_last_name,last_name);
        (*head)->next = NULL;
        (*head)->item_data = NULL;
    }
    else
{
    struct client *temp;
    temp = (*head);
    //             //
    while(temp->next)
    temp=temp->next;
    //              //
    (temp->next) = malloc(sizeof(struct client));
    strcpy(temp->next->client_name,name);
    strcpy(temp->next->client_last_name,last_name);
    temp->next->next = NULL;
    temp->next->item_data = NULL;
}
}
//ADD CLIENT END//
//////////////////
//FIND TO ADD//
struct client *FindToAdd(struct client *head, char named[30],char last_name[30])
{
    struct client *current = head;
    while(current != NULL)
    {
        if((strcmp(current->client_name,named) == 0 )&& (strcmp(current->client_last_name,last_name) == 0))
        {
            printf("Client found :%s %s\n",current->client_name,current->client_last_name);
            return current;
        }
        current = current->next;
    }
    return (NULL);
}
//FIND TO ADD END//
///////////////////
//ADD ITEM TO CLIENT//
void AddItemToClient(struct client *head, char item_name[30], char item_state[30], double price, char status[30], double price_if_not, int day, int month, int year)
{
    struct item* it = malloc(sizeof(struct item));
    strcpy(it->item_name, item_name);
    strcpy(it->item_state, item_state);
    strcpy(it->item_status, status);
    it->item_price = price;
    it->item_price_if_not = price_if_not;
    it->issue_date = malloc(sizeof(struct date));
    it->issue_date->day = day;
    it->issue_date->month = month;
    it->issue_date->year = year;
    it->next = NULL;
    it->issue_date->next = NULL;
    if (head->item_data == NULL)
    {
        head->item_data = it;
    }
    else
    {
        struct item* node = head->item_data;
        while (node->next != NULL) node = node->next;
        node->next = it;
    }
}
//ADD ITEM TO CLIENT END//
//////////////////////////
// DISPLAY//
void Display(struct client *head)
    {int i=1,b=1;
    struct client *current;
    current = head;
        while(current != NULL)
        {
           printf("[%d] Client name:%s %s \n",i, current->client_name, current->client_last_name);
           struct item *CurrentItem = head->item_data;
           while(CurrentItem != NULL)
           {
             printf("-----------------------------------------------\n");
             printf("[%d] \n",b);
             printf("Item name: %s\n",CurrentItem->item_name);
             printf("Item state: %s\n",CurrentItem->item_state);
             printf("Item status: %s\n",CurrentItem->item_status);
             printf("Item price: %lf\n",CurrentItem->item_price);
             printf("Item price if debt wasnt paid: %lf\n",CurrentItem->item_price_if_not);
             printf("Issue date: %d/%d/%d\n",CurrentItem->issue_date->day,CurrentItem->issue_date->month,CurrentItem->issue_date->year);
             printf("-----------------------------------------------\n");
             CurrentItem = CurrentItem->next;
             b++;
           }
           current=current->next;
           i++;
        }
    }
//DISPLAY END//
///////////////
int main()
{
    struct client* List = NULL;
         int choice;
         char name[30];
         char client_name[30];
         char client_named[30];
         char client_last_named[30];
          char client_last_name[30];
          char item_name[30];
          char item_state[30];
          char to_find_name[30];
          char to_find_last_name[30];
          double price;
          char status[30];
          double price_if_not;
          int day,month,year;
         scanf( "%d", &choice );
    while( choice != 6)
    {
        printf("Option 1: New person:\nOption 2: Display\nOption 3: Add item to person\n");
    switch( choice )
    {
    case 1:
     printf( "New person: \n" );
                printf("Provide client name\n");
                scanf("%s",client_name);
                printf("Provide client last name\n");
                scanf("%s",client_last_name);
                AddClient(&List,client_name,client_last_name);
                break;
    case 2:
     printf("Display\n");
                Display(List);
                break;
    case 3:
    printf("Add item to person: \n");
    struct client *temporal = NULL;
    printf("Name of desired person\n");
      scanf("%s",client_named);
    printf("Last name of desired person\n");
      scanf("%s",client_last_named);
    temporal = FindToAdd(List,client_named,client_last_named);
    printf("Item name\n");
      scanf("%s",item_name);
    printf("Item state\n");
    scanf("%s",item_state);
    printf("Item price\n");
      scanf("%lf",&price);
    printf("Item status\n");
    scanf("%s",status);
    printf("Item price if debt wasnt paid\n");
      scanf("%lf",&price_if_not);
    printf("Issue date: day\n");
    scanf("%d",&day);
    printf("Issue date: month\n");
    scanf("%d",&month);
    printf("Issue date: year\n");
    scanf("%d",&year);
    AddItemToClient(temporal,item_name,item_state,price,status,price_if_not,day,month,year);
    break;
    case 4:
     printf("Name of desired person\n");
      scanf("%s",to_find_name);
    printf("Last name of desired person\n");
      scanf("%s",to_find_last_name);
    FindToAdd(List,to_find_name,to_find_last_name);
    break;
    default:
            printf( "Invalid choice.\n\n" );
            break;
        }
        printf( "? " );
      scanf( "%d", &choice );
    }
}

【问题讨论】:

  • 这看起来与您之前的问题stackoverflow.com/questions/21205697/… 相同。如果您没有得到答案,请尝试改进问题并提供更多信息,而不是简单地重复。
  • 在我之前的问题中,AddItemToClient 函数根本不起作用是覆盖以前的节点,现在至少对于第一个节点的链接是正确完成的,所以我认为现在的问题很可能是不同的,因为 find功能。所以我认为它找到了项目的尾部,所以我认为这仍然不是同样的问题

标签: c linked-list nested


【解决方案1】:

问题就在这里:

           struct item *CurrentItem = head->item_data;

应该是

       struct item *CurrentItem = current->item_data;

【讨论】:

  • 谢谢,没想到问题可能是显示功能一直在想我只是在查找或添加功能中做错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2020-12-28
  • 1970-01-01
相关资源
最近更新 更多