2011-10-08 73 views
0

我需要一些帮助,用我的代码覆盖存储在我的链接列表中的以前的输入。这个项目比我在这里的要大得多,但我不能继续下去,直到我解决这个问题。因此,如果用户输入“ins妈妈”,“ins爸爸”,“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

什么是'h'?正如'* h = * t = temp;'? –

+0

@Lasse V. Karlsen我认为它应该被理解为“如果头是空的”(空列表),然后将头部尾部和尾部设置为温度,因此它是一个循环链表。 – dcousens

+0

是的,如果head最初是null,那么head和tail都指向temp,因为列表只有一个节点长。 – user985843

回答

1

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

temp->symbol 

,而不是只用一个简单的公式中插入在其他的字符串。使用方法:

strcpy() 

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

+0

如果我注释掉while循环,所以我的打印函数只是打印h值,它仍然会打印出用户输入的最后一个字符串,忽略之前输入的任何其他字符。我认为我的问题与插入方法有关。 – user985843

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