0

I want to create with the existing singly linked list a new list but backwards. First, print the original list, then backwards.

The progress so far:

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

struct Digits {
  int digit;
  struct Digits* next;
};

void displayNodes(struct Digits*);
void freeNodes(struct Digits* );
struct Digits* backwardsList(struct Digits*, int);
struct Digits* appendNodeFront(struct Digits*, struct Digits*);
struct Digits* appendNode(struct Digits*, struct Digits*);
struct Digits* createNode(int);

int main(void) {
  int digitNodes, digits;

  struct Digits* newNode, * headNode, * endNode, * backwards;

  printf("Nodes: ");
  scanf("%d", &digitNodes);

  for(int i = 0; i < digitNodes; i++) {
    printf("Enter numebr: ");
    scanf("%d", &digits);
    newNode = createNode(digits);

    if(i == 0) {
      headNode = endNode = newNode;
    }
    else {
      endNode = appendNode(endNode, newNode);
    }
  }

  backwards = backwardsList(headNode, digits);

  displayNodes(backwards);
  displayNodes(headNode);
  freeNodes(headNode);
  freeNodes(backwards); // no need for this statement right? 
}

struct Digits* backwardsList(struct Digits* headNode, int digits) {
  struct Digits* headNodeCpy = headNode, * endNode, * newNode;

  for(int i = 0; i < digits; i++) {
    newNode = createNode(headNodeCpy->digit);
    headNodeCpy = headNodeCpy->next;

    if(i == 0) endNode = newNode;
    else endNode = appendNodeFront(newNode, endNode);
  }

  return endNode;
}

struct Digits* appendNodeFront(struct Digits* newNode, struct Digits* endNode) {
  newNode->next = endNode;

  return newNode->next;
}

void freeNodes(struct Digits* headNode) {
  int i = 0;
  struct Digits* headNodeCpy = headNode;
  struct Digits* tmp;

  while(headNodeCpy != NULL) {
    tmp = headNodeCpy->next;
    free(headNodeCpy);
    headNodeCpy = NULL;
    if(headNodeCpy == NULL) printf("Node %d cleared\n", i);
    else printf("Error, node %d not cleared\n", i);
    headNodeCpy = tmp;
    i++;
  }

printf("\n");
}

void displayNodes(struct Digits* headNode) {
  struct Digits* headNodeCpy = headNode;

  while(headNodeCpy != NULL) {
    printf("%d\n", headNodeCpy->digit);
    headNodeCpy = headNodeCpy->next;
  }

  printf("\n");
}

struct Digits* appendNode(struct Digits* endNode, struct Digits* newNode) {
  endNode->next = newNode;

  return endNode->next;
}

struct Digits* createNode(int digits) {
  struct Digits* newNode;

  newNode = (struct Digits*)malloc(sizeof(struct Digits));

  newNode->digit = digits;
  newNode->next = NULL;

  return newNode;
}

I think that the error occurs in the backwardsList() function, because i didn't set the "end" of this backwards list like i do up in main().

Function where the error might occurs:

struct Digits* backwardsList(struct Digits* headNode, int digits) {
  struct Digits* headNodeCpy = headNode, * endNode, * newNode;

  for(int i = 0; i < digits; i++) {
    newNode = createNode(headNodeCpy->digit);
    headNodeCpy = headNodeCpy->next;

    if(i == 0) endNode = newNode;
    else endNode = appendNodeFront(newNode, endNode);
  }

  return endNode;
}

Any help appreciated!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
I Y S Z
  • 45
  • 5
  • try to debug your code since segmentation fault likely to happen almost every segment of your code. – Ali Ziya ÇEVİK Apr 14 '22 at 14:08
  • I've read a document about debugging with gdb but i don't understand anything – I Y S Z Apr 14 '22 at 14:09
  • okay then, just print something to the console in your functions that you are calling in main. For instance, print something in your ```backwardsList``` function before the loop and after the loop. – Ali Ziya ÇEVİK Apr 14 '22 at 14:12
  • If you think ```backwardsList``` function is the problem, then in your ```main``` print something before calling the ```backwardsList``` and after. – Ali Ziya ÇEVİK Apr 14 '22 at 14:16
  • Il try to do that then – I Y S Z Apr 14 '22 at 14:16
  • it seems like that insinde of `backwardsList` the LAST node is creating this error. – I Y S Z Apr 14 '22 at 14:24
  • ` if(i == 0) endNode = newNode; else endNode = appendNodeFront(newNode, endNode); }` this is the error maker in this case then – I Y S Z Apr 14 '22 at 14:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243902/discussion-between-ali-ziya-cevk-and-i-y-s-z). – Ali Ziya ÇEVİK Apr 14 '22 at 14:31
  • There are many (over 300) questions that get listed with an SO search term '`[c] reverse linked list is:q`'. At least one of those should be able to help you sort out your trouble. – Jonathan Leffler Apr 14 '22 at 15:41
  • I suggest that [How to reverse a singly-linked list using only two pointers?](https://stackoverflow.com/q/1801549/15168) is worth studying. Look at the answers with the highest scores and the check mark — they're the ones most worth study. – Jonathan Leffler Apr 14 '22 at 16:05

0 Answers0