C 正在覆盖链接列表输入

C 正在覆盖链接列表输入,c,list,linked-list,C,List,Linked List,我需要一些帮助,帮助我的代码覆盖以前存储在链接列表中的输入。这个项目比我这里的要大得多,但我不能继续下去,直到我弄清楚这个问题。假设用户输入“ins mom”“ins dad”“ins bob”,如果他们执行命令“prl”,它将打印出“bob”。它获得了正确的节点数,但最后输入的ins命令总是填充列表并覆盖以前的内容。我花了一段时间试图修复它,但仍然无法解决它。有人能帮我吗 struct node{ char *symbol; int count; struct node

我需要一些帮助,帮助我的代码覆盖以前存储在链接列表中的输入。这个项目比我这里的要大得多,但我不能继续下去,直到我弄清楚这个问题。假设用户输入“ins mom”“ins dad”“ins bob”,如果他们执行命令“prl”,它将打印出“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;
        }
    }
}

首先,您应该意识到为以下各项留出空间:

temp->symbol
而不仅仅是用一个简单的方程在另一个方程中插入一个字符串。使用:

strcpy()
在为字符串分配内存之后

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

\include
#包括
#包括
#定义最大值80
类型定义结构节点{
字符*符号;
整数计数;
结构节点*下一步;
}节点;
节点*生成节点(字符*字,整数计数){
节点*温度;
char*w;
if((temp=(Node*)malloc(sizeof(Node)))==NULL){
printf(“节点分配失败。\n”);
出口(1);
}
if((w=strdup(word))==NULL){
printf(“字分配失败。\n”);
出口(1);
}
温度->计数=计数;
温度->符号=w;
temp->next=NULL;
返回温度;
}
无效节点\u自由(节点*节点){
if(node==NULL)返回;
如果(节点->下一步){
节点空闲(节点->下一步);
}
自由(节点->符号);
自由(节点);
}
void insert_node(node**h,node**t,char*w,int c){//将字符串插入列表
节点*温度;
temp=制造节点(w,c);
如果(*h==NULL){
*h=*t=温度;
}否则{
(*t)->next=温度;
*t=温度;
}
}
void print_list(节点*h){//打印列表
if(h==NULL){
printf(“列表为空。\n”);
}
否则{
while(h!=NULL){
printf(“%d%s\n”,h->count,h->symbol);
h=h->next;
}
}
}
内部主(空){
节点*头,*尾;
char命令[MAX];
字符字[MAX];
int i=1;
头=尾=空;
做{
printf(“命令?”);
scanf(“%s”,命令);
如果(strcmp(命令,“prl”)==0){
打印列表(标题);
}else if(strcmp(命令,“ins”)==0){
printf(“输入字:”);
scanf(“%s”,单词);
插入_节点(头、尾、字、i++);
}
}while(strcmp(命令,“结束”)!=0);
无节点(头部);
返回0;
}

什么是
h
?如
*h=*t=temp?@Lasse V.Karlsen我认为应该理解为“如果head为null”(空列表)然后将head-to-tail和tail-to-temp设置为循环链表。是的,如果head最初为null,那么head和tail都指向temp,因为列表只有一个节点长。如果我注释掉while循环,那么我的print函数只打印h值,它仍然打印出用户输入的最后一个字符串,忽略之前输入的任何其他内容。我认为我的问题与insert方法有关。
#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;
}