WizDom
WizDom

Reputation: 13

C - Linked list printing in the wrong order

I have created a linked list whose elements are strings acquired from command line arguments:

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

    struct element_Args {
        char commandLineArgs[500];
    };


    struct list {

    struct element_Args element;
    struct list *next;
    };

    int main(int argc, char *argv[]) {

    struct list *head;
    struct list *current;

    head = (struct list *) malloc(sizeof(struct list));


    head->next = NULL;

    int i;
    for(i = 0; i < argc; i++) {
        current = malloc (sizeof(struct list));
        strcpy(current->element.commandLineArgs, argv[i]);
        current->next = head;
        head = current;

    }

    current = head;

    while(current->next != NULL) {
       printf("%s\n", current->element.commandLineArgs);
       current = current->next;
    }

    return 0;

    }

However, when I print the elements in the linked list, they are printing out in the reverse order from which they were entered as arguments. How can I print them in the same order in which they were entered? I feel as though I'm missing something small, but I can't figure out what that is.

Upvotes: 1

Views: 207

Answers (2)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

Here is your problem

head = current;

you should make head point to the first node, and never overwrite it again.

So in your code, head is actually the tail from that it is logical that the values are printed in the reverse order, it's not reverse you expect the reverse of it.

Try this

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

struct element_Args {
    char commandLineArgs[500];
};


struct list {

struct element_Args element;
struct list *next;
};

int main(int argc, char *argv[]) {
    struct list *head;
    struct list *current;
    struct list *last;
    int i;

    head = malloc(sizeof(*head));
    if (head == NULL)
        return -1;
    head->next = NULL;

    last = head;
    for(i = 0 ; i < argc ; i++) {
        current = malloc (sizeof(*current));
        if (current != NULL) {

            strcpy(current->element.commandLineArgs, argv[i]);

            last->next = current;
            last       = current;
        }
    }

    current = head;
    while(current != NULL) {
        printf("%s\n", current->element.commandLineArgs);
        current = current->next;
    }

    return 0;
}

Don't forget to write a freeList() function to free all the malloced stuff.

Upvotes: 0

mattsap
mattsap

Reputation: 3806

In your for loop, remove head = current.

Basically, you're losing track of your head by using this line. You can later traverse the head by setting temp pointers, but don't reset the head (unless you're inserting a new head).

To insert a new head you would say, newHead->next = head; head = newHead; If you want to insert them in order, you should keep a tail pointer and always add at the end.

int i;
struct list* tail = head;
for(i = 0; i < argc; i++) {
    current = malloc (sizeof(struct list));
    if(current != NULL){
        strcpy(current->element.commandLineArgs, argv[i]);
        tail->next =  current; // add this line
        tail = tail->next;
        current->next = head; //this line makes you add in reverse order. Remove this as well.

        head = current; // remove this line here
    }


}

Upvotes: 3

Related Questions