#include<bits/stdc++.h>
using namespace std;
//链表的基本操作步骤: 头插法!!!!!!!!!!!!!!!!!!!!!
//(带 *号的备注是我为了建立简单链表的无脑操作)
typedef struct Node//结构体(结点)建立完成
{
int date;
struct Node *next;
}SequenList;
//int hanshu()是返回一个整数,类比得:下面是返回一个结构体指针。
SequenList *creat_list(SequenList* &head)//创建链表
{
head = (SequenList*)malloc(sizeof(SequenList));//为 head指针指向 malloc所分配的空间
SequenList *node = NULL;
head->next = NULL;
node = head->next;//node指针 等于 head所指的结点的指针域的数据
int count = 0;//*count代表我想建立结点的个数
printf("输入你想建立结点的个数:");
scanf("%d", &count);
for(int i=1; i<=count; i++)
{
node = (SequenList*)malloc(sizeof(SequenList));//为 node指针指向 malloc所分配的空间
node->date = i;//*简单地填写数据
node->next = head->next;//node的结点指向 head所指的结点的下一个结点 (node指针指向结点的指针域 等于 head指针指向结点的指针域)
head->next = node; //head所指的结点更换为 node的结点
//再进入循环, node重新定义,继续发挥作用,不断的插头
}
//return head;//为什么这里return没用如果去掉 &传递: SequenList* &head????????????????????????????
}
void shuchu(SequenList* head)
{
SequenList* t = head;
while(t->next != NULL)
{
t = t->next;
printf("%d\n", t->date);
}
}
int main()
{
SequenList* head = NULL;
creat_list(head);
shuchu(head);
return 0;
}
链表头插法
最新推荐文章于 2024-06-25 07:11:52 发布