Chris3101
Chris3101

Reputation: 55

How to reverse a single-linked list in C?

for my class I have the task to build a linked list, enter data and perform different functions. My code is compiling fine, however the reversion and the constructive reversion function are not working.

If I reverse the list and print it, I only get the first node back. I seem to be missing the point somewhere. Here is my code: (If you notice any mistakes in any of the other functions do let me know) Thanks in advance!

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

typedef struct DoubleNode{
    struct DoubleNode* next;
    double data;

} DoubleNode;

DoubleNode* insertFirst(DoubleNode*head, double c) {
    DoubleNode* temp;
    temp=malloc(sizeof(DoubleNode));
    temp->data= c;
    temp->next= head;

    return temp;
}
void printList(DoubleNode*head) {
    DoubleNode* cursor;
    printf("(");
    cursor=head;
    while (cursor != NULL) {
        printf("%fl", cursor->data);
        printf(" ");
        cursor=cursor->next;
    }
    printf(")");
}


DoubleNode* insertLast(DoubleNode *head, double d) {
    DoubleNode *tmp, *cursor;
    //trivial case: empty list
    if (head==NULL)
    {return insertFirst(head,d);
    } else{
        //general case: goto end
        cursor=head;
        while (cursor->next != NULL){
            cursor =cursor->next;
        }
        tmp= malloc(sizeof(DoubleNode));
        tmp->data = d;
        tmp->next = NULL;
        cursor->next=tmp;
        return head;
    }
}

DoubleNode* reverseDoubleListCon(DoubleNode*head) {
    DoubleNode *temp, *res, *cell;
    cell=head;
    res=NULL;
    while (cell!=NULL) {
        temp=malloc(sizeof(DoubleNode));
        temp->data=cell->data;
        temp->next=res;
        res=temp;
        cell=cell->next;
    }
    return res;
}

void reverseDoubleList(DoubleNode*head) {
    DoubleNode *chain, *revChain, *cell;
    cell=head;
    revChain=NULL;
    while (cell!=NULL) {
        chain=cell->next;
        cell->next=revChain;
        revChain=cell;
        cell=chain;
    }
    head=revChain;
}

double get(DoubleNode*head, double n) {
    DoubleNode *cursor;
    cursor=head;
    for (int i=0; i<n; i++) {
        cursor=cursor->next;

    }
    return cursor->data;
}

DoubleNode* delete(DoubleNode*head, double n) {
    DoubleNode *cursor, *rest;
    cursor=head;
    for (int i=0; i<n; i++) {
        cursor=rest;
        cursor=cursor->next;

    }
    rest->next=cursor->next;
    free(cursor);
    return head;

}

DoubleNode* insert(DoubleNode*head, double d, double n) {
    DoubleNode *cursor, *temp, *rest;
    cursor=head;

    for (int i = 0; i<n-1; i++) {
        rest=cursor;
        cursor= cursor->next;
    }
    temp=malloc(sizeof(DoubleNode));
    temp->data=d;
    temp->next=cursor;
    rest->next= temp;

    return head;
}

int main(int argc, const char * argv[]) {
    DoubleNode *head;
    head=malloc(sizeof(DoubleNode));

    for (double i=0.0; i <11.0; i++) {
        head=insertLast(head, i);
    }
    printList(head);
    reverseDoubleList(head);
    printList(head);
    reverseDoubleListCon(head);
    printList(head);

    return 0;
}

Upvotes: 2

Views: 488

Answers (3)

user3813674
user3813674

Reputation: 2683

This'll work for your reverse function.

void reverseDoubleList(DoubleNode **head) {
    DoubleNode *prev, *curr, *next;
    curr=*head;
    prev=NULL;
    while (curr!=NULL) {
        next=curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    *head=prev;
}

Then in main, call it with reverseDoubleList(&head). Print the list after that to see effect.

Upvotes: 3

Rabbid76
Rabbid76

Reputation: 210890

To create reverse copy of a single linked list copy one element after another and add it at front of reverse list:

DoubleNode* reverseDoubleListCon( DoubleNode* head) {
    DoubleNode *reverse = NULL;
    while ( head )
    {
        DoubleNode *temp = malloc(sizeof(DoubleNode));
        temp->data = head->data; // copy data to new element
        head = head->next;       // step one forward
        temp->next = reverse;    // successor of new element is first element of reverse list
        reverse = temp;          // head of reverse list is new element
    }
    return reverse;
}

DoubleNode* reverseHead = reverseDoubleListCon( head );

To reverse a single linked list remove first element from list and add it at front of reverse list:

 DoubleNode* reverseDoubleList( DoubleNode* head) {
    DoubleNode *reverse = NULL;
    while ( head )
    {
        DoubleNode *temp = head;  // next element is head of list
        head = head->next;        // step one forward
        temp->next = reverse;     // successor of next element is first element of reverse list
        reverse = temp;           // head of reverse list is next element
    }
    return reverse;
}

head  = reverseDoubleList( head );

Upvotes: 2

lost_in_the_source
lost_in_the_source

Reputation: 11237

void rev(node **head)
{
    node *prev = NULL;
    node *next = *head->next;
    node *cur;

    for (cur = *head; cur; cur = next) {
        next = cur->next;
        prev = cur;
    }
    *head = prev
}

This is the linked list at first: enter image description here


Step 2: Node B is made to point to A, and A->next is made NULL enter image description here
Step 3: The process continues... enter image description here
Step 4: The linked list is now reversed. enter image description here

Upvotes: 3

Related Questions