【发布时间】:2015-11-21 18:10:32
【问题描述】:
我正在尝试在队列末尾插入节点并面临以下错误。这是编译代码时的简单基本错误,但让我的生活变得艰难。
#include<stdio.h>
#include<stdlib.h>
typedef struct UNIX {
char str[20];
struct UNIX *next;
}examp;
examp *head=NULL;
int insert_last(char *s)
{
examp *new,*slide;
slide=head;
new = (examp *)malloc(sizeof(examp));
if(!new)
return(EXIT_FAILURE);
while(slide->next!=NULL)
slide=slide->next;
slide->next=new;
new->str=s;
new->next=NULL;
if(head==NULL)
{ head=new;
return 1;
}
return 1;
}
void display (void);
int main()
{
insert_last("hello ");
insert_last("how ");
insert_last("have ");
insert_last("you ");
insert_last("been ");
insert_last("! ");
display();
}
void display(void)
{
examp *slide;
slide=head;
while(slide->next!=NULL)
{ printf("%s ",slide->str);
slide=slide->next;
}
}
错误:stack_queue.c:27:10:错误:赋值给数组类型的表达式 新->str=s;
更新:使用 strncpy 解决了错误,但代码未按预期工作并意外停止。
【问题讨论】:
标签: c linked-list stack queue