如何在 C 中反转单链表?

How to reverse a single-linked list in C?

对于我的 class 我的任务是建立链表、输入数据和执行不同的功能。我的代码编译正常,但是还原和建设性还原功能不起作用。

如果我反转列表并打印它,我只会得到第一个节点。 我似乎在某处错过了重点。这是我的代码: (如果您发现任何其他功能有任何错误,请告诉我) 提前致谢!

#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;
}

这将适用于您的反向功能。

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;
}

然后在main中,用reverseDoubleList(&head)调用它。之后打印列表看看效果。

要创建单个链表的反向副本,一个接一个地复制元素并将其添加到反向列表的前面:

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 );

要反转单个链表,从列表中删除第一个元素并将其添加到反转列表的前面:

 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 );
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
}

首先是链表:


第二步:让节点B指向A,让A->next为NULL
第 3 步:该过程继续......
第 4 步:链表现在反转了。