将单词读入链表
Reading in words into a linked list
我正在尝试编写一个程序来读取用户输入的每个单词,然后将该单词粘贴到链表中。到目前为止,这是我尝试过的方法,但遇到了段错误,但不太确定 mallocing/pointers 哪里出错了。 (还没有实现 printList)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 20
typedef struct node{
char *word;
struct node *next;
}node_t;
node_t *read(node_t *node);
void printList(node_t *node);
node_t *insertNode(char *word, node_t *node, int size);
int main(int argc, char *argv[]) {
node_t *start = NULL;
printf("Enter a sentence:\n");
read(start);
return 0;
}
void *read(node_t *node){
int i, size = MAX_LEN;
char c, *word;
if(!(word=malloc(size))){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
while((c=getchar())!='\n'){
for(i=0;c!=' ';i++){
word[i]=c;
if(i>size){
size=size*2;
if(!realloc(word, size)){
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
}
}
node = insertNode(word,node,size);
}
return node;
}
node_t *insertNode(char *word, node_t *node, int size){
node_t *new_node, *current;
new_node = (node_t*)malloc(sizeof(node_t));
new_node->next = NULL;
if(!(new_node->word = malloc(size))){
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(new_node->word,word);
if (node == NULL){
node = new_node;
current = new_node;
}
else{
current->next = new_node;
current = new_node;
}
return node;
}
我认为错误是由行引起的
return node;
在 insertNode
中。应该是
return new_node;
有几个问题:
- 您的原型与
read
的实现不匹配;使 return 成为 node_t *
.
- 您有两个用于输入的嵌套循环,一个从
stdin
读取,另一个循环遍历字符。内循环从不更新其条件,因为 c
只能由外循环更改。应该只有一个循环,负责从流中读取数据并写入字符串。
- 您没有保留
realloc
的结果,这意味着当分配的内存句柄发生变化时,您不会反映更新。在这些情况下,您将访问已失效的旧句柄。
- 您没有以空字符终止您的字符串。
- 您应该在越界访问内存之前重新分配内存。这通常意味着在写入数组之前检查是否扩大数组。请注意,对于长度为
n
的数组,n
本身已经是一个非法索引。
getchar
的结果应该是 int
,而不是 char
,以便所有有效输入都不同于 EOF
,您不检查.
可能还有更多问题,列出的是与read
有关的问题。我没有研究链表插入。
为了正确地以零终止字符串,我建议编写一个无限循环并在可能的重新分配后推迟 break
条件。敌人例子:
node_t *read(node_t *node)
{
int size = MAX_LEN;
int i = 0;
char *word = malloc(size);
if(word == NULL) {
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
while (1) {
int c = getchar();
if(i >= size) {
size = size*2;
word = realloc(word, size);
if (word == NULL) {
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
}
if (c == '\n' || c == EOF) {
word[i] = '[=10=]';
break;
}
word[i++] = c;
}
node = insertNode(word, node, size);
return node;
}
我正在尝试编写一个程序来读取用户输入的每个单词,然后将该单词粘贴到链表中。到目前为止,这是我尝试过的方法,但遇到了段错误,但不太确定 mallocing/pointers 哪里出错了。 (还没有实现 printList)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 20
typedef struct node{
char *word;
struct node *next;
}node_t;
node_t *read(node_t *node);
void printList(node_t *node);
node_t *insertNode(char *word, node_t *node, int size);
int main(int argc, char *argv[]) {
node_t *start = NULL;
printf("Enter a sentence:\n");
read(start);
return 0;
}
void *read(node_t *node){
int i, size = MAX_LEN;
char c, *word;
if(!(word=malloc(size))){
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
while((c=getchar())!='\n'){
for(i=0;c!=' ';i++){
word[i]=c;
if(i>size){
size=size*2;
if(!realloc(word, size)){
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
}
}
node = insertNode(word,node,size);
}
return node;
}
node_t *insertNode(char *word, node_t *node, int size){
node_t *new_node, *current;
new_node = (node_t*)malloc(sizeof(node_t));
new_node->next = NULL;
if(!(new_node->word = malloc(size))){
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(new_node->word,word);
if (node == NULL){
node = new_node;
current = new_node;
}
else{
current->next = new_node;
current = new_node;
}
return node;
}
我认为错误是由行引起的
return node;
在 insertNode
中。应该是
return new_node;
有几个问题:
- 您的原型与
read
的实现不匹配;使 return 成为node_t *
. - 您有两个用于输入的嵌套循环,一个从
stdin
读取,另一个循环遍历字符。内循环从不更新其条件,因为c
只能由外循环更改。应该只有一个循环,负责从流中读取数据并写入字符串。 - 您没有保留
realloc
的结果,这意味着当分配的内存句柄发生变化时,您不会反映更新。在这些情况下,您将访问已失效的旧句柄。 - 您没有以空字符终止您的字符串。
- 您应该在越界访问内存之前重新分配内存。这通常意味着在写入数组之前检查是否扩大数组。请注意,对于长度为
n
的数组,n
本身已经是一个非法索引。 getchar
的结果应该是int
,而不是char
,以便所有有效输入都不同于EOF
,您不检查.
可能还有更多问题,列出的是与read
有关的问题。我没有研究链表插入。
为了正确地以零终止字符串,我建议编写一个无限循环并在可能的重新分配后推迟 break
条件。敌人例子:
node_t *read(node_t *node)
{
int size = MAX_LEN;
int i = 0;
char *word = malloc(size);
if(word == NULL) {
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
while (1) {
int c = getchar();
if(i >= size) {
size = size*2;
word = realloc(word, size);
if (word == NULL) {
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
}
if (c == '\n' || c == EOF) {
word[i] = '[=10=]';
break;
}
word[i++] = c;
}
node = insertNode(word, node, size);
return node;
}