创建一个有序链表

#include <iostream>
using namespace std;

struct Node//节点结构体
{
	int n;
	Node *next;
};

void Insert(Node *H, int n);//有序插入节点函数
void Show(Node *H);//输出链表

int main(int argc, char const *argv[])
{
	int n;
	Node *HeadPointer;//定义头指针
	Node *HeadNode = new Node;//开辟头节点

	HeadPointer = HeadNode;//头指针指向头节点
	HeadNode->next = NULL;//头节点的下一个位置为空

	for (int i = 0; i < 10; i++)
	{
		cout << "input " << i+1 << "th num" << endl;
		cin >> n;
		Insert(HeadNode, n);//依次插入10个节点
	}
	Show(HeadPointer);//输入构造的有序链表
	return 0;
}

void Insert(Node *H, int n)//传递头节点指针和将要插入的元素
{
	Node *pre = H;//pre为传递过来的头节点的指针,
	Node *cur = H->next;//cur表示当前位置,这一句很重要,不然插入到最后时链表无法终止

	Node *_new = new Node;
	_new->n = n;//建立新的节点

	while (cur && n > cur->n)//如果链表不为空,且输入的元素比当前元素大
	{
		pre = cur;
		cur = cur->next;//循环得到中断位置的指针
	}

	_new->next = cur;
	pre->next = _new;//将新节点插入中断位置
}

void Show(Node *H)
{
	Node *p = H->next;//由于头节点数据区没有具体元素,所有输出时跳过头结点
	while (p)
	{
		cout << p->n << " ";
		p = p->next;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值