0

When I print the linked list countryName value always stay with the last string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct usa_primaries * ptr;

typedef struct primariesDate{
    int month;
    int day;
    int hour;
}primariesDate;

 typedef struct usa_primaries{
primariesDate date;
char *countryName;
int isOpen;
ptr next;
}usa_primaries;

void add (int month, int day, int hour, char *country, int isOpen, ptr *head) {
    ptr t;
    t = (ptr) malloc(sizeof(usa_primaries));

    t->date.month = month;
    t->date.day = day;
    t->date.hour = hour;
    t->countryName = (char *)  malloc(strlen(country)+1);
    strcpy(t->countryName, country);
    t->isOpen = isOpen;

    if(!(*head)) {
        t->next = NULL;
        *head = t;
    }
    else {
        t->next = *head;
        *head = t;
    }

Below is the main function, I'm trying to print only countryName details but what I see is only the last value that is inserted. for example: scanf: test1, test2 the output is: test2 test2

int main() {
    ptr head = NULL;
    int month, day, hour, isopen;
    char country[20];
    while (scanf("%d %d %d %s %d", &month, &day, &hour, country, &isopen) != EOF) {
        add(month, day, hour, country, isopen, &head);
    }
    ptr print = head;
    while (print) {
        printf("\n %s ", head->countryName);
        print = print->next;
    }

    free(head);
    return 0;
}
  • Your `add` function is **pre**pending to the list. Is this what you want? – Angew is no longer proud of SO Aug 23 '19 at 09:27
  • yes, i think something wrong there. –  Aug 23 '19 at 09:30
  • 4
    Please indent your code properly and provide a [MCVE]. Also read this: [ask]. – Jabberwocky Aug 23 '19 at 09:31
  • 2
    Both branches of the final conditional do the same thing. (If `*head` is null, `t->next = NULL` is equivalent to `t->next = *head`.) – molbdnilo Aug 23 '19 at 09:31
  • 3
    Please post code that creates a minimal list and outputs it, and which exhibits the problem you mention. That is, a [mcve]. – molbdnilo Aug 23 '19 at 09:33
  • Typedeffing pointers to structs is not a very good idea, and `ptr` is a really bad name for that. – vgru Aug 23 '19 at 09:43
  • 1
    [Do not cast the value returned from `malloc()`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – pmg Aug 23 '19 at 09:44
  • What is `usa_primaries` ? What is `ptr`? – KamilCuk Aug 23 '19 at 09:57
  • 1
    Please provide minmal input and expected vs. actual output. – Jabberwocky Aug 23 '19 at 10:02
  • `printf("\n %s ", head->countryName);` should be `printf("\n %s ", print->countryName);`, you are basically printing the head while moving the print along the linked list. – MadKarel Aug 23 '19 at 10:04
  • 1
    OT: `typedef struct usa_primaries * ptr;` this is a very bad idea. Don't hide pointers behind typedefs, this only adds confusion. What is more clear, `ptr head;` or `struct usa_primaries *head;`? Also the name `ptr` is a very poor choice, it should at least be named something like: `usa_primaries_ptr`. – Jabberwocky Aug 23 '19 at 10:08

1 Answers1

2
while (print) {
    printf("\n %s ", head->countryName);
    //               ^^^^
    print = print->next;
}

You print only the head in the loop. You should print the current node:

while (print) {
    printf("\n %s ", print->countryName);
    //               ^^^^^
    print = print->next;
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111