1

I'm quite new to C and programming in general.
I did a bubble sort on a linked list so node with the highest int should be the last one.

I would like to know if its possible to only print the name(char [20]) of the last node?

Here's what I've got so far:

void display(albumlist *head) {  
  albumlist *current = head;  
  if(head == NULL) {  
    printf("error\n");  
    return;  
  }  
  while(current != NULL) {     
    printf("%s ", current->name);   
  }  
  printf("\n");  
}  
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
bihyuh
  • 11
  • 2
  • Yes -- keep a `tail` pointer to the last node in the list , in addition to a `head` pointer to the first node in the list. (makes insert-at-end `O(1)` time as well) Suggest `if (head == NULL) { puts ("(list-empty)"); return; }` – David C. Rankin Dec 18 '20 at 04:49

2 Answers2

1

Your display will print all nodes, which is what one normally wants to do. It works fine for that.

To print only the last node, it needs a slight adjustment:

void
display_last(albumlist *head)
{
    albumlist *current = head;

    if (head == NULL) {
        printf("error\n");
        return;
    }

    albumlist *last = NULL;
    for (;  current != NULL;  current = current->next)
        last = current;

    printf("%s\n", last->name);
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • In a comment to a now-deleted question that featured `main() { … }`, I commented about `int main(void)` being preferred although `int main()` can also be used. You asked: _Isn't `int main()` a K&R [vs. ANSI `int main(void)`]? A full K&R would be: `int main(argc,argv) int argc; char **argv { ... }`_. I'm commenting here for lack of a better way to discuss it – see my profile if you'd like to use email. The code in the Q missed the return type; that was allowed in K&R and C90, but outlawed in C99 (the return type is mandatory). _[…continued 1…]_ – Jonathan Leffler Dec 22 '20 at 20:18
  • _[…continuation 1…]_ The argument list is optional, but `int main()` does not define a prototype, which mostly doesn't matter but doesn't stop you writing `main("C90", 3.14159)` in your code. The preferred style in K&R was `int main()` or the version you showed, but it was not mandatory. And you could omit the `int argc;` or list it after `char **argv;` if you were perverse enough. So, `int main()` is K&R and standard C (up to and including C18), but is not the preferred notation. Omitting the return type was K&R and C90 only. _[…continued 2…]_ – Jonathan Leffler Dec 22 '20 at 20:23
  • _[…continuation 2…]_ The implementation is not allowed to provide a declaration for `main()` because different signatures are valid (both `int main(void);` and `int main(int argc, char **argv);` — and others are mentioned in Annex J.5, and are defined as valid by implementations. A lot of this is discussed in my answer to [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336). – Jonathan Leffler Dec 22 '20 at 20:26
1

if you want to print only the last node value then you need to check for current->next != NULL, when that satisfies that means you have reached last node, then you can print current->name

void display(albumlist *head) {  

    albumlist *current = head;  
    if(head == NULL) 
    {  
        puts("error");  
        return;  
    }  
    while(current->next != NULL)
        current = current->next;
    puts(current->name);   
}  
IrAM
  • 1,720
  • 5
  • 18