首页 > 解决方案 > 链表返回单链表 C 中的根节点

问题描述

我正在编写一个程序来删除大于 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;
}

标签: cstructlinked-listnodessingly-linked-list

解决方案


一种简单的实现方式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;
}

推荐阅读