嵌套链接列表在C [英] Nested linked-list in c

查看:163
本文介绍了嵌套链接列表在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功能AddItemToClient似乎没有工作,我不知道如果是因为由于AddItemToClient功能查找功能或只是输出很奇怪。当我尝试添加项目到另一个客户端不是第一个,它不工作,但工作propely只有第一个客户端,但藏汉添加到每个现有的客户端相同的项目可能有人解释,为什么?

 的#include<&stdlib.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdio.h中GT;
结构日期
{
    日整型;
    INT月;
    INT年;
    结构日期*接下来的;
};
项目结构
{
    烧焦ITEM_NAME [30];
    烧焦item_state [30];
    双倍ITEM_PRICE;
    烧焦ITEM_STATUS [30];
    双倍item_price_if_not;
    结构日期* ISSUE_DATE;
    结构项目*接下来的;
};
客户结构
{
    CHAR CLIENT_NAME [30];
    CHAR client_last_name [30];
    结构项目* item_data;
    客户结构*下一个;
};
//添加客户//
无效了addClient(结构客户**头,焦炭名[30],CHAR姓氏[30])
{
    如果((*头)== NULL)
    {
        *头=的malloc(sizeof的(结构客户端));
        的strcpy((*头) - > CLIENT_NAME,名);
        的strcpy((*头) - > client_last_name,姓氏);
        (*头) - >接下来= NULL;
        (*头) - > item_data = NULL;
    }
    其他
{
    客户结构*温度;
    TEMP =(*头);
    // //
    而(TEMP->下面)
    TEMP = TEMP->接下来,
    // //
    (TEMP->下面)=的ma​​lloc(sizeof的(结构客户端));
    的strcpy(TEMP-GT&;&下一步 - GT; CLIENT_NAME,名);
    的strcpy(TEMP-GT&;&下一步 - GT; client_last_name,姓氏);
    TEMP-GT&;下一步 - >接下来= NULL;
    TEMP-GT&;&下一步 - GT; item_data = NULL;
}
}
//添加客户端END //
//////////////////
//找到地址//
客户结构* FindToAdd(客户结构*头上,CHAR名为[30],CHAR姓氏[30])
{
    客户结构*电流=头;
    而(电流!= NULL)
    {
        如果((STRCMP(电流 - > CLIENT_NAME,命名)== 0)及及(STRCMP(电流 - > client_last_name,姓氏)== 0))
        {
            的printf(客户发现:%s%S \\ n,电流 - > CLIENT_NAME,电流 - > client_last_name);
            返回电流;
        }
        电流=电流 - >接着,
    }
    返回(NULL);
}
//寻找添加结束​​//
///////////////////
//添加物品到CLIENT //
无效AddItemToClient(客户结构*头,CHAR ITEM_NAME [30],CHAR item_state [30],双价,焦状态[30],双price_if_not,诠释天,月整型,INT年)
{
    结构项目*它=的malloc(sizeof的(结构件));
    的strcpy(IT-> ITEM_NAME,ITEM_NAME);
    的strcpy(IT-> item_state,item_state);
    的strcpy(IT-> ITEM_STATUS,地位);
    IT-> ITEM_PRICE =价格;
    IT-> item_price_if_not = price_if_not;
    IT-> ISSUE_DATE =的malloc(sizeof的(结构日期));
    IT-> issue_date->天=天;
    IT-> issue_date->当月=月;
    IT-> issue_date->同期=年;
    IT->接着= NULL;
    IT-> issue_date->接着= NULL;
    如果(流浆> item_data == NULL)
    {
        流浆> item_data =它;
    }
    其他
    {
        结构项目*节点=流浆> item_data;
        而(与于节点GT;!下次= NULL)节点=于节点>接下来,
        于节点>接下来=它;
    }
}
//添加物品到客户端的最终//
//////////////////////////
// //显示
无效显示(客户结构*头)
    {INT I = 1,B = 1;
    客户结构*电流;
    电流=头;
        而(电流!= NULL)
        {
           的printf([%D]。客户名称:%s%s的\\ n,我,电流 - > CLIENT_NAME,电流 - > client_last_name);
           结构项目* CURRENTITEM =流浆> item_data;
           而(CURRENTITEM!= NULL)
           {
             的printf(----------------------------------------------- \\ n);
             的printf([%D]。\\ n,B);
             输出(项目名称:%s \\ n,CurrentItem-> ITEM_NAME);
             的printf(项状态:%S \\ n,CurrentItem-> item_state);
             输出(项目状态:%S \\ n,CurrentItem-> ITEM_STATUS);
             的printf(一口价:%LF \\ n,CurrentItem-> ITEM_PRICE);
             的printf(一口价,如果债务wasnt支付:%LF \\ n,CurrentItem-> item_price_if_not);
             的printf(发行日期: %d/%d/%d\
\",CurrentItem->issue_date->day,CurrentItem->issue_date->month,CurrentItem->issue_date->year);
             的printf(----------------------------------------------- \\ n);
             CURRENTITEM = CurrentItem->接下来,
             基础B ++;
           }
           电流=电流 - >接着,
           我++;
        }
    }
//显示结束//
///////////////
诠释的main()
{
    客户结构*名单= NULL;
         INT选择;
         CHAR名称[30];
         CHAR CLIENT_NAME [30];
         CHAR client_named [30];
         CHAR client_last_named [30];
          CHAR client_last_name [30];
          烧焦ITEM_NAME [30];
          烧焦item_state [30];
          CHAR to_find_name [30];
          CHAR to_find_last_name [30];
          双价;
          焦状态[30];
          双price_if_not;
          INT日,月,年;
         scanf函数(%d个,&安培;选择);
    而(选择!= 6)
    {
        的printf(选项1:新的人:\\ nOption 2:显示\\ nOption 3:添加物品到人\\ n);
    开关(选择)
    {
    情况1:
     的printf(新的人:\\ n);
                的printf(提供客户端名称\\ n);
                scanf函数(%S,CLIENT_NAME);
                的printf(提供客户的姓名\\ n);
                scanf函数(%S,client_last_name);
                了addClient(安培;名单,CLIENT_NAME,client_last_name);
                打破;
    案例2:
     的printf(显示\\ n);
                显示(列表);
                打破;
    案例3:
    的printf(添加项目负责人:\\ n);
    结构客户*颞= NULL;
    的printf(期望的人的\\ n名称);
      scanf函数(%S,client_named);
    的printf(期望的人的\\ n姓);
      scanf函数(%S,client_last_named);
    时间= FindToAdd(列表,client_named,client_last_named);
    输出(项目名称\\ n);
      scanf函数(%S,ITEM_NAME);
    的printf(项状态\\ n);
    scanf函数(%S,item_state);
    的printf(一口价\\ n);
      scanf函数(%LF,&安培;价格);
    输出(项目状态\\ n);
    scanf函数(%S,地位);
    的printf(一口价,如果债务wasnt支付\\ n);
      scanf函数(%LF,&安培; price_if_not);
    的printf(发行日期:天\\ n);
    scanf函数(%D,和天);
    的printf(发行日期:每月\\ n);
    scanf函数(%d个,&安培;当月);
    的printf(发行日期:年\\ n);
    scanf函数(%d个,&安培;一年);
    AddItemToClient(时间,ITEM_NAME,item_state,价格,地位,price_if_not,日,月,年);
    打破;
    情况4:
     的printf(期望的人的\\ n名称);
      scanf函数(%S,to_​​find_name);
    的printf(期望的人的\\ n姓);
      scanf函数(%S,to_​​find_last_name);
    FindToAdd(列表,to_find_name,to_find_last_name);
    打破;
    默认:
            的printf(无效的选择\\ n \\ n);
            打破;
        }
        的printf(?);
      scanf函数(%d个,&安培;选择);
    }
}


解决方案

问题是在这里:

 结构项目* CURRENTITEM =流浆> item_data;

 结构项目* CURRENTITEM =电流 - > item_data;

My function AddItemToClient doesnt seem to work I'm not sure if its because of find function or because of AddItemToClient function but the output is strange. When I try to add item to another client than first one, it doesnt work but works propely only to first client but aswell adds to every existing client same item could somebody explain why?

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

解决方案

problem is here:

           struct item *CurrentItem = head->item_data;

should be

       struct item *CurrentItem = current->item_data;

这篇关于嵌套链接列表在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆