也就是用头插法
#include<stdio.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode*next;
}LNode,*LinkList;
int main(){
LNode*head;
head=(LNode*)malloc(sizeof(LNode));
head->next=NULL;
int n;
scanf("%d",&n);
LNode*p,*r;
int x;
for(int i=0;i<n;i++){
p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&x);
p->data=x;
p->next=head->next;
head->next=p;
}
LNode*a=head->next;
while(a!=NULL){
printf("%d ",a->data);
a=a->next;
}
return 0;
}
该程序演示了如何使用C语言实现链表的头插法插入节点。首先创建一个空链表,然后读取用户输入的数据,每次插入一个新节点到链表头部,并更新链表头指针。最后遍历并打印链表中的所有元素。
反向输出一个链表&spm=1001.2101.3001.5002&articleId=129776512&d=1&t=3&u=9b7240e29546476b838524c42b4031d3)
339

被折叠的 条评论
为什么被折叠?



