输入一行字符,按输入字符的反序建立一个字符结点的单向链表,并输出该链表中的字符。

#include<iostream>
using namespace std;
typedef struct node
{
char data[100];
struct node *next;
}node;
typedef node *link;
void main()
{
int i;
link ptr,head,tail;
tail=(link)malloc(sizeof(node));
tail->next=NULL;
ptr=tail;
char str[100];
while(str!=NULL){
scanf("%s",&str);
ptr->data=str; 这行出问题 请问如何改?
head=(link)malloc(sizeof(node));
head->next=ptr;
ptr=head;
}
while(ptr!=NULL)
{
printf("The value is ==>%s",ptr->data);
ptr=ptr->next;
}}

第1个回答  推荐于2018-03-11
字符串是字符数组,在C中不能直接赋值。使用strcpy函数: strcpy(ptr->data,str);注意,头文件引入:#include<string.h>本回答被网友采纳
相似回答