链表在C语言中的实现
我不熟悉链表,我正在尝试用C语言实现链表。 以下是我的代码:-链表在C语言中的实现,c,linked-list,C,Linked List,我不熟悉链表,我正在尝试用C语言实现链表。 以下是我的代码:- #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert (struct node *head, int data); void print (struct node *head); int main() { struct node *head ;
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void insert (struct node *head, int data);
void print (struct node *head);
int main()
{
struct node *head ;
head= NULL;
printf("new\n");
insert(head,5);
printf("%d\n",head);
insert(head,4);
insert(head,6);
print(head);
print(head);
print(head);
}
void insert(struct node *head,int data){
printf("%d\n",head);
if(head == NULL){
head =malloc(sizeof(struct node));
head->next = NULL;
head->data = data;
}
else {
printf("entered else\n");
struct node *tmp = head;
if(tmp->next!=NULL){
tmp = tmp->next;
}
tmp->next = malloc(sizeof(struct node));
tmp->next->next = NULL;
tmp->next->data = data;
}
}
void print (struct node *head) {
printf("%d\n",head);
struct node *tmp = head;
if (head == NULL) {
printf("entered null\n");
return;
}
while (tmp != NULL) {
if (tmp->next == NULL) {
printf("%0d", tmp->data);
} else {
printf("%0d -> ", tmp->data);
}
tmp = tmp->next;
}
printf("\n");
}
头部始终为空,并且不会更新空值。它不会进入insert中的else循环。
谁能帮我修一下这个吗。指出我所犯的错误。
谢谢您的代码中可能还有其他错误,但一个大问题是您试图在
插入
中设置头节点,但这只会影响传入的指针的本地副本,因此在调用方无效:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
您还需要确保当您传递的节点不是NULL
时,您实际上将新节点附加到头部。
您可以通过向指针传递指针或返回集合指针来解决第一个问题。比如说,
void insert(struct node **head, int data) {
if(*head == NULL) {
// create the head node
...
*head = malloc(sizeof(struct node));
....
else {
// create a new node and attach it to the head
struct node* tmp = malloc(sizeof(struct node));
....
(*head)->next = tmp;
}
}
然后,在main
中,您需要传递一个指向头部指针的指针,即使用操作员和的地址:
struct node *head = NULL;
insert(&head, 5);
注意问题的一部分是函数试图做的太多。它被称为insert
,但如果传入的指针为NULL
,它会尝试创建一个新节点。最好将这些责任分开:
// allocate a node and set its data field
struct node* create_node(int data)
{
struct node* n = malloc(sizeof(struct node));
n->next = NULL;
n->data = data;
return n;
}
// create a node and add to end node.
// return the new end node.
// end has to point to a valid node object.
struct node* append_node(struct node* tail, int node_data)
{
struct node* new_tail = create_node(node_data);
tail->next = new_tail;
return new_tail;
}
您的代码中可能还有其他错误,但一个大问题是您试图在insert
中设置头节点,但这只会影响传入的指针的本地副本,因此在调用方无效:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
您还需要确保当您传递的节点不是NULL
时,您实际上将新节点附加到头部。
您可以通过向指针传递指针或返回集合指针来解决第一个问题。比如说,
void insert(struct node **head, int data) {
if(*head == NULL) {
// create the head node
...
*head = malloc(sizeof(struct node));
....
else {
// create a new node and attach it to the head
struct node* tmp = malloc(sizeof(struct node));
....
(*head)->next = tmp;
}
}
然后,在main
中,您需要传递一个指向头部指针的指针,即使用操作员和的地址:
struct node *head = NULL;
insert(&head, 5);
注意问题的一部分是函数试图做的太多。它被称为insert
,但如果传入的指针为NULL
,它会尝试创建一个新节点。最好将这些责任分开:
// allocate a node and set its data field
struct node* create_node(int data)
{
struct node* n = malloc(sizeof(struct node));
n->next = NULL;
n->data = data;
return n;
}
// create a node and add to end node.
// return the new end node.
// end has to point to a valid node object.
struct node* append_node(struct node* tail, int node_data)
{
struct node* new_tail = create_node(node_data);
tail->next = new_tail;
return new_tail;
}
您的代码中可能还有其他错误,但一个大问题是您试图在insert
中设置头节点,但这只会影响传入的指针的本地副本,因此在调用方无效:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
您还需要确保当您传递的节点不是NULL
时,您实际上将新节点附加到头部。
您可以通过向指针传递指针或返回集合指针来解决第一个问题。比如说,
void insert(struct node **head, int data) {
if(*head == NULL) {
// create the head node
...
*head = malloc(sizeof(struct node));
....
else {
// create a new node and attach it to the head
struct node* tmp = malloc(sizeof(struct node));
....
(*head)->next = tmp;
}
}
然后,在main
中,您需要传递一个指向头部指针的指针,即使用操作员和的地址:
struct node *head = NULL;
insert(&head, 5);
注意问题的一部分是函数试图做的太多。它被称为insert
,但如果传入的指针为NULL
,它会尝试创建一个新节点。最好将这些责任分开:
// allocate a node and set its data field
struct node* create_node(int data)
{
struct node* n = malloc(sizeof(struct node));
n->next = NULL;
n->data = data;
return n;
}
// create a node and add to end node.
// return the new end node.
// end has to point to a valid node object.
struct node* append_node(struct node* tail, int node_data)
{
struct node* new_tail = create_node(node_data);
tail->next = new_tail;
return new_tail;
}
您的代码中可能还有其他错误,但一个大问题是您试图在insert
中设置头节点,但这只会影响传入的指针的本地副本,因此在调用方无效:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
您还需要确保当您传递的节点不是NULL
时,您实际上将新节点附加到头部。
您可以通过向指针传递指针或返回集合指针来解决第一个问题。比如说,
void insert(struct node **head, int data) {
if(*head == NULL) {
// create the head node
...
*head = malloc(sizeof(struct node));
....
else {
// create a new node and attach it to the head
struct node* tmp = malloc(sizeof(struct node));
....
(*head)->next = tmp;
}
}
然后,在main
中,您需要传递一个指向头部指针的指针,即使用操作员和的地址:
struct node *head = NULL;
insert(&head, 5);
注意问题的一部分是函数试图做的太多。它被称为insert
,但如果传入的指针为NULL
,它会尝试创建一个新节点。最好将这些责任分开:
// allocate a node and set its data field
struct node* create_node(int data)
{
struct node* n = malloc(sizeof(struct node));
n->next = NULL;
n->data = data;
return n;
}
// create a node and add to end node.
// return the new end node.
// end has to point to a valid node object.
struct node* append_node(struct node* tail, int node_data)
{
struct node* new_tail = create_node(node_data);
tail->next = new_tail;
return new_tail;
}
void insert(结构节点和头,int数据){//传递引用而不是指针
^
您传递了一个指针,但这只允许您更改它所指向的数据。要更改指针本身,您必须传递一个引用。我不确定我的C是否有点生锈,但我只是为您指出了正确的方向
问候,
Andrévoid insert(结构节点和头,int数据){//传递引用而不是指针
^
您传递了一个指针,但这只允许您更改它所指向的数据。要更改指针本身,您必须传递一个引用。我不确定我的C是否有点生锈,但我只是为您指出了正确的方向
问候,
Andrévoid insert(结构节点和头,int数据){//传递引用而不是指针
^
您传递了一个指针,但这只允许您更改它所指向的数据。要更改指针本身,您必须传递一个引用。我不确定我的C是否有点生锈,但我只是为您指出了正确的方向
问候,
Andrévoid insert(结构节点和头,int数据){//传递引用而不是指针
^
您传递了一个指针,但这只允许您更改它所指向的数据。要更改指针本身,您必须传递一个引用。我不确定我的C是否有点生锈,但我只是为您指出了正确的方向
问候,
André我已修复了您的插入功能:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
#define CREATENODE malloc(sizeof(struct node))
void insert (struct node *head, int data);
void print (struct node *head);
int main()
{
struct node *head ;
head = CREATENODE;
printf("new\n");
insert(head,5);
insert(head,4);
insert(head,6);
print(head);
print(head);
print(head);
}
void insert(struct node *head,int data){
struct node *temp, *nn;
for(temp=head;temp->next!=NULL;temp=temp->next);
nn=CREATENODE;
nn->data = data;
nn->next =temp->next;
temp->next = nn;
}
void print (struct node *head) {
struct node *tmp = head->next;
if (head == NULL) {
printf("entered null\n");
return;
}
while (tmp != NULL) {
if (tmp->next == NULL) {
printf("%0d", tmp->data);
} else {
printf("%0d -> ", tmp->data);
}
tmp = tmp->next;
}
printf("\n");
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
#定义CREATENODE malloc(sizeof(struct node))
void insert(结构节点*head,int数据);
作废打印(结构节点*头);
int main()
{
结构节点*头部;
head=CREATENODE;
printf(“新的”);
插入(头,5);
插入(头,4);
插入(头,6);
打印头;
打印头;
打印头;
}
空插入(结构节点*头,整数数据){
结构节点*temp,*nn;
对于(温度=头部;温度->下一步!=NU