繁体   English   中英

链表反向功能导致无限打印循环

[英]Linked list reverse function leads to infinite printing loop

我正在编写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;

create_in_between()不处理pos==0的情况。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM