【问题标题】:Linked list returning root node in singly-linked list C链表返回单链表 C 中的根节点
【发布时间】:2021-03-26 08:32:20
【问题描述】:

我正在编写一个程序来删除大于 x 的节点。我想删除后返回链表的根节点。我将新的根节点保存在变量“root”中。但不幸的是,当我在 main 函数中获取 root 时,该值似乎为 0。有人可以帮我找出问题吗?**

// structure of a node
struct Node {
    int data;
    Node* next;
};

// function to get a new node
Node* getNode(int data)
{
  
    Node* newNode = (Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// function to delete all the nodes from the list
// that are greater than the specified value x
Node *deleteGreaterNodes(Node* head_ref, int x)
{
    Node *temp = head_ref, *prev;
    static Node *root;

    // If head node itself holds the value greater than 'x'
    if (temp != NULL && temp->data > x) {
        head_ref = temp->next; // Changed head
        free(temp); // free old head
        temp = head_ref; // Change temp
    }
    root = temp;
    // Delete occurrences other than head
    while (temp != NULL) {

        // Search for the node to be deleted,
        // keep track of the previous node as we
        // need to change 'prev->next'
        while (temp != NULL && temp->data <= x) {
            prev = temp;
            temp = temp->next;
        }

        // If required value node was not present
        // in linked list
        if (temp == NULL)
            return 0;

        // Unlink the node from linked list
        prev->next = temp->next;

        free (temp); // Free memory

        // Update Temp for next iteration of
        // outer loop
        temp = prev->next;
    }
    return head_ref;
}

// function to a print a linked list
void printList(Node* head)
{
    while (head) {
       
        printf("%d ",head->data);
        head = head->next;
    }
}

// Driver program to test above
int main()
{
    static Node *root;
    // Create list: 7->3->4->8->5->1
    Node* head = getNode(7);
    head->next = getNode(3);
    head->next->next = getNode(4);
    head->next->next->next = getNode(8);
    head->next->next->next->next = getNode(5);
    head->next->next->next->next->next = getNode(1);

    int x = 3;

   
     printf("Original List: "); 
    printList(head);

   root = deleteGreaterNodes(head, x);
     
     printf("\nModified List: "); 
    printList(root);

    return 0;
}

【问题讨论】:

  • 您不能在 C 程序中使用 cout &lt;&lt; "Original List: ";。不要使用 C++ 编译器来编译 C 程序。由于其余代码似乎确实是 C 而不是 C++,因此您可以简单地编辑该行(后面有一个等效的 printf())。但请小心。
  • ist 中的最后一个节点的值为 1,它不大于 3。在最里面的循环中,你前进通过你想要保留的所有节点,如果你到达终点,你return 0。所以返回 0 的代码就在你的代码中。
  • 我收回了——你正在使用 C++ 的另一个特性,而不仅仅是一个流浪的 cout。你定义struct Node;在 C++ 中,它还定义了一个类型 Node,但它没有在 C 中。在定义结构类型之前,您需要 typedef struct Node Node; 或等效项。您不应该在使用static Node *root; 的任何地方使用它。在main() 中,它应该是一个简单的本地(自动)变量。在deleteGreaterNodes(),应该完全删除。
  • 谢谢@M Oehm。就是这样!我最后返回 0 。现在我将其更改为返回我的头节点。

标签: c struct linked-list nodes singly-linked-list


【解决方案1】:

实现deleteGreaterNodes 的一种简单方法是使用额外的间接级别来访问Node * 链接。

// function to delete all the nodes from the list
// that are greater than the specified value x
Node *deleteGreaterNodes(Node* head_ref, int x)
{
    Node **link = &head_ref; // Point to the pointer to the first node.
    Node *cur;

    while ((cur = *link) != NULL) {
        if (cur->data > x) {
            // The current node needs to be deleted from the list.
            // Change the link pointing to the current node to point to the next node.
            *link = cur->next;
            // Delete the current node.
            free(cur);
        } else {
            // The current node is to be kept on the list.
            // Get a pointer to the link to the next node.
            link = &cur->next;
        }
    }
    return head_ref;
}
【解决方案2】:

首先,不要在一个翻译单元中混用两种语言 C 和 C++。

编写 C 或 C++ 程序。

例如 C 中的这个声明

// structure of a node
struct Node {
    int data;
    Node* next;
};

不正确,你必须写

// structure of a node
struct Node {
    int data;
    struct Node* next;
};

声明静态变量root没有太大意义

static Node *root;

然后是另一个变量head

// Create list: 7->3->4->8->5->1
Node* head = getNode(7);

你的函数deleteGreaterNodes太复杂了,和任何复杂的函数一样,它在这段代码sn-p中包含一个错误

    // If required value node was not present
    // in linked list
    if (temp == NULL)
        return 0;

因为它返回一个空指针而不是返回指针head_ref

这是根据 C 语言调整的更新程序,其中包含修改后的函数deleteGreaterNodes

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

// structure of a node
struct Node {
    int data;
    struct Node* next;
};

// function to get a new node
struct Node* getNode(int data)
{
  
    struct Node* newNode = ( struct Node * )malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// function to delete all the nodes from the list
// that are greater than the specified value x

struct Node *deleteGreaterNodes( struct Node* head_ref, int x )
{
    while ( head_ref && x < head_ref->data )
    {
        struct Node *tmp = head_ref;
        head_ref = head_ref->next;
        free( tmp );
    }
    
    if ( head_ref )
    {
        for ( struct Node *current = head_ref; current->next != NULL; )
        {
            if ( x < current->next->data )
            {
                struct Node *tmp = current->next;
                current->next = current->next->next;
                free( tmp );
            }
            else
            {
                current = current->next;
            }
        }
    }
    
    return head_ref;
}

// function to a print a linked list
void printList(struct Node* head)
{
    while (head) {
       
        printf("%d ",head->data);
        head = head->next;
    }
}

// Driver program to test above
int main()
{
    struct Node* head = getNode(7);
    head->next = getNode(3);
    head->next->next = getNode(4);
    head->next->next->next = getNode(8);
    head->next->next->next->next = getNode(5);
    head->next->next->next->next->next = getNode(1);

    int x = 3;

    printf( "Original List: " );
    // printf("Original List: "); 
    printList(head);

   head = deleteGreaterNodes(head, x);

    
     printf("\nModified List: "); 
    printList( head );

    return 0;
}

程序输出是

Original List: 7 3 4 8 5 1 
Modified List: 3 1

【讨论】:

  • 感谢您为我省去编写等效答案的麻烦。除了我在定义结构之前添加了typedef struct Node Node;(因此也不需要在其他地方添加struct),您得到的诊断与我提出的相同。
  • @JonathanLeffler 添加 typedef 更有效地更新程序。:)
猜你喜欢
  • 2014-10-16
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多