I've been working on a code to reverse a linked list, but I only seem to be getting the first element printed out in my list. Here is my function:
void reverseSqueue(Squeue squeue) {
if (squeue -> first == NULL || squeue -> last == NULL){
return;
}
Node* temp = NULL;
Node* curr = squeue -> first;
squeue -> first = squeue -> last;
squeue -> last = curr;
/*if (squeue -> first != squeue -> last){
curr = curr -> next;
}
printf("%s\n", curr -> next -> value);
printf("%s\n", curr -> value);
printf("%s\n", curr -> prev -> value);
printf("______________\n");
printf("%s\n", squeue -> first -> value);
Node* nde = squeue -> first;
while (nde != NULL){
printf("%s\n", nde -> value);
nde = nde -> prev;
}*/
while (curr != NULL){
/*if (curr != squeue -> first || curr != squeue -> last){
if (curr -> next == squeue -> last){
curr = NULL;
}else{*/
temp = curr;
curr -> next = curr;
curr = temp;
curr = curr -> prev;
//}
//}else{
//curr = NULL;
//}
}
}
I apologize for the messy code, I tried running multiple tests and commented them out here; I had code before that worked fine but gave a garbage value warning in OClint, so I tried fixing them and now my code just doesn't seem to work anymore. If nothing else works, I can just go back to my original code. Any input is much appreciated!
struct node {
char *value;
struct node *next;
struct node *prev;
};
typedef struct node Node;
struct squeue {
struct node* first;
struct node* last;
};
typedef struct squeue * Squeue;
With curr->next = curr you are saying that the next node of the current node is the current node, that is, current node points to itself as it's next node.