0

I am trying to reverse a linked list in c, that I created by copying the data from a structure.

I have created the following function named reverselist, after reading a lot of suggestions about reversing a list, but after applying it, and trying again to print I get nothing printed on my screen.

I don't want to just print the file, but I want the linked list permanently changed. Can someone spot out what is the problem in my code?

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

int i,j,temp;

typedef struct{
    int nr;
}PASSENGERS;

typedef struct list1{
    int nr;
    struct list1 *next;
}LIST1;


LIST1* reverselist (LIST1 *head)
{
    LIST1 *cursor=NULL;
    LIST1 *next;
    while(head){
        next=head->next;
        head->next=cursor;
        cursor=head;
        head=next;
    }
    return cursor;
}

int main()
{
    PASSENGERS passenger[53];
    for (j=0;j<53;j++)
        passenger[j].nr=j+1;

    PASSENGERS *start=NULL;
    char selection;
    do{
        printf("0. Exit and Print File\n");
        scanf(" %c",&selection);
    } while (selection!='0');

    LIST1 *list1, *start=NULL;
    for (i=0;i<53;i++){
        list1 = (LIST1 *) malloc (sizeof(LIST1));
        list1->next = NULL;
        list1->nr = passenger[i].nr;

        if (start ==NULL)
            start = list1;
        else //add new node at the beginning of list
        {
            list1->next = start;
            start = list1;
        }
    }

    LIST1 *current = list1;
    printf("The original list is:");
    while (current !=NULL) /* Printing the names on the list ok*/
    {
        printf("%d\n",current->nr);
        current = current->next;
    }

    LIST1* head = NULL;
    head=reverselist(head);

    printf("The reversed list is:");

    while (current !=NULL){
        printf("%d\n",current->nr);
        current = current->next;
    }
}
2
  • 2
    There are numerous questions about how to reverse the items in a singly-linked list. Are you sure you can't find any of them. Commented Dec 21, 2016 at 20:25
  • Well I have found many ways, and I tried to apply some of them.. The one that looks closest to a solution I think is the one I posted here, but still can't seem to make it work.. Thanks for the suggestion, and I am still focused on reading here and there to find the solution ! ! Commented Dec 21, 2016 at 20:27

1 Answer 1

1

This code is obviously wrong, because you're passing NULL to reverselist:

LIST1* head = NULL;
head=reverselist(head);

I'm guessing that this should be:

current = reverselist(list1);

(You don't need the head variable here because you are never using it)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, it was just in front of my eyes but couldn't spot the problem!
@baskon1 - of the 16 questions you've asked so far on StackOverflow, you've only accepted 1 answer. While you are not required to accept an answer, you should make an effort to when an answer resolves the issues you are facing.
Sorry! Will do it from now on! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.