单链表

#pragma once

#include "stdafx.h"
#include "linearList.h"
template
struct Node{
	Node* next = nullptr;
	T data;
	Node() = default;
	Node(T theData, Node* theNext=nullptr){ next = theNext; data = theData; }
};



//Tyical data-struct template chain
template
struct chain :public linearList{
	~chain();
	chain() { headPtr = tailPtr = nullptr; chainLength = 0; }
	chain(const chain&);
	
	void insert(const T& data, size_t Index)override; 
	void push_back(const T &data);
	void push_front(const T &data);
	size_t size()const override{ return chainLength; }
	bool empty()const override{ return chainLength != 0; }
	void erase(const size_t &index)override;
	void output(ostream& out)const override;
	T& searchIndex(const size_t& index)const;
	Node* searchData(const T&)const;
protected:
	Node* headPtr = nullptr;
	Node* tailPtr = nullptr;
	size_t chainLength = 0;
};

template
chain::~chain(){
	auto temp = headPtr;
	while (temp){
		temp = temp->next;
		delete headPtr;
		headPtr = temp;
	}
}

template
chain::chain(const chain& fromChain){
	auto temp = fromChain.headPtr;
	if (temp){
		tailPtr=headPtr = new Node(&temp);
		temp = temp->next;
	}
	while (temp){
		tailPtr->next = new Node(&temp);
		tailPtr = tailPtr->next;
		temp = temp->next;
	}
	chainLength = fromChain.chainLength;
}

template
void chain::insert(const T& data, size_t Index){
	int i = 0;
	auto temp = headPtr;
	while (i != Index)temp = temp->next;
	temp->next = new Node(data, temp->next);
	++chainLength;
}

template
void chain::push_back(const T &data){
	if (chainLength == 0){
		tailPtr = new Node(data);
		headPtr = tailPtr;
		++chainLength;
	}
	else{
		tailPtr->next = new Node(data);
		tailPtr = tailPtr->next;
		++chainLength;
	}
}

template
void chain::push_front(const T &data){
	headPtr = new Node( data, headPtr);
	if (chainLength == 0){
		tailPtr = headPtr;
	}
	++chainLength;
}

template
void chain::erase(const size_t& index){
	if (!index){
		auto temp= headPtr;
		headPtr = headPtr->next;
		delete temp;
	}
	else if(index==chainLength){
		auto temp = headPtr;
		for (int i = 1; i != chainLength ; ++i){
			temp = temp->next;
		}
		delete temp->next;
		tailPtr = temp;
		--chainLength;
	}
	else{
		auto temp = headPtr;
		for (int i = 1; i != index; ++i){
			temp = temp->next;
		}
		auto toerase = temp;
		temp->next = temp->next->next;
		delete toerase->next;
		--chainLength;
	}
}

template
void chain::output(ostream& out)const{
	Node* temp = headPtr;
	for (int i = 0; i != chainLength; ++i){
		out << temp->data<<" ";
		temp = temp->next;
	}
}

template
T& chain::searchIndex(const size_t& index)const{
	if (index <= || index >= size())throw "Illegal index !!F::searchIndex";
	auto temp = headPtr;
	for (int i = 0; i != index; ++i){
		temp = temp->next;
	}
	return temp->data;
}

template
Node* chain::searchData(const T& theElement)const{
	auto temp = headPtr;
	while (temp&&temp->data!=theElement){
		temp = temp->next;
	}
	if (!temp)throw "No such data!!F::searchData";
	else return temp;
}







你可能感兴趣的:(单链表)