链表头插法

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值