【问题标题】:Linked list inputs being overwritten链表输入被覆盖
【发布时间】:2011-10-08 22:43:02
【问题描述】:

我需要一些帮助,以便我的代码覆盖之前存储在我的链接列表中的输入。这个项目比我在这里的要大得多,但在我弄清楚这个问题之前我不能继续。因此,假设用户输入“ins mom”“ins dad”“ins bob”,如果他们执行命令“prl”,它将打印出“bob bob bob”。它得到了正确的节点数,但最后输入的 ins 命令总是填充列表并覆盖以前的内容。我花了一段时间试图修复它,但仍然无法弄清楚。有人可以帮我吗?

struct node{
    char *symbol;
    int count;
    struct node *next;
};
int main(void){

    void insert_node(struct node**,struct node**,char*,int);
    void print_list(struct node*); 

    struct node *head,*tail;
    char command[MAX]; 
    char word[MAX];
    int i = 1;
    head = tail = NULL;

    printf("Command? ");
    scanf("%s",command);
    if((strcmp(command,"prl")==0))
    {
        printf("The list is empty.");
        printf("Command? ");
        scanf("%s",command);    
    }
    else{
        scanf("%s",word);
    }
    while((strcmp(command,"end") != 0))
    {  
        if((strcmp(command,"ins")== 0))
        {
            insert_node(&head,&tail,word,i);
        }
        printf("Command? ");
        scanf("%s",command);
        if((strcmp(command,"prl")==0))
        {
            print_list(head);
        }
        else{
            scanf("%s",word);
        }
    }
    return 0;
}
void insert_node(struct node**h,struct node**t,char w[],int c) //inserts string into the list
{
    struct node *temp;

    if((temp = (struct node *)malloc(sizeof(struct node))) == NULL){
        printf("Node allocation failed. \n");
        exit(1);
    }
    temp->count = c;
    temp->symbol = w;
    temp->next = NULL; //edited this in

    if(*h == NULL)
    {
        *h = *t = temp;

    }
    else{
        (*t)->next = temp;  *t = (*t)->next;
    }
}
void print_list(struct node *h){ //prints the list

    if(h == NULL){
        printf("The list is empty.\n");
    }
    else{
        while(h != NULL)
        {
            printf("%d %s\n",h->count,h->symbol);
            h = h->next;
        }
    }
}

【问题讨论】:

  • 什么是h?如*h = *t = temp;?
  • @Lasse V. Karlsen 我认为它应该被解读为“如果 head 为空”(空列表),然后将 head 设置为 tail,tail 设置为 temp,因此它是一个循环链表。
  • 是的,如果 head 最初为 null,那么 head 和 tail 都指向 temp,因为列表只有一个节点长。

标签: c list linked-list


【解决方案1】:

首先你应该意识到给空间:

temp->symbol

而不仅仅是使用一个简单的等式在另一个中插入一个字符串。使用:

strcpy()

在为字符串分配内存之后。

其次,在你的 print 函数中,所有节点都应该被打印,但最后一个需要,因为 while 循环将被终止。更好地检查你的代码,你会没事的:)

【讨论】:

  • 如果我注释掉 while 循环,所以我的打印函数只打印 h 值,它仍然会打印出用户输入的最后一个字符串,而忽略之前输入的任何其他内容。我认为我的问题与插入方法有关。
【解决方案2】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 80

typedef struct node{
    char *symbol;
    int count;
    struct node *next;
} Node;

Node* make_node(char *word, int count){
    Node *temp;
    char *w;
    if((temp = (Node*)malloc(sizeof(Node))) == NULL){
        printf("Node allocation failed. \n");
        exit(1);
    }
    if((w = strdup(word)) == NULL){
        printf("word allocation failed. \n");
        exit(1);
    }
    temp->count = count;
    temp->symbol = w;
    temp->next = NULL;
    return temp;
}

void node_free(Node *node){
    if(node == NULL) return;
    if(node->next){
        node_free(node->next);
    }
    free(node->symbol);
    free(node);
}

void insert_node(Node **h, Node **t, char *w, int c){ //inserts string into the list
    Node *temp;

    temp = make_node(w, c);

    if(*h == NULL){
        *h = *t = temp;
    } else {
        (*t)->next = temp;
        *t = temp;
    }
}
void print_list(Node *h){ //prints the list

    if(h == NULL){
        printf("The list is empty.\n");
    }
    else{
        while(h != NULL){
            printf("%d %s\n",h->count, h->symbol);
            h = h->next;
        }
    }
}

int main(void){
    Node *head,*tail;
    char command[MAX]; 
    char word[MAX];
    int i = 1;
    head = tail = NULL;

    do{  
        printf("Command? ");
        scanf("%s", command);
        if(strcmp(command,"prl") ==0){
            print_list(head);
        } else  if(strcmp(command,"ins") == 0){
            printf("input word:");
            scanf("%s",word);
            insert_node(&head,&tail, word, i++);
        }
    }while(strcmp(command,"end") != 0);
    node_free(head);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多