c-链表反向函数导致无限打印循环



我正在编写一个C代码来反转一个链接列表。我遇到了一个问题。如果我不将next指针设为NULL,我的反向函数可以正常工作,但如果我将其设为null,则链表总是在while循环中继续打印。

以下是正确的程序,运行良好。但如果我制作*next = NULL,显示功能将在while循环中保持打印。

#include <stdio.h>
#include <stdlib.h>
struct node {
    int data;
    struct node *next;
} *head;
/*************************************************************/
/*                                                           */
/*  create - Function to create Nodes and add them at last   */
/*                                                           */
/*************************************************************/
int create(int data)
{
    struct node *temp,*ptr = NULL;
    //int data = 0;
    ptr = head;
    //Printf(" Enter the Data for Node : ");
    //scanf(" %d ", data);  
    temp = (struct node *)malloc(sizeof(struct node));
    if (ptr == NULL) {
        // this is the first node
        temp->next = NULL;
        temp->data = data;
        head = temp;
    } else {
        // this is not the first node
        while (ptr != NULL) {
            if (ptr->next == NULL) {
                temp->next = NULL;
                temp->data = data;
                ptr->next = temp;
                break;
            }
            ptr = ptr->next;
        }
    }
    return 0;
}
/*************************************************************/
/*                                                           */
/*  create_in_front - Function to add Node in Front          */
/*                                                           */
/*************************************************************/
int create_in_front(int data)
{
    struct node *temp,*ptr = NULL;
    ptr = head;
    temp = (struct node *)malloc(sizeof(struct node));
    if (ptr == NULL) {
        // this is the first node
        temp->next = NULL;
        temp->data = data;
        head = temp;
    } else {
        // this is not the first node
        temp->next = ptr->next;
        temp->data = data;
        head = temp;
    }
    return 0;
}
/*************************************************************/
/*                                                           */
/*  create_in_between - Function to add Node in between nodes*/
/*                                                           */
/*************************************************************/
int create_in_between(int data,int pos)
{
    struct node *temp, *ptr = NULL;
    int i = 0;
    ptr = head;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = data;
    for (i = 0; i < pos; i++) {
        if (i == pos-1) {
            temp->next = ptr->next;
            ptr->next = temp;
        }
        ptr = ptr->next;
    }
    return 0;
}

/*************************************************************/
/*                                                           */
/*  delete_in_between - Function to add Node in between nodes*/
/*                                                           */
/*************************************************************/
delete_in_between(int pos)
{
    struct node *ptr, *prev = NULL;
    ptr = head;
    int i = 0;
    for (i = 0; i < pos; i++) {
        if (i == pos-1) {
            prev = ptr->next;
            free(ptr);
            break;
        }
        prev = ptr;
        ptr = ptr->next;
    }
    return 0;
}
/*************************************************************/
/*                                                           */
/*  reverse - Function to reverse link list                  */
/*                                                           */
/*************************************************************/
int reverse()
{
    struct node *prev = NULL;
    struct node *curr = head;
    struct node *next = NULL;
    curr = head;
    while (curr != NULL) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    head = prev;
    return 0;
}
/*************************************************************/
/*                                                           */
/*  display - Function to diplay link list                   */
/*                                                           */
/*************************************************************/
// Function to display Link List
int display()
{
    struct node *temp = head;
    while (temp != NULL) {
        printf("%d->",temp->data);
        temp = temp->next;
    }
    return 0;
}
int main()
{
    create(10);
    create(20);
    create(30);
    create(40);
    create(50);
    create_in_front(34);
    create_in_between(55,2);
    //delete_in_between(4);
    reverse();
    display();
    return 0;
}

让我知道这背后的逻辑。

函数create_in_front()是伪造的:temp->next = ptr->next;应更改为temp->next = ptr;

CCD_ 7不处理CCD_ 8的情况。

delete_in_between()完全功能失调:节点是空闲的,但它的前一个节点仍然指向它

reverse()在我看来是正确的,它可以这样简化:

int reverse() {
    struct node *prev = NULL;
    struct node *curr = head;
    while (curr != NULL) {
        struct node *next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    head = prev;
    return 0;
}

这个问题似乎与您修改reverse()函数无关,可能是其他函数中错误的副作用。

您的reverse()函数似乎是正确的,但代码的其余部分有些过于复杂。试试这样的东西:

void create(int data) {
    struct node *temp = malloc(sizeof(struct node));
    if (temp != NULL) {
        temp->next = NULL;
        temp->data = data;
        if (head == NULL) {   // this is the first node
            head = temp;
        } else {
            // this is not the first node
            struct node *last = head;
            while (last->next)
                last = last->next;
            last->next = temp;
        }
    }
}
void create_in_front(int data) {
    struct node *temp = malloc(sizeof(struct node));
    if (temp != NULL) {
        temp->next = head;
        temp->data = data;
        head = temp;
    }
}

最新更新