不啰嗦,以下是我的代码,比较适合新手吧。
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;
//带头结点的头插法
int main()
{
int x;
int n=0;
printf("请输入你要插入的结点个数:");
scanf("%d",&x);
struct Node* head;
head=(Node*)malloc(sizeof(Node));
head->next=NULL;
while(n!=x)
{
struct Node* t;
int a;
t=(Node*)malloc(sizeof(Node));
printf("请输入你要插入的结点值:");
scanf("%d",&a);
t->data=a;
t->next=head->next;
head->next=t;
n++;
}
//遍历
while(head->next!=NULL)
{
printf("%d",head->next->data);
head=head->next;
}
}