C - 如何实现链表


#include <stdio.h>    
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)(*head_ref)->prev = new_node;
(*head_ref) = new_node;}
void append(struct Node** head_ref, int new_data){
/* 1. allocate node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->data = new_data;
/* 3. This new node is going to be the last node, so
make next of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new
node as head */
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return;}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
/* 7. Make last node as previous of new node */
new_node->prev = last;
return;}
void insertAfter(struct Node* prev_node, int new_data){
/*1. check if the given prev_node is NULL */
if (prev_node == NULL) {
printf("the given previous node cannot be NULL");
return;}
/* 2. allocate new node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
new_node->next->prev = new_node;}
void printList(struct Node* node){
struct Node* last;
printf("nTraversal in forward direction n");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next;}
printf("nTraversal in reverse direction n");
while (last != NULL) {
printf(" %d ", last->data);
last = last->prev;
}}

void sortedInsert(struct Node** head, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
struct Node* temp;
if ((*head) == NULL || (new_node->data) > (*head)->prev->data) {
append(head, new_data);
return;
}
if ((new_node->data) < ((*head)->data)) {
push(head, new_data);
return;
}
temp = (*head)->next;
while ((temp->data) < (new_node->data)) {
temp = temp->next;
}
insertAfter(head, new_data);
}
int main() {
struct Node* head = NULL;
sortedInsert(&head, 0);
sortedInsert(&head, 9);
sortedInsert(&head, 4);
sortedInsert(&head, 3);
sortedInsert(&head, 34);
sortedInsert(&head, 15);
printf("n Created Linked list is: ");
printList(head);
return 0;}

我正在尝试编写一个 C 程序,其中必须以有序的方式(从小到高(插入数据 当我运行时,由于注释而出现错误: 预期的"结构节点*",但参数的类型为"结构节点**" 我该如何解决这个问题,我已经查看了其他解决方案,例如: 警告 - 预期的"结构节点**"但参数类型为"结构节点**"是什么意思? 但这些都无法解决我的问题。 任何帮助不胜感激

insertAfter(head, new_data);

如何解决此问题

你忘了取消引用struct Node** head就像你在函数sortedInsert中的其他地方正确做的那样;要得到正确的参数类型,它会是insertAfter(*head, new_data)

但是插入逻辑仍然不太正确;这是一个更正的版本:

void sortedInsert(struct Node** head, int new_data) {
// new node is allocated in append(), push() or insertAfter()
struct Node* temp;
if ((*head) == NULL || new_data < (*head)->data) {
push(head, new_data);
return;
}
temp = *head;
while (temp->next && temp->next->data < new_data) {
temp = temp->next;
}
insertAfter(temp, new_data);
}

当你使用你声明的函数时,你应该将引用(地址(传递给头部而不是头部本身,因为这是你的代码所需要的。

例如,使用append(&head, 3)而不是append(head, 3)

相关内容

  • 没有找到相关文章

最新更新