-1

I am trying to make linked list. I can add node one by one but I could not print the linked list as i wanted. How to print linked list node from top to bottom

#include<stdio.h>
#include<stdlib.h>

struct node{
  int N;
  struct node *next;
};

struct node* newNode(int number, struct node *next) {
    struct node *new = malloc(sizeof(*new));
    new->N = number;
    new->next = next;
    return new;
}

void show(struct node *head){
     struct node *c;
     c = head;
     while (c!=NULL){
           printf("%d\n",c->N);
           c = c->next;
     }
}

int main (void ) {
    struct node *head = NULL;

    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
    head = newNode(40, head);

    show(head);
    return 0;
}

Output 40 30 20 10

I am trying to print node like below

10 20 30 40

How to get above output ?

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
Daljeet12
  • 73
  • 1
  • 2
  • 11
  • This is not valid c++. Please dont tag unrelated languages (`new` is a keyword in C++) – 463035818_is_not_an_ai Nov 21 '19 at 10:06
  • You're printing the list correctly, head to tail. You are adding your elements at the head each time, however, treating it like a stack. You could either add new elements to the tail instead of at the head, or print your list from back (tail) to front (head). – lurker Nov 21 '19 at 10:08
  • Would you consider changing your current list structure? You can switch to double-linked list and then either add new elements to tail or iterate starting from tail. – Denis Sheremet Nov 21 '19 at 10:09
  • 2
    You are inserting the new node at the beginning of the list. If you insert at the end, then you will get the results as expected. – Rishikesh Raje Nov 21 '19 at 10:09
  • You've got two broad routes you could take: 1. Modify the structure to be a doubly-linked list; here you have prev(ious) links as well as next, so you can just start at the end and work backwards. 2. Work down the list until you find the end, then print that out; keep track of the one-before-the-end and print that out, perhaps using a recursive call. How often you want to print the list backwards vs. how often you add or remove elements are factors you could consider when choosing 1 or 2. – Edd Inglis Nov 21 '19 at 10:11
  • Don't forget to `free()` everything once you are done... – moooeeeep Nov 21 '19 at 10:12

2 Answers2

3

Since I understand this is probably part of an excercise, I will attempt to answer it in the style of assistance, while still giving a comprehensive answer.

I let aside the fact that you insert your elements in the head - which I am not sure it is what you want to do, and I consider the question as "How to print it backwards, once I have entered the elements correctly?".

We have to examine possible solutions on this:

1) Create a method void addToTail(Node* head, int value); That traverses the list and adds elements to the tail of the list instead of the head. Side note: this operation is time costly, as it requires O(N) time complexity. About complexities, read more here. Se also this StackOverflow question.

2) You mention the term "linked list". By what you say, you do not specify if it is a singly linked, or a doubly linked. And since you have access to the node implementation, I suggest that you add a pointer to each node that points to the previous element, thus converting your singly-linked list to a doubly-linked one.

struct node{
  int N;
  struct node *next;
  struct node *prev;
};

And, of course, you need to update this node respectively in the operations of your list - otherwise it will not work - I let this to you.

That way, you will be able to easily iterate the list backwards then in order to print the number in the order desired.

3) You could implement a function Node * reverseList(Node* head); that "reverses" a list via iteration, and then use it to print the list reversed. And again, I let the implementation to you. Of, course, you need to consider the list state on each time, as also if you need to reverse the list in-place or return a pointer to a new, reversed list (as the function contract above indicates).

What you need to do now, is re-read your excercise brief, stop for a moment and think: "Do I really need these solutions? Is this what is requested from me?".

If you are just entering the data in the wrong order, probably not.

But if it is specifically required from you to print the list elements backwards, then you have some good hints on how to proceed.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • I think that transforming the singly linked list into a doubly linked list is both unneeded and out of scope of the question. Furthermore if this is part of an exercise like you assume it is much more likely for the list to need to be singly linked. – Marco Bonelli Nov 21 '19 at 10:13
  • I agree with you, as you are probably right - yet, my purpose is to give the available options to the OP with a "hint" - and not actual implementation, so that 1. he/she knows the alternative and 2. think if is indeed needed so that he/she proceeds with that. – Nick Louloudakis Nov 21 '19 at 10:18
  • 1
    If you want to help OP this way I would advise explaining addition to tail or reversing of a list instead, makes more sense IMHO. – Marco Bonelli Nov 21 '19 at 10:19
  • Excellent point, I will update the question respectively, thanks @MarcoBonelli! – Nick Louloudakis Nov 21 '19 at 10:25
1

Recursive approach works great here:

void show(struct node *c){
    if (c == NULL)
        return;
    show(c->next);
    printf("%d\n", c->N);
}
sephiroth
  • 896
  • 12
  • 22