2021年4月7日星期三

Reversing a doubly linked list in C

I'm currently trying to reverse a doubly-linked list and print it out in C. Unfortunately, my code is only printing one item and I have no idea why. I have looked at multiple solutions on Stack Overflow and online

Basically, I pass a txt file that looks like this.

Name 100  John 40  

Here's the full code:

    #include <stdio.h>      #include <string.h>      #include <stdlib.h>            typedef struct person{        char* firstName;          char* age;        struct person* previous;         struct person* next;       }Person;            void print(Person*);            void reverseList(Person*);            Person* add (Person*, char*, char*);            void print (Person* list) {        while(list != NULL) {          printf("%s %s\n",list->firstName,list->age);          list = list->next;        }      }            void reverseList (Person* list) {        Person *current = list;        Person* last = current;              while(current != NULL) {          Person *temp = NULL;          temp = current->previous;           current->previous = current->next;          current->next = temp;          last = current;          current = current->previous;        }        list = last;              while(list != NULL) {          printf("%s %s\n",list->firstName,list->age);          list = list->next;        }      }            Person* add (Person *list, char *firstName, char *age) {        // allocate node        Person* node = (Person*)malloc(sizeof(Person));              // if node is empty        if(node != NULL) {          node->firstName=(char *)malloc(sizeof(char)*strlen(firstName)+1);                    // insert name          strcpy(node->firstName, firstName);                // allocate age & insert          node->age = (char*)malloc(sizeof(char)*strlen(age)+1);          strcpy(node->age, age);          node->next = NULL;                if(list == NULL) list = node;          else {            Person *curr = list;            Person *pre = NULL;            while(curr != NULL) {              if(strcmp(curr->firstName, firstName) > 0) {                if(pre == NULL) {                  node->next = list;                  list = node;                } else {                  pre->next = node;                  node->next = curr;                }                return list;              }              pre = curr;              curr = curr->next;            }            pre->next = node;          }          return list;        }        return list;      }            int main (int argc, char **argv) {        FILE *in = fopen(argv[1], "r");        char name[20];        char num[20];        Person *list=NULL;               while(fscanf(in, "%s %s",name, num)==2) {          list = add(list,name, num);        }              printf ("Original List\n***\n");        print (list);                printf ("Printing Reversed List\n***\n");        reverseList(list);                return 0;      }  

The goal is, if I pass a list like:

John 10  Mary 5  

It would print

Mary 5  John 10  

Any idea what's going on here? Not sure what I am misunderstanding

https://stackoverflow.com/questions/66996368/reversing-a-doubly-linked-list-in-c April 08, 2021 at 10:01AM

没有评论:

发表评论