List 带指针和函数的C列表
我创建了以下程序,这是一个链表(议程),按字母顺序排列联系人,并允许用户搜索特定联系人 然后我试着用函数制作同样的程序:(贴在下面),但到目前为止都没有成功。我是新手,不知道自己做错了什么。非常感谢您的帮助List 带指针和函数的C列表,list,function,pointers,linked-list,nodes,List,Function,Pointers,Linked List,Nodes,我创建了以下程序,这是一个链表(议程),按字母顺序排列联系人,并允许用户搜索特定联系人 然后我试着用函数制作同样的程序:(贴在下面),但到目前为止都没有成功。我是新手,不知道自己做错了什么。非常感谢您的帮助 #define MAX 20 typedef struct temp { int data; char name[MAX]; char telephone[10]; char email[MAX]; char address[MAX]; char zipcode[10]; temp
#define MAX 20
typedef struct temp
{
int data;
char name[MAX];
char telephone[10];
char email[MAX];
char address[MAX];
char zipcode[10];
temp *next;
} node;
node* creation1 ( )
{
node *start= NULL;
node *NEW = NULL;
node *current = NULL, *aux = NULL, *save = NULL;
NEW = (node*)malloc(sizeof(node));
current = start= aux = save = NEW;
return NEW;
}
node* creation2 ()
{
node *start= NULL;
node *NEW = creation1();
start= NEW;
return start;
}
node* creation3 ( )
{
node *NEW = creation1();
node *current = NULL;
current=NEW;
return current;
}
node* consult ()
{
node *NEW= creation1();
node *start= creation2();
node *current = creation3();
int exit;
printf("How many contacts do you want to add in the agenda? ");
scanf("%i",&exit);
for(int i=1; i<=exit; i++)
{
NEW = (node*)malloc(sizeof(node));
current->next=NEW;
current = NEW;
fflush(stdin);
puts("NAME: ");
gets(NEW->name);
puts("TELEPHONE: ");
gets(NEW->telephone);
puts("EMAIL: ");
gets(NEW->email);
puts("ADDRESS: ");
gets(NEW->address);
puts("ZIP CODE: ");
gets(NEW->zipcode);
NEW->next=NULL;
}
current = start->next;
return current;
}
node* order ()
{
node *NEW=creation1();
node *start=creation2 ();
node *current =NULL;
current=consult();
node *aux = NULL;
node *save = NULL;
aux=NEW;
save=NEW;
int i = 0;
do
{
i++;
current = current->next; /* THIS IS WHERE I'M GETTING AN ERROR MS Visual Studio tells me: "Unhandled exception...Access violation reading location..." */
}
while (current != NULL);
current = start->next;
aux = current->next;
for (int j = 1; j < i; j++)
{
current = start->next;
aux = current->next;
while(current->next != NULL)
{
if (strcmp(current->name,aux->name) > 0)
{
strcpy(save->name, current->name);
strcpy(save->telephone, current->telephone);
strcpy(save->email, current->email);
strcpy(save->address, current->address);
strcpy(save->zipcode, current->zipcode);
strcpy(current->name, aux->name);
strcpy(current->telephone, aux->telephone);
strcpy(current->email, aux->email);
strcpy(current->address, aux->address);
strcpy(current->zipcode, aux->zipcode);
strcpy(aux->name, save->name);
strcpy(aux->telephone, save->telephone);
strcpy(aux->email, save->email);
strcpy(aux->address, save->address);
strcpy(aux->zipcode, save->zipcode);
}
current = current->next;
aux = current->next;
}
}
return current;
}
node* displayorder()
{
node *NEW=creation1();
node *start=creation2 ();
node *current = order();
current = start->next;
while(current != NULL)
{
printf("\n********************");
printf("\n NAME: %s",current->name);
printf("\n TELEPHONE: %s", current->telephone);
printf("\n E-MAIL: %s", current->email);
printf("\n ADDRESS: %s ", current->address);
printf("\n ZIP CODE: %s ", current->zipcode);
current = current->next;
}
getch();
return current;
}
node* displaysearch()
{
node *current = displayorder();
node *start= creation2();
char search[MAX];
printf("\n\nGive a name to search: ");
scanf("%s",search);
current = start->next;
while(current != NULL)
{
if(strcmp(search, current->name)==0)
{
printf("\n********************");
printf("\n NAME: %s",current->name);
printf("\n TELEPHONE: %s", current->telephone);
printf("\n E-MAIL: %s", current->email);
printf("\n ADDRESS: %s ", current->address);
printf("\n ZIP CODE: %s ", current->zipcode);
}
current = current->next;
}
return current;
getch();
}
int main(int argc, char** argv)
{
displaysearch();
}
#定义最大值20
类型定义结构温度
{
int数据;
字符名[MAX];
字符电话[10];
字符电子邮件[MAX];
字符地址[MAX];
char-zipcode[10];
临时*下一步;
}节点;
节点*creation1()
{
node*start=NULL;
node*NEW=NULL;
节点*current=NULL,*aux=NULL,*save=NULL;
NEW=(node*)malloc(sizeof(node));
当前=启动=辅助=保存=新建;
归还新的;
}
节点*creation2()
{
node*start=NULL;
节点*NEW=creation1();
开始=新的;
返回启动;
}
节点*creation3()
{
节点*NEW=creation1();
节点*当前=空;
当前=新的;
回流;
}
节点*consult()
{
节点*NEW=creation1();
节点*start=creation2();
节点*current=creation3();
int出口;
printf(“您希望在议程中添加多少联系人?”);
scanf(“%i”,退出(&E);
对于(int i=1;INXT=NEW;
当前=新的;
fflush(stdin);
出售(名称:);
获取(新建->名称);
放置(电话:);
获取(新建->电话);
放入(“电子邮件:”);
获取(新建->电子邮件);
卖出(地址:);
获取(新建->地址);
放入(“邮政编码:”);
获取(新建->zipcode);
新建->下一步=空;
}
当前=开始->下一步;
回流;
}
节点*顺序()
{
节点*NEW=creation1();
node*start=creation2();
节点*当前=空;
当前=参考();
node*aux=NULL;
节点*save=NULL;
aux=新的;
保存=新建;
int i=0;
做
{
i++;
current=current->next;/*这是我收到错误的地方,微软Visual Studio告诉我:“未处理的异常…访问冲突读取位置…”*/
}
while(当前!=NULL);
当前=开始->下一步;
aux=当前->下一步;
对于(int j=1;j下一步;
aux=当前->下一步;
while(当前->下一步!=NULL)
{
如果(strcmp(当前->名称,辅助->名称)>0)
{
strcpy(保存->名称,当前->名称);
strcpy(保存->电话,当前->电话);
strcpy(保存->电子邮件,当前->电子邮件);
strcpy(保存->地址,当前->地址);
strcpy(保存->zipcode,当前->zipcode);
strcpy(当前->名称,辅助->名称);
strcpy(当前->电话,辅助->电话);
strcpy(当前->电子邮件,辅助->电子邮件);
strcpy(当前->地址,辅助->地址);
strcpy(当前->zipcode,辅助->zipcode);
strcpy(辅助->名称,保存->名称);
strcpy(辅助->电话,保存->电话);
strcpy(辅助->电子邮件,保存->电子邮件);
strcpy(辅助->地址,保存->地址);
strcpy(aux->zipcode,save->zipcode);
}
当前=当前->下一步;
aux=当前->下一步;
}
}
回流;
}
节点*displayorder()
{
节点*NEW=creation1();
node*start=creation2();
节点*当前=顺序();
当前=开始->下一步;
while(当前!=NULL)
{
printf(“\n*******************”);
printf(“\n名称:%s”,当前->名称);
printf(“\n电话:%s”,当前->电话);
printf(“\n电子邮件:%s”,当前->电子邮件);
printf(“\n地址:%s”,当前->地址);
printf(“\n邮政编码:%s”,当前->邮政编码);
当前=当前->下一步;
}
getch();
回流;
}
节点*displaysearch()
{
节点*当前=显示顺序();
节点*start=creation2();
字符搜索[MAX];
printf(“\n\n请输入要搜索的名称:”);
scanf(“%s”,搜索);
当前=开始->下一步;
while(当前!=NULL)
{
if(strcmp(搜索,当前->名称)==0)
{
printf(“\n*******************”);
printf(“\n名称:%s”,当前->名称);
printf(“\n电话:%s”,当前->电话);
printf(“\n电子邮件:%s”,当前->电子邮件);
printf(“\n地址:%s”,当前->地址);
printf(“\n邮政编码:%s”,当前->邮政编码);
}
当前=当前->下一步;
}
回流;
getch();
}
int main(int argc,字符**argv)
{
displaysearch();
}
函数order()中的局部变量current
从函数consult()获取其值
。该函数依次返回start->next
的值,该值从未初始化为指向任何节点
结构。因此,内存访问冲突异常是预期的异常。请直接在帖子中添加代码,简化为显示问题的最小示例。在开头添加四个空格创建代码块所需的行数。代码太多了。问题出在哪里?您对什么感到困惑?我不确定是否正确声明了函数并返回了指针。当程序到达order()函数的第一个循环时,我会收到一个错误。MS Visual Studio中的错误告诉我:“未处理的异常…访问冲突读取位置…”,它指向当前=当前->下一个in:int i=0;do{i++;当前=当前->下一个;}while(当前!=NULL);Serge的答案是一个好的开始,但我强烈建议(正如其他人一样)你可以最小化你发布的代码,你会找到你需要的帮助。考虑用代码的部分来替换这个部分所做的注释。