数据结构之排序单链表(类模板实现)(C++)

1.实现了链表的顺序插入功能(可升序或降序),需实现元素结点的 int (* compare )(Elem *e1, Elem *e2);  比较接口;

//文件名:"SortedList.h"
#pragma once
#ifndef SORTEDLIST
#define SORTEDLIST
#include <string>
using namespace std;

/*
.	排序链表 类模板实现
*/
//链表结点
template <typename ElemType>
struct SLNode
{
	ElemType * e;	//数据域
	SLNode<ElemType> * next;		//链域
};

template <typename ElemType>
class SortedList
{
private:
	SLNode<ElemType> * head;					//头结点
	int length;									//链长
	int (*compare)(ElemType *, ElemType *);		//比较 函数指针
	int sortedType;								//排序类型

public:
	static const int _ASC = 1;				//排序类型:升序标识
	static const int _DESC = 0;				//排序类型:降序标识

	SortedList();						//无参构造
	void Init(int(*compare)(ElemType *, ElemType *), int sortedType);	//初始化

	int Length();						//获取链表长度
	void Insert(ElemType *e);			//顺序插入一个元素
	ElemType * Get(int index);			//获取第 index 个元素
	ElemType * Delete(int index);		//删除第 index 个元素
	void Display();						//显示链表
};

template <typename ElemType>
SortedList<ElemType>::SortedList()
{
	/*
	.	有参构造
	*/
	//初始化头结点
	this->head = new SLNode<ElemType>;
	this->head->e = NULL;
	this->head->next = NULL;
}

template <typename ElemType>
void SortedList<ElemType>::Init(int(*compare)(ElemType *, ElemType *), int sortedType)
{
	/*
	.初始化:
	.	参数:
	.		1.int(*compare)(ElemType *, ElemType *): 比较函数()
	.			其返回值:-1|小于 0|等于 1|大于
	.		2.int sortedType: 排序类型
	.			取值:_ASC 或 _DESC
	*/
	//比较函数 初始化
	this->compare = compare;
	//排序类型
	this->sortedType = sortedType;
}

template <typename ElemType>
int SortedList<ElemType>::Length()
{
	/*
	.	获取链表长度
	*/
	return this->length;
}

template <typename ElemType>
void SortedList<ElemType>::Insert(ElemType *e)
{
	/*
	.	顺序插入一个元素
	*/
	//初始化  指针 p 为第一个结点,q 指向其前驱结点
	SLNode<ElemType> *p = this->head->next, *q = this->head;
	SLNode<ElemType> *newNode = NULL;
	//按 升序或降序 顺序寻找符合 compare 比较条件的 结点位置
	while (p != NULL && ((this->sortedType == this->_ASC && compare(e, p->e) > 0)
		|| (this->sortedType == this->_DESC && compare(e, p->e) < 0)))
	{
		q = p;
		p = p->next;
	}
	//在 p 结点前 (q 结点后)插入
	newNode = new SLNode<ElemType>;
	newNode->e = e;
	newNode->next = p;
	q->next = newNode;
	//自增 链长
	this->length++;
}

template <typename ElemType>
ElemType * SortedList<ElemType>::Get(int index)
{
	/*
	.	获取第 index 个元素
	*/
	//链表越界检查
	if (index < 1 || index > this->length)
	{
		cout << "索引越界!" << endl;
		return NULL;
	}
	//初始化链表索引 
	int i = 1;
	//初始化 p 指向链表第一个结点
	SLNode<ElemType> *p = this->head->next;
	//第一个结点为空,返回 空
	if (p == NULL)
		return NULL;
	//遍历链表,查找第 index 个元素结点
	while (i < index)
	{
		p = p->next;
		i++;
	}
	return p->e;
}

template <typename ElemType>
ElemType * SortedList<ElemType>::Delete(int index)
{
	/*
	.	删除第 index 个元素
	*/
	//链表越界检查
	if (index < 1 || index > this->length)
	{
		cout << "索引越界!" << endl;
		return NULL;
	}
	//初始化链表索引 
	int i = 1;
	//初始化 p 指向链表第一个结点,p 的前驱结点 q
	SLNode<ElemType> *p = this->head->next, *q = this->head;
	ElemType *e = NULL;
	//第一个结点为空,返回 空
	if (p == NULL)
		return NULL;
	//遍历链表,查找第 index 个元素结点
	while (i < index)
	{
		q = p;
		p = p->next;
		i++;
	}
	e = p->e;
	//删除 p 指针处结点
	q->next = p->next;
	delete p;
	//自减 链长
	this->length--;

	return e;
}

template <typename ElemType>
void SortedList<ElemType>::Display()
{
	/*
	.	显示链表
	*/
	SLNode<ElemType> *p = this->head->next;
	cout << "The sorted list is : ";
	if (p == NULL)
		cout << "NULL";
	while (p != NULL)
	{
		cout << p->e << " ";
		p = p->next;
	}
	cout << endl;
}

#endif // !SORTEDLIST
//文件名:"SortedList_Test.cpp"
#include "stdafx.h"
#include <iostream>
#include "SortedList.h"
using namespace std;

struct Node
{
	int a;
	int b;
	friend ostream & operator <<(ostream& out, Node *p)
	{
		/*
		.	友元函数重载输出操作符,实现对象输出
		*/
		out << p->a;
		return out;
	}
};

int compare(Node * e1, Node *e2)
{
	if (e1->a > e2->a)
		return 1;
	else if (e1->a == e2->a)
		return 0;
	else
		return -1;
}

int main()
{
	Node *n1 = new Node{ 1, 1 };
	Node *n2 = new Node{ 2, 2 };
	Node *n3 = new Node{ 3, 3 };

	SortedList<Node> *list = new SortedList<Node>();
	list->Init(compare, list->_ASC);	//初始化为升序,实现 compare 接口
	//乱序插入三个元素
	list->Insert(n2);
	list->Insert(n3);
	list->Insert(n1);
	//测试 三个元素是否升序排列
	list->Display();
	//测试 链表索引越界
	list->Get(0);
	list->Get(4);
	//测试 获取第一个元素,并删除
	cout << "第一个元素:" << list->Get(1) << endl;
	list->Delete(1);
	list->Display();
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39469127/article/details/80573850