Andre Stern
Andre Stern

Reputation: 33

Problems with strings in a linked list. Fields are overwritten with the last nodes's inputs

I've been trying to create an album catalog in which each node on a linked list will have an index (code), the artist's name and the album tytle. For some reason however, every time I try to print the list, it will show the correct indexes I've assigned for each node, but the artist and tytle displayed will be, for every item, the ones I've entered for the very last node. That is, if I've entered '1, Oasis and Definetely_Maybe' for one node and '5, Aerosmith and Pump' for the second node, when I run print_list, it will show

Code: 1

Artist: Aerosmith

Album: Pump

and

Code: 5

Artist: Aerosmith

Album: Pump

Somehow, it overwrites the first node's artist and album tytle with the last. It will happen regardless of how many nodes I enter before I end the run.

I understand this is very novice, but I have been only just started programing and would greatly appreciate the help. The code is as follows. Thanks a lot.

 #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100

    typedef struct n {int code; char *artist; char *album; struct n* next;} Node;

    void start_list (Node **first);
    Node *create_node (int code, char *art, char *alb);
    void insert_list (Node **first, Node *next);
    void print_list (Node *p);
    void exit_list (Node **p);

    int main(int argc, char *argv[])
    {
      Node *first;
      Node *new;
      int n;
      char artist[MAX];
      char album[MAX];
      start_list(&prim);
      do{
        printf("Please enter a number for the next Node or 0 to exit the list: ");
        scanf("%d",&n);
        if(n==0)break;
        printf("\nNow enter the artist's name: ");
        scanf("%s",&artist);
        printf("\nType the album tytle now: ");
        scanf("%s",&album);
        printf("\n\nCode: %d ",n);
        printf("\nArtist: %s ",artist);
        printf("\nAlbum: %s \n",album);
        new=create_node(n,artist,album);
        insert_list(&first,new);
      }while(n!=0);
      print_list (prim);
      exit_list(&prim);
      system("PAUSE");  
      return 0;
    }

    void start_list (No **prim){
      *prim=NULL;
    }  

    Node *create_node (int code, char *art, char *alb){
      Node *new;
      new=(Node*)malloc(sizeof(Node)); 
      new->code=code;
      new->artist=art;
      new->album=alb;
      new->next=NULL;
      return new;
    }

    void insert_list (Node **first, Node *new){
      new->next=*first;
      *first=new;
    }

    void print_list (Node *p){
      Node *aux=p;
      while (aux!=NULL){
        printf("\n\nCode: %d ",aux->code);
        printf("\nArtist: %s ",aux->artist);
        printf("\nAlbum: %s \n",aux->album);
        aux=aux->next;
      }
    }

    void exit_list (Node **p){
      Node *aux=*p;
      while(aux!=NULL){
        *p=(*p)->next;
        free(aux);
        aux=*p;
      }
    }

Upvotes: 2

Views: 284

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109593

  Node* first = NULL;
  new->artist = strdup(art);
  new->album = strdup(alb);

and in exit_list freeing them, just before free(aux).

    free(aux->art);
    free(aux->alb);
    free(aux);

Upvotes: 1

A.E. Drew
A.E. Drew

Reputation: 2137

Your create_node function sets the char pointers artist and album to point to the artist and album arrays in the main function. Each iteration just rewrites what is stored in these arrays. Thus, every node points to the same strings and at the end of your process, the most recently entered strings are in the arrays.

You need to allocate (e.g. use malloc or a string function that uses the heap) storage for each new string so that the strings persist between calls and are not overwritten.

Upvotes: 0

Related Questions