typedef struct SL
{
int data;//输入的数据
struct SL* next;//指向下一个节点
}SLnode;
SLnode* creat_slist()
{
int x = 0;
SLnode* h, * r, * s;
h = (SLnode*)malloc(sizeof(SLnode));//生成头节点,头结点不存放数据,访问时r = h->next开始
r = h;
printf("请输入参数\n");
scanf("%d", &x);
while (x!=-1)
{
s = (SLnode *) malloc(sizeof(SLnode));
s->data = x;
r->next = s;//r h s 因为r已经指向h;
r = s;//将r赋为尾节点
scanf("%d", &x);
}
r->next = NULL;
return h;//返回头结点
}
void print_phead(SLnode* head)
{
SLnode* ret = head->next;//头结点下一个开始输出,因为头结点没数据,只是引用
while (ret)//ret!=NULL
{
printf("%d->", ret->data);
ret = ret->next;
}
printf("NULL\n");
}
int main()
{
SLnode* head = creat_slist();
//打印
print_phead(head);
return 0;
}
编写一个简单的单向链表
最新推荐文章于 2024-07-22 20:42:52 发布