【发布时间】:2019-12-23 14:31:40
【问题描述】:
我已经在下面的代码中实现了链表,但它没有打印出任何东西。在代码中我添加了 3 个节点。有人请告诉我我错在哪里。感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int value;
struct Node *next;
}node;
node* insert_head(node *head, int value){
node *temp;
temp=(struct Node*)malloc(sizeof(struct Node));
temp->value=value;
if (head==NULL)
{
head=temp;
}
else{
temp->next=head;
head=temp;
}
return temp;
}
void printlist(node *head){
node *p=head;
while (p->next!=NULL)
{
printf("%d", p->value);
p=p->next;
}
}
int main(){
node *head;
head=(struct Node *)malloc(sizeof(struct Node));
head->value=0;
head->next=NULL;
insert_head(head, 1);
insert_head(head, 2);
insert_head(head, 3);
printlist(head);
return 0;
}
【问题讨论】:
-
你想要什么输出:“0 1 2 3”还是“3 2 1 0”?
-
我的意思是“3 2 1 0”。函数名是插入到头部
标签: c data-structures struct linked-list singly-linked-list