#include<iostream>
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
LinkList HeadInsert(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
LNode *s;
int x;
cin>>x;
while(x!=-1)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
cin>>x;
}
return L;
}
void ListVisit(LinkList L)
{
LNode *p=L->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main()
{
LinkList L;
HeadInsert(L);//头插法
ListVisit(L);//输出
return 0;
}
头插法的单链表
于 2024-04-07 16:15:07 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)