user985843
user985843

Reputation: 1

Linked list inputs being overwritten

I need some help with my code overwriting previous inputs that were stored in my linked list. This project is a lot bigger than what I have here but I can't continue on until I figure this issue out. So say a user inputs "ins mom" "ins dad" "ins bob" if they do the command "prl" it would print out "bob bob bob". It's getting the number of nodes correct but the last ins command entered always fills the list and overwrites the previous things. I spent awhile trying to fix it but still can't figure it out. Can someone help me?

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

Upvotes: 0

Views: 1457

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

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

Upvotes: 0

nLamprok
nLamprok

Reputation: 77

First of all you should aware giving space to:

temp->symbol

and not just use a simple equation to insert a string in an other. Use:

strcpy()

after the memory allocation for the string.

Secondly, in your print function, all nodes should be printed but the last want, since the while loop will be terminated. Check better your code and you'll be fine :)

Upvotes: 1

Related Questions