【发布时间】:2014-01-11 21:18:19
【问题描述】:
我正在为我的 c 类编写数据库程序几天,现在我试图弄清楚如何使用单链表,我会说它很难理解,至少对我来说是这样。总的来说,我得到了我的项目所需的部分代码,基本上我得到了向客户端添加客户端和项目的功能,这要归功于 stackoverflow 某人的帮助我到底可以打印它,尝试按照您可以看到我的显示功能的方式进行打印,但它不适用于链接到客户端的项目我应该更改什么才能使其正常工作?
#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.修复 AddClient 以实际履行其声明声明的返回值承诺。 2. 您将一个指向指针的指针(即指针的地址)传递给
find()(无论如何都会被破坏),它只需要一个指针。AddItemToClient(temp,&data1)也是如此。启动编译器上的警告。 -
在代码块中它编译时没有任何警告:|
-
所以基本上如果我是正确的,我必须从这两行中删除 &: temp = find(&head,name); AddItemToClient(temp,&data1);当我尝试在 dev 中编译时,我对那些有警告,当我删除那些没有警告时正在编译,但是在我这样做之后查找功能根本不起作用,但对于那些它工作正常。指针让我很困惑.. :|
-
at display :
struct item *ItemCurrent = head->firstItem;需要分别设置CurrentClient。 -
您能解释一下“有必要以更详细的方式设置 CurrentClient 吗?:x
标签: c linked-list singly-linked-list