码迷,mamicode.com
首页 > 编程语言 > 详细

C++用模板实现单项链表List

时间:2018-08-18 17:40:54      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:个数   name   namespace   while   析构函数   class   front   函数   node   

#include <iostream>

using namespace std;

template<class T>
class List
{
	struct Node
	{
		T data;
		Node* next;
		Node() {next=NULL;}
	};
	Node* head;
public:
	List()
	{
		head = new Node;
	}
	List(List<T>& that)//拷贝构造
	{
		head = new Node;
		Node* node = that.head->next;
		while(node!=NULL)
		{
			push_back(node->data);
			node=node->next;
		}
	}
	List operator = (List<T>& that)//赋值构造
	{
		head = new Node;
		List<T> list(that);
		head = list.head;
	}
	void push_back(T data)//在list的末尾添加一个元素
	{
		Node* node = new Node;
		node->data = data;
		back()->next = node;
	}
	void pop_back()//删除最后一个元素
	{
		Node* node = head;
		while(node->next->next!=NULL) node=node->next;
		delete node->next;
		node->next = NULL;
	}
	Node* front()//返回第一个元素
	{
		Node* node = head->next;
		return node;
	}
	Node* back()//返回最后一个元素
	{
		Node* node = head;
		while(node->next!=NULL) node=node->next;
		return node;
	}
	bool empty()//判断list是否为空
	{
		return head->next==NULL;
	}
	void reserve()//反转链表
	{
		if(head->next==NULL||head->next->next==NULL)
			return;
		Node* prevNode = NULL;
		Node* curNode = head->next;
		while(curNode!=NULL)
		{
			Node* nextNode = curNode->next;
			curNode->next = prevNode;
			prevNode = curNode;
			curNode = nextNode;
		}
		head->next = prevNode;
	}
	void remove(T val)//从list删除元素
	{
		Node* node = head->next;
		while(node!=NULL)
		{
			if(node->next!=NULL && node->next->data == val)
			{
				Node* no = node->next;
				node->next = node->next->next;
				delete no;
			}
			else node = node->next;
		}
	}
	void merge(List<T> list)//合并两个list
	{
		Node* node = list.head->next;
		while(node!=NULL)
		{
			push_back(node->data);
			node=node->next;
		}
	}
	void clear()//删除所有元素
	{
		Node* node = head;
		while(node!=NULL)
		{
			Node* temp = node;
			node = node->next;
			delete temp;
		}
	}
	void swap(List<T>& list)//交换两个list
	{
		Node* node = list.head;
		list.head = head;
		head = node;
	}
	int size()//返回list中的元素个数
	{
		Node* node = head->next;
		int count = 0;
		while(node!=NULL)
		{
			count++;
			node=node->next;
		}
		return count;
	}
	void sort()//给链表排序
	{
		int n = size();
		T t[n];
		Node* node = head->next;
		for (int i = 0; i < n; ++i)
		{
			t[i] = node->data;
			node = node->next;
		}
		for (int i = 0; i < n-1; ++i)
			for (int j = i+1; j < n; ++j)
				if(t[i]>t[j])
				{
					T a = t[i];
					t[i] = t[j];
					t[j] = a;
				}		
		clear();
		head = new Node;
		for (int i = 0; i < n; ++i)
			push_back(t[i]);
	}
	friend ostream& operator << (ostream& os,List& list)//重载输出<<运算符
	{
		Node* node=list.head->next;
		while(node != NULL)
		{
			os << node->data << " ";
			node = node->next;
		}
		return os;
	}
	~List()//析构函数
	{
		clear();
	}
};

int main()
{
	List<int> list;
	List<int> li;
	for(int i=0;i<10;i++)
		list.push_back(i);
	for (int i = 20; i > 10; --i)
		li.push_back(i);
	list.swap(li);
	cout << list << endl;
	cout << li << endl;
	list.merge(li);
	cout << list << endl;
	list.sort();
	list.reserve();
	cout << list << endl;
}

  

C++用模板实现单项链表List

标签:个数   name   namespace   while   析构函数   class   front   函数   node   

原文地址:https://www.cnblogs.com/yyb123/p/9497618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!