链接列表输入被覆盖

时间:2011-10-08 22:43:02

标签: c list linked-list

我需要一些帮助,我的代码会覆盖存储在链接列表中的先前输入。这个项目比我在这里的项目要大得多,但在我解决这个问题之前我无法继续。所以说用户输入“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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

首先,你应该知道给予空间:

temp->symbol

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

strcpy()

为字符串分配内存后。

其次,在您的打印功能中,应该打印所有节点但最后需要,因为while循环将被终止。检查你的代码会更好,你会没事的。)

答案 1 :(得分:0)

#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;
}