單鏈表的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:我只知道用插入,產生一個線性鏈表,不知道大家還有什麼其他方法?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章