Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在链表中插入字符串时出现问题_C_Data Structures_Linked List - Fatal编程技术网

C 在链表中插入字符串时出现问题

C 在链表中插入字符串时出现问题,c,data-structures,linked-list,C,Data Structures,Linked List,我正在写一个程序,将数据插入链表并打印出来 LinkedList.c #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> struct node { char *data; struct node *next; }; void insert(struct node** head_ref,char *new_data) { struct

我正在写一个程序,将数据插入链表并打印出来

LinkedList.c

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct node 
{
  char *data;
  struct node *next;
};

void insert(struct node** head_ref,char *new_data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node *last = *head_ref;
strcpy(new_node->data,new_data);
new_node->next = NULL;
if (*head_ref == NULL)
{
   *head_ref = new_node;//assigning head node
   return;
}

while (last->next != NULL)
    last = last->next;//this helps to traverse to last node

last->next = new_node;
return;
}


void printList(struct node *node)//function to print the linked list
{
  while (node != NULL)
  {
   printf(" %s ", node->data);
   node = node->next;
  }
}


int main() {
   int t;
   char datas[1000];
   scanf("%d",&t);
   struct node* head=NULL;
   int i;
   for(i=0;i<t;i++)
   {
      scanf("%s",datas);//this data should be added into the linkedlist
      insert(&head,datas);
   }
   printList(head);

  return 0;
 }
#包括
#包括
#包括
#包括
结构节点
{
字符*数据;
结构节点*下一步;
};
void insert(结构节点**head\u ref,char*新数据)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
结构节点*last=*head\u ref;
strcpy(新建节点->数据,新建数据);
新建节点->下一步=空;
如果(*head_ref==NULL)
{
*head\u ref=new\u node;//分配head节点
回来
}
while(上一次->下一次!=NULL)
last=last->next;//这有助于遍历到最后一个节点
last->next=新建_节点;
回来
}
void printList(struct node*node)//用于打印链表的函数
{
while(节点!=NULL)
{
printf(“%s”,节点->数据);
节点=节点->下一步;
}
}
int main(){
int t;
字符数据[1000];
scanf(“%d”、&t);
结构节点*head=NULL;
int i;

对于(i=0;i,您的代码给出了未定义的行为

请查看的文档

长话短说,
strcpy()
要求
目标
(您的
新节点->数据
)是已分配的
字符
数组,但您尚未为其分配数据,并且正在写入未定义(和未分配)的内存

要克服这个问题,可以动态地为新字符串分配空间(并且在释放节点时不要忘记释放它),或者将
data
a
char[]
替换为
char*


此外,请记住。因为它看起来像是教育目的代码,所以请仔细想想——我想,不要试图解决它。

您的代码给出了未定义的行为

请查看的文档

长话短说,
strcpy()
要求
目标
(您的
新节点->数据
)是已分配的
字符
数组,但您尚未为其分配数据,并且正在写入未定义(和未分配)的内存

要克服这个问题,可以动态地为新字符串分配空间(并且在释放节点时不要忘记释放它),或者将
data
a
char[]
替换为
char*


此外,请记住。因为它看起来像是教育目的代码,所以请仔细想想——我想,不要试图解决它。

您错过了内存分配,请尝试以下方法:

void insert(struct node** head_ref,char *new_data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node *last = *head_ref;

// you should allocate memory because you use data as char *
new_node->data = malloc(strlen(new_data)+1); 

/* you can use new_node->data = strdup(new_data);  
instead of new_node->data = malloc(strlen(new_data)); and strcpy(new_node->data,new_data); 
because strdup allocate and copy string with exact size */
strcpy(new_node->data,new_data); 

new_node->next = NULL;
if (*head_ref == NULL)
{
   *head_ref = new_node;//assigning head node
   return;
}

while (last->next != NULL)
    last = last->next;//this helps to traverse to last node

last->next = new_node;
return;
}

您错过了内存分配,请尝试以下操作:

void insert(struct node** head_ref,char *new_data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node *last = *head_ref;

// you should allocate memory because you use data as char *
new_node->data = malloc(strlen(new_data)+1); 

/* you can use new_node->data = strdup(new_data);  
instead of new_node->data = malloc(strlen(new_data)); and strcpy(new_node->data,new_data); 
because strdup allocate and copy string with exact size */
strcpy(new_node->data,new_data); 

new_node->next = NULL;
if (*head_ref == NULL)
{
   *head_ref = new_node;//assigning head node
   return;
}

while (last->next != NULL)
    last = last->next;//this helps to traverse to last node

last->next = new_node;
return;
}

这也会导致未定义的行为。新数据需要多少空间?它是strlen(新数据)字节吗?是的,它是,新数据是从主函数中的scanf读取的,所以长度不同于0让我重新表述一下,新数据需要strlen(新数据)字节吗?空终止符存储在哪里?字符串
有多少空间
take?它的长度是多少?不,但在这种情况下,字符串是用scanf读取的,因此我们有空终止符storedAssume我将字符串
a
插入到scanf中。为了存储这个字符串,我需要在
new\u node->data
中分配多少字节?这也会导致未定义的行为。new\u data t需要多少空间akes?是strlen(新数据)字节吗?是的,它是,新数据是从主函数中的scanf读取的,因此长度不同于0让我重新表述一下,新数据是否需要strlen(新数据)字节?空终止符存储在哪里?字符串
有多少空间
take?它的长度是多少?不,但在这种情况下,字符串是用scanf读取的,因此我们有空终止符storedAssume,我将字符串
a
插入scanf。为了存储此字符串,我需要在
new_node->data
中分配多少字节?