嵌套单链表c的显示函数
我为我的c类写了几天的数据库程序,现在我正试图弄明白如何使用单链表。我会说,这很难理解,至少对我来说是这样。总的来说,我得到了我的项目所需的部分代码,基本上我得到了向客户机添加客户机和项目的函数,多亏了stackoverflow的帮助,我有点想知道如何向一个客户机添加多个项目,但现在我很难弄清楚如何准确地打印它,尝试以您可以看到im my display功能的方式执行此操作,但它不适用于链接到客户端的项目我应该更改什么以使其工作嵌套单链表c的显示函数,c,linked-list,singly-linked-list,C,Linked List,Singly Linked List,我为我的c类写了几天的数据库程序,现在我正试图弄明白如何使用单链表。我会说,这很难理解,至少对我来说是这样。总的来说,我得到了我的项目所需的部分代码,基本上我得到了向客户机添加客户机和项目的函数,多亏了stackoverflow的帮助,我有点想知道如何向一个客户机添加多个项目,但现在我很难弄清楚如何准确地打印它,尝试以您可以看到im my display功能的方式执行此操作,但它不适用于链接到客户端的项目我应该更改什么以使其工作 #include <stdio.h> #include
#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();
}
#包括
#包括
结构项{
字符项_名称[30];
结构项*NextItem;
};
结构客户端{
结构客户端*NextClient;
字符名[30];
char last_name[30];
结构项*第一项;
结构项*最后一项;
};
结构客户端*head=NULL;
/////////////////////////////
结构客户端*FindTailClient(结构客户端*head)
{
结构客户端*temp=头部;
while(临时->下一个客户端!=NULL)
{
temp=temp->NextClient;
}
返回温度;
}
/////////////////////////////
////////
结构客户端*GetClientData()
{
字符数据[30];
结构客户端*temp;
temp=(struct client*)malloc(sizeof(struct client));
printf(“输入此人的姓名-->”;
扫描频率(“%s”,数据);
strcpy(临时->名称、数据);
printf(“输入此人的姓氏-->”;
扫描频率(“%s”,数据);
strcpy(临时->姓氏,数据);
temp->NextClient=NULL;
返回温度;
}
///////////
结构项*GetItemData()
{
结构项*温度;
字符数据[30];
temp=(结构项*)malloc(sizeof(结构项));
printf(“输入项目名称-->”;
扫描频率(“%s”,数据);
strcpy(临时->项目名称、数据);
temp->NextItem=NULL;
返回温度;
}
///////////
//////////////
结构客户端*AddClient()
{
结构客户端*temp,temp1;
温度=水头;
struct client*data=GetClientData();
if(head==NULL)
{
头=数据;
head->NextClient=NULL;
}
其他的
{
while(临时->下一个客户端!=NULL)
{
temp=temp->NextClient;
}
data->NextClient=NULL;
temp->NextClient=数据;
}
}
///////////////////
///////////////////
///////////////////
无效显示()
{
结构客户端*CurrentClient=head;
结构项目*ItemCurrent=head->firstItem;
while(CurrentClient!=NULL)
{
printf(“->%s->%s\n”,CurrentClient->name,CurrentClient->last\u name);
while(ItemCurrent!=NULL)
{
printf(“->%s\n”,ItemCurrent->item\u name);
ItemCurrent=ItemCurrent->NextItem;
}
CurrentClient=CurrentClient->NextClient;
}
}
///////////////////
无效AddItemToClient(结构客户端*头,结构项*项)
{
item->NextItem=NULL;
if(head->firstItem==NULL){
head->firstItem=物料;
}否则{
head->lastItem->NextItem=项目;
}
head->lastItem=物料;
}
///////////////////
结构客户端*查找(结构客户端*头,字符名[])
{
while(head->NextClient!=NULL)
{
if(strcmp(head->name,name)==0)
{
printf(“找到的目标:%s\n”,头部->名称);
回流头;
}
head=head->NextClient;
}
printf(“未找到目标”);
返回NULL;
}
//////////////////
int main()
{
int i;
字符数据[30];
字符名[30];
结构客户端*temp;
结构客户端*head;
结构项*data1;
对于此行中的(i=0;i:“while(ItemCurrent!=ItemLast)”-ItemLast未声明。可能您想编写head->lastItem,或者更好的ItemCurrent->next!=NULL。
另外,请确保存储刚创建的客户机以便能够访问它们:head=AddClient();而不是简单地AddClient();1.修复AddClient以实际实现其声明声明声明的返回值。2.将指针(即指针的地址)传递到find()
(无论如何都会断开),它只需要一个指针。AddItemToClient(temp,&data1)
也是如此。在编译器上增加警告。在代码块中,它编译时没有任何警告:|因此,基本上如果我是正确的,我必须从这两行中删除:&temp=find(&head,name);AddItemToClient(temp,&data1);当我试图在dev中编译时,我对它们发出了警告,当我删除那些没有警告的时,我正在编译,但查找函数在我完成后根本不起作用,但与那些运行良好的函数相比。指针让我非常困惑:;在显示时:struct item*ItemCurrent=head->firstItem;
有必要分别设置CurrentClient。您能解释一下“有必要以更详细的方式设置CurrentClient每个”是什么意思吗?:xforgot要在发布之前更改它,正如我们试图弄清楚的那样,在我的代码中它应该是ItemCurrent!=NULL