[英]Linked list returning root node in singly-linked list C
我正在編寫一個程序來刪除大於 x 的節點。 我想刪除后返回鏈表的根節點。 我將新的根節點保存在變量“root”中。 但不幸的是,當我在主 function 中獲得 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 和 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;
};
聲明 static 變量root
沒有太大意義
static Node *root;
然后又是一個可變head
。
// Create list: 7->3->4->8->5->1
Node* head = getNode(7);
您的 function deleteGreaterNodes
太復雜了,與任何復雜的 function 一樣,它在此代碼段中包含一個錯誤
// If required value node was not present
// in linked list
if (temp == NULL)
return 0;
因為它返回一個 null 指針而不是返回指針head_ref
。
這是根據 C 語言調整的更新程序,其中包含修改后的 function 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;
}
程序 output 是
Original List: 7 3 4 8 5 1
Modified List: 3 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.