单链表的C++的实现

代码如有问题和优化的地方,请指出,谢谢!

1.头文件

#ifndef CHAIN_H
#define CHAIN_H


#include<iostream>

template<class T>
class chainnode
{
  template<class T>
  friend class chain;
public:
	chainnode()
	{
		data = 0;
		next = nullptr;
	}
	~chainnode()
	{

	}
public:
	T getdata()
	{
		return data;
	}
	chainnode<T>* getnext()
	{
		return next;
	}
private:
	T data;
	chainnode<T>* next;
};

template<class T>
class chain
{

public:
	chain()
	{
		first = nullptr;
		length = 0;
	}
	~chain()
	{
		chainnode<T>* temp;
		while (first)
		{
			temp = first->next;
			delete first;
			first = temp;
		}
	}

public:
	bool isempty()
	{
		return top == nullptr;
	}
	chainnode<T>* getfirst()
	{
		return first;
	}

	void deletenode(int pos);
	int  getlength();
	void insertbefore(T dat, int pos);
	void insertafter(T dat, int pos);
	T find(int pos);
private:
	chainnode<T>* first;
	int length;
};


template<class T>
void chain<T>::insertbefore(T dat, int pos)
{
	if (pos< 0 || pos>length)
	{
		std::cout << "输入的位置数错误" << endl;
		exit(1);
	}

	chainnode<T>* now = new chainnode < T >;
	now->data = dat;
	if (first == nullptr)
	{
		first = now;
		first->next = nullptr;
		++length;
	}
	else
	{
		chainnode<T>* pp = first;
		for (int i = 1; i < pos - 1 && pp; ++i)
			pp = pp->next;
		if (pp)
		{
			if (pos == 1)
			{
				now->next = first;
				first = now;
			}
			else
			{
				now->next = pp->next;
				pp->next = now;
			}
			++length;
		}
	}
}

template<class T>
void chain<T>::insertafter(T dat, int pos)
{
	if (pos < 0 || pos>length)
	{
		std::cout << "输入的位置数错误" << endl;
		exit(1);
	}

	chainnode<T>* now = new chainnode < T >;
	now->data = dat;
	if (first == nullptr)
	{
		first = now;
		first->next = nullptr;
		++length;
	}
	else
	{
		chainnode<T>* pp = first;
		for (int i = 1; i < pos && pp; ++i)
			pp = pp->next;
		if (pp)
		{
			if (pos == length)
			{
				now->next = nullptr;
				pp->next = now;
				
			}
			else
			{
				now->next = pp->next;
				pp->next = now;
			}
			++length;
		}
	}
}
template<class T>
T chain<T>::find(int pos)
{
	if (first)
	{
		if (pos<0 || pos>length)
		{
			std::cout << "输入的位置数错误" << endl;
			exit(1);
		}
		chainnode<T>* curr=first;
		for (int i = 1; i < pos; ++i)
			curr = curr->next;
		return curr->data;
	}
}
template<class T>
int chain<T>::getlength()
{
	chainnode<T>* current = first;
	int len = 0;
	while (current)
	{
		++len;
		current = current->next;
	}
	length = len;
	return len;
}

template<class T>
void chain<T>::deletenode(int pos)
{
	if (first)
	{
		if (pos<0 || pos>length)
		{
			std::cout << "输入的位置数错误" << endl;
			exit(1);
		}
		if (pos == 1)
		{
			chainnode<T>* temp;
			temp = first;
			first = temp->next;
			delete temp;
			--length;
		}
		else
		{
			chainnode<T>* tem = first;
			for (int i = 1; i < pos - 1 && tem; ++i) //获得pos前一个元素的地址
				tem = tem->next;
			if (tem &&tem->next)   //确保第pos个元素存在
			{
				chainnode<T>* curr = tem->next; //curr是pos位置元素的地址
				tem->next = curr->next;
				delete curr;
				--length;
			}
		}
	}
}

template<class T>
std::ostream& operator <<(std::ostream& out, chain<T>& list)
{
	chainnode<T>* cc = list.getfirst();
	for (int i = 1; i <= list.getlength(); ++i)
	{
		out <<cc->getdata() << " ";
		cc = cc->getnext();
	}
	std::cout << std::endl;
	return out;
}
#endif

2.主文件:

#include<iostream>
#include"chain.h"
using namespace std;

int main()
{
	chain<int> liner;
	liner.insertafter(10, 0);
	liner.insertafter(9, 1);
	liner.insertafter(8, 2);
	liner.insertafter(7, 3);
	liner.insertbefore(2,1);
	auto dd = liner.find(3);
	cout << dd << endl;
	cout<< liner << endl;
	system("pause");
	return 0;
}
PS:我只知道用插入,产生一个线性链表,不知道大家还有什么其他方法?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值