c - 在单链表的开头添加一个节点 (C)
问题描述
该代码可以正常工作,但我似乎不知道为什么没有将新节点插入到列表的开头。它可能与else
第一个函数 ( ) 中的语句有关,insertNode
但我不确定,这是怎么回事?
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
void insertNode(struct node *head, int x) {
//Create node to be added and add the input integer to it
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = x;
//Check if there are any existing nodes in the list, if not, the let the head be equal to the new temp pointer
if (head == NULL) {
head = temp;
} else {
//If not, then we need to add the node to the beginning
temp->link = head;
head = temp;
printf("Node was added successfully!\n");
}
}
int findsize(struct node *head) {
//Finds the size of the list
struct node *temp = head;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->link;
}
return count;
}
void printData(struct node *head) {
//Prints the elements of the list
struct node *temp = head;
while (temp != NULL) {
printf("Element: %d\n", temp->data);
temp = temp->link;
}
}
void main() {
//Created a node and allocated memory
struct node *head;
head = (struct node *)malloc(sizeof(struct node));
//Added data to the node and created another one linked to it
head->data = 15;
head->link = (struct node *)malloc(sizeof(struct node));
head->link->data = 30;
head->link->link = NULL;
//Used the above function to add a new node at the beginning of the list
insertNode(head, 5);
//Print the size of the list
printf("The size of the list you gave is: %d\n", findsize(head));
//Print the elements of the list
printData(head);
}
解决方案
当你在列表的开头插入一个节点时,你实际上改变了列表的开头,所以这个新的初始节点必须返回给调用者。insertNode()
必须更改for 的原型以返回列表头或获取指向列表头的指针。
这是第一种方法的修改版本:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
struct node *insertNode(struct node *head, int x) {
//Create node to be added and add the input integer to it
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if (temp != NULL) {
temp->data = x;
temp->link = head;
}
return temp;
}
int findsize(struct node *head) {
//Find the size of the list
struct node *temp = head;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->link;
}
return count;
}
void printData(struct node *head) {
//Prints the elements of the list
struct node *temp = head;
while (temp != NULL) {
printf("Element: %d\n", temp->data);
temp = temp->link;
}
}
void main() {
//Created a node and allocated memory
struct node *head = NULL;
//Insert 3 nodes with values 30, 15 and 5
head = insertNode(head, 30);
head = insertNode(head, 15);
head = insertNode(head, 5);
//Print the size of the list
printf("The size of the list you gave is: %d\n", findsize(head));
//Print the elements of the list
printData(head);
//Should free the nodes
return 0;
}
推荐阅读
- c# - HttpResponseMessage 到字符串
- asp.net - 有什么方法可以在运行时提供连接字符串?
- domain-driven-design - Axon 框架:在总负载上发送命令
- c# - XAML 设计未在 Visual Studio 中加载
- javascript - jquery .each 函数在 iframe 中不起作用
- javascript - 从数组中删除特定对象 - Node.js
- c# - 同一解决方案中两个 ASP.NET Core 项目之间的通信
- javascript - 如何修复具有多重嵌套的下拉菜单?香草JS
- javascript - 在句子中找到圆括号并筛选它们
- javascript - 如果其他条件在 Javascript 模板文字中不起作用