用C++编写单链表类模板,实现链表的创建,遍历,链表结点的插入,删除,查找,并建立一个整数链表和一个字符串链表

编写单链表类模板,实现链表的创建,遍历,链表结点的插入,删除,查找,并建立一个整数链表和一个字符串链表。

IntList.h

#include<iostream>
using namespace std;
template<typename T>
class intList
{
protected:
	struct Node {
		struct Node* next;
		T data;
	};
	Node* pFirst;
public:
	intList();			//构造函数
	~intList();			//构析函数
			//向链表的第i个位置插入一个元素,插入成功返回true,失败返回false
	bool insert(int i, T elem);
	//删除链表的第i个元素,删除成功返回true,失败返回false
	bool remove(int i, T& elem);
	int find(T elem)const;		//查早elem的元素,返回该元素在链表中的位置
	int length() const;				//返回链表长度
	void printList();				//输出链表
};
template<typename T>
intList<T>::intList() {
	pFirst = new Node;
	pFirst->next = NULL;
	cout << "链表头创立成功\n";
}
template<typename T>
intList<T>::~intList() {
	Node* p, * q;
	p = pFirst;
	while (p->next != NULL)
	{
		q = p->next;
		delete p;
		p = q;
	}
	delete p;
	pFirst = NULL;
}
template<typename T>
int intList<T>::length() const {
	Node* p; int j;
	p = pFirst->next; j = 0;
	while (p != NULL)
	{
		p = p->next; j++;
	}
	return j;
}
template<typename T>
int intList<T>::find(T elem) const {
	Node* p;
	int j; T data;
	p = pFirst->next; j = 1;
	while (p != NULL)
	{
		data = p->data;
		if (data == elem) break;
		p = p->next; j++;
	}
	if (p != NULL) return j;
	else return 0;
}
template<typename T>
bool intList<T>::insert(int loc, T elem) {
	Node* p, * s; int  j;
	p = pFirst; j = 0;
	while ((p != NULL) && (j < loc - 1))
	{
		p = p->next; j++;
	}
	if (p)				//判断p不是null否则无法做P->next的引用
	{
		if ((j == loc - 1) && p->next == NULL)
		{
			s = new Node;
			p->data = elem;
			s->next = NULL;
			p->next = s;
			return true;
		}
		else if ((j == loc - 1) && (p->next != NULL))
		{
			s = new Node;
			p->data = elem;
			s->next = p->next->next;
			p->next = s;
			return true;
		}
		else return 0;
	}
}
template<typename T>
bool intList<T>::remove(int i, T& elem) {
	Node* p, * q; int j;
	p = pFirst; j = 0;
	while ((p->next != NULL) && (j < i - 1))
	{
		p = p->next; j++;
	}
	if ((p->next != NULL) && (j < i))
	{
		q = p->next;
		p->next = p->next->next;
		elem = q->data;
		delete(q);
		return true;
	}
	else return false;
}
template<typename T>
void intList<T>::printList() {
	Node* p;
	p = pFirst;
	while (p->next != NULL)
	{
		cout << p->data;
		p = p->next;
	};
	cout << endl;
}

主函数

#include"inList.h"
int main() {
	intList<int> L;		//整数链表
	for (int i = 0; i < 10; i++)
	{
		L.insert(i + 1, i);
	}
	L.printList();
	int el1;
	if (L.remove(4, el1))
	{
		cout << "Delete the fourth element success!" << "--delete is " << el1 << endl;
	}
	else {
		cout << "Delete fail!" << endl;
	}
	L.printList();
	int loc = L.find(5);
	if (loc != 0) {
		cout << "Find! The position of Element 5 is " << loc << endl;
	}
	else {
		cout << "Not find!" << endl;
	}
	intList<string> M;		//字符串链表
	for (int i = 0; i < 10; i++)
	{
		string s = "this is ";
		s += (char)(65 + i);
		M.insert(i + 1, s+" ");
	}
	M.printList();
	string el2 ;
	if (M.remove(4, el2))
	{
		cout << "Delete the fourth element success!" << "--delete is " << el2 << endl;
	}
	else {
		cout << "Delete fail!" << endl;
	}
	M.printList();
	loc = M.find("this is D ");
	if (loc != 0) {
		cout << "Find! The position of Element \"B\" is " << loc << endl;
	}
	else {
		cout << "Not find!" << endl;
	}
	return 0;
}

运行结果

在这里插入图片描述
代码仅供参考~~~~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值