天天看點

c++ 連結清單(模闆)簡單實作

/*模闆*/

#include <iostream>
using namespace std;

template<class T>
struct Node
{
	T data;
	Node<T> *next;

	Node()
	{
		next=NULL;
	}
	Node(T element)
	{
		this->data=element;
		next=NULL;
	}
};

template<class T>
class List
{
public:
	List();

	//連結清單頭添加資料
	void addFirst(T data);

	//連結清單尾添加資料
	void addLast(T data);

	//得到連結清單頭元素
	T getFirst();

	//得到連結清單尾元素
	T getLast();

	//删除連結清單頭元素并傳回值
	T removeFirt();
	T removeLast();

	//指定位置插入元素
	void addPos(size_t index,T data);

	//清空連結清單
	void clear();

	//傳回連結清單指定下标元素
	T get(size_t index);

	//傳回連結清單中第一個比對元素的結點下标
	size_t indexOf(T data);

	bool isEmpty();

	//删除指定元素(連結清單中的第一個)
	void remove(T data);

	//删除指定位置元素
	T removeAt(size_t index);

	size_t getSize();

private:
	Node<T> *head,*tail;
	size_t size;
};

template<class T>
List<T>::List()
{
	head=tail=NULL;
	size=0;
}

template<class T>
void List<T>::addFirst(T data)
{
	Node<T> *newNode=new Node<T>(data);
	newNode->next=head;
	head=newNode;
	size++;
	if(tail==NULL)
	{
		tail=head;
	}
}

template<class T>
void List<T>::addLast(T data)
{
	if(tail==NULL)
	{
		head=tail=new Node<T>(data);
	}
	else
	{
		tail->next=new Node<T>(data);
		tail=tail->next;
	}
	size++;
}

template<class T>
T List<T>::getFirst() throw(runtime_error)
{
	if(size==0)
	{
		throw runtime_error("Index out of rangge");
	}
	else
	{
		return head->data;
	}
}

template<class T>
T List<T>::getLast() throw (runtime_error)
{
	if(size==0)
	{
		throw runtime_error("Index out of range");
	}
	else
	{
		return tail->data;
	}
}

template<class T>
T List<T>::removeFirt() throw (runtime_error)
{
	if(size==0)
	{
		throw runtime_error("No element in the list");
	}
	else
	{
		Node<T> *temp=head;
		head=head->next;
		if(head==NULL)
		{
			tail=NULL;
		}
		size--;
		T data=temp->data;
		delete temp;
		return data;
	}
}

template<class T>
T List<T>::removeLast() throw(runtime_error)
{
	if(size==0)
	{
		throw runtime_error("No elements in the list");
	}
	else if(size==1)
	{
		Node<T> *temp=head;
		head=tail=NULL;
		size=0;
		T data=temp->data;
		delete temp;
		return data;
	}
	else
	{
		Node<T> *current=head;
		for(size_t i=0;i<size-2;i++)
		{
			current=current->next;
		}
		Node<T> *temp=tail;
		tail=current;
		tail->next=NULL;
		size--;
		T data=temp->data;
		delete temp;
		return data;
	}
}

template<class T>
T List<T>::removeAt(size_t index)
{
	if(index<0 || index>=size)
	{
		throw runtime_error("Index out of range");
	}
	else if(index==0)
	{
		return removeFirt();
	}
	else if(index==size-1)
	{
		return removeLast();
	}
	else
	{
		Node<T> *previous=head;
		for(size_t i=1;i<index;i++)
		{
			previous=previous->next;
		}
		Node<T> *current=previous->next;
		previous->next=current->next;
		size--;
		T data=current->data;
		delete current;
		return data;
	}
}

template<class T>
void List<T>::addPos(size_t index,T data)
{
	if(index==0)
	{
		addFirst(data);
	}
	else if(index>=size)
	{
		addLast(data);
	}
	else
	{
		Node<T> *current=head;
		for(size_t i=0;i<index;i++)
		{
			current=current->next;
		}
		Node<T> *temp=current->next;
		current->next=new Node<T>(data);
		(current->next)->next=temp;
		size++;
	}
}

template<class T>
void List<T>::clear()
{
	while(head!=NULL)
	{
		Node<T> *temp=head;
		head=head->next;
		delete temp;
		size--;
	}
	tail=NULL;
}

template<class T>
T List<T>::get(size_t index)
{
	if(index<0 || index>size-1)
	{
		throw runtime_error("Index out of range");
	}

	Node<T> *current=head;
	for(size_t i=0;i<index;i++)
	{
		current=current->next;
	}
	return current->data;
}

template<class T>
size_t List<T>::indexOf(T data)
{
	Node<T> *current=head;

	for(size_t i=0;i<size;i++)
	{
		if(current->data=data)
		{
			return i;
		}
		current=current->next;
	}
	return -1;
}

template<class T>
bool List<T>::isEmpty()
{
	return head==NULL;
}

template<class T>
void List<T>::remove(T element)
{
	Node<T> *current=head;

	for(size_t i=0;i<size;i++)
	{
		if(current->data==element)
		{
			if(i==0)
			{
				removeFirt();
			}
			else if(i==size-1)
			{
				removeLast();
			}
			else
			{
				removeAt(i);
			}
			break;
		}
		current=current->next;
	}
}

template<class T>
size_t List<T>::getSize()
{
	return size;
}
int main()
{
    /*别忘了捕獲異常*/
    try
    {

    List<int> list;

    }
    catch(exception e)
    {
        cout<<e.what()<<endl;
    }

    cout<<endl;
    system("pause");
    return 0;
}
           

繼續閱讀