C++单链表操作

#pragma once


template<class ElemType>
class LinkList
{

public:
	LinkList();
	~LinkList();

	struct Node
	{
		ElemType data;
		Node*	 next;
	};

	//************************************
	// Method:    Push_Front 在链表头插入元素
	// FullName:  LinkList<ElemType>::Push_Front
	// Access:    virtual public 
	// Returns:   bool
	// Qualifier:
	// Parameter: ElemType _elem
	//************************************
	virtual bool Push_Front( ElemType _elem );

	//************************************
	// Method:    Push_Back 在链表尾插入元素
	// FullName:  LinkList<ElemType>::Push_Back
	// Access:    virtual public 
	// Returns:   bool
	// Qualifier:
	// Parameter: ElemType _elem
	//************************************
	virtual bool Push_Back( ElemType _elem );

	//************************************
	// Method:    isEmpty 是否为空
	// FullName:  LinkList<ElemType>::isEmpty
	// Access:    virtual public 
	// Returns:   bool
	// Qualifier:
	//************************************
	virtual bool isEmpty();

	//************************************
	// Method:    InsertElement
	// FullName:  LinkList<ElemType>::InsertElement
	// Access:    virtual public 
	// Returns:   bool
	// Qualifier:
	// Parameter: int _index
	// Parameter: ElemType _elem
	//************************************
	virtual bool InsertElement(int _index, ElemType _elem);

	//************************************
	// Method:    DeleteElement 删除对应元素
	// FullName:  LinkList<ElemType>::DeleteElement
	// Access:    virtual public 
	// Returns:   bool
	// Qualifier:
	// Parameter: ElemType _elem
	//************************************
	virtual bool DeleteElement(ElemType _elem);

	//************************************
	// Method:    DeleteElementByIndex 删除指定位置的元素
	// FullName:  LinkList<ElemType>::DeleteElementByIndex
	// Access:    virtual public 
	// Returns:   bool
	// Qualifier:
	// Parameter: int _index
	//************************************
	virtual bool DeleteElementByIndex(int _index);

	//************************************
	// Method:    FindElement 查找元素
	// FullName:  LinkList<ElemType>::FindElement
	// Access:    virtual public 
	// Returns:   bool 
	// Qualifier:
	// Parameter: ElemType _elem
	//************************************
	virtual bool FindElement(ElemType _elem);

	virtual void Print();

private:
	Node* pHead;
	Node* pEnd;
	int   InSize;
};

template<class ElemType>
bool LinkList<ElemType>::FindElement(ElemType _elem)
{
	cout << "查找元素: " << _elem << endl;

	Node* temp = pHead;

	while (temp)
	{
		if (temp->data == _elem)
		{
			cout << "已找到查找的元素: " << _elem << endl;
			return true;
		}
		temp = temp->next;
	}

	cout << "未找到查找的元素: " << _elem << endl;
	return false;
}

template<class ElemType>
bool LinkList<ElemType>::DeleteElementByIndex(int _index)
{
	cout << "删除索引: " << _index << " 的元素" << endl;

	if (_index <= 0) return false;

	Node* temp = pHead;
	Node* temp1 = NULL;
	int t_index = _index - 1;

	while (temp && t_index > 0)
	{
		t_index--;
		temp1 = temp;
		temp = temp->next;
	}

	if (temp == pHead)
	{
		pHead = pHead->next;

		delete temp1;

		temp1 = NULL;

		InSize--;

		if (InSize == 0)
		{
			pHead = NULL;
			pEnd = NULL;
		}

		Print();

		return true;
	}
	else
	{
		temp1->next = temp->next;

		if (temp == pEnd)
		{
			pEnd = temp1;
		}

		delete temp;

		temp = NULL;

		InSize--;

		if (InSize == 0)
		{
			pHead = NULL;
			pEnd = NULL;
		}

		Print();

		return true;
	}

	return false;
}

template<class ElemType>
bool LinkList<ElemType>::DeleteElement(ElemType _elem)
{
	cout << "删除元素: " << _elem << endl;

	Node* temp = pHead;
	Node* temp1 = NULL;
	while (temp)
	{
		if (temp->data == _elem)
		{
			if ( temp == pHead )
			{
				pHead = temp->next;

				delete temp;

				temp = NULL;

				InSize--;

				if (InSize == 0)
				{
					pHead = NULL;
					pEnd = NULL;
				}

				Print();

				return true;
			}
			else
			{
				temp1->next = temp->next;

				if (temp == pEnd)
				{
					pEnd = temp1;
				}

				delete temp;

				temp = NULL;

				InSize--;

				if (InSize == 0)
				{
					pHead = NULL;
					pEnd = NULL;
				}

				Print();

				return true;
			}
		}

		temp1 = temp;
		temp = temp->next;
	}

	return false;
}

template<class ElemType>
void LinkList<ElemType>::Print()
{
	Node* temp = pHead;
	while ( temp )
	{
		cout << temp->data << " ";

		temp = temp->next;
	}

	//if (pHead)
	//{
	//	cout << "头结点:" << pHead->data << " 尾结点:" << pEnd->data << " size: " << InSize;
	//}
	cout << endl;
}

template<class ElemType>
bool LinkList<ElemType>::InsertElement(int _index, ElemType _elem)
{
	cout << "在索引: " << _index << " 位置加入元素 :" << _elem << endl;

	if (_index >= InSize)
	{
		Push_Back(_elem);
	}
	else if (_index <= 0 || _index == 1)
	{
		Push_Front(_elem);
	}
	else
	{
		Node* temp = new Node;

		temp->data = _elem;

		temp->next = NULL;

		Node* pFind = pHead;

		int t_index = 0;

		while (pFind && t_index < _index - 2)
		{
			pFind = pFind->next;

			t_index++;
		}

		temp->next = pFind->next;

		pFind->next = temp;

		InSize++;
	}

	Print();

	return true;
}

template<class ElemType>
bool LinkList<ElemType>::isEmpty()
{
	return pHead == NULL;
}

template<class ElemType>
bool LinkList<ElemType>::Push_Back(ElemType _elem)
{
	cout << "在尾部加入元素: " << _elem << endl;

	Node* temp = new Node;

	temp->data = _elem;

	temp->next = NULL;

	if (this->isEmpty())
	{
		pHead = temp;

		pEnd = pHead;
	}
	else
	{
		pEnd->next = temp;

		pEnd = temp;
	}

	InSize++;

	Print();

	return true;
}

template<class ElemType>
bool LinkList<ElemType>::Push_Front(ElemType _elem)
{
	cout << "在头部加入元素: " << _elem << endl;

	Node* temp = new Node;

	temp->data = _elem;

	temp->next = NULL;

	if ( this->isEmpty() )
	{
		pHead = temp;

		pEnd  = pHead;
	}
	else
	{
		temp->next = pHead;

		pHead = temp;
	}

	InSize++;

	Print();

	return true;
}

template<class ElemType>
LinkList<ElemType>::~LinkList()
{
	Node* pTemp = pHead;
	while ( pTemp )
	{
		Node* t_next = pTemp->next;

		delete pTemp;

		pTemp = NULL;

		pTemp = t_next;
	}

	pHead = NULL;
}

template<class ElemType>
LinkList<ElemType>::LinkList()
{
	pHead = NULL;
	pEnd  = NULL;
	InSize = 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值