C++模板实现的单向链表

28 篇文章 5 订阅
27 篇文章 1 订阅

C++模板实现的单向链表,实现了链表的初始化创建,元素插入,元素链表末尾添加,元素删除,链表清空

//Lists.h

#ifndef DDXXLISTS_H
#define DDXXLISTS_H
#include <iostream>
using namespace std;
template<typename Type>
class Lists
{
public:
	Lists();
	Lists(int nSize);
	~Lists();
public:
	struct Node
	{
		Type  e;
		Node* next;
		Node()
		{
		}
		Node(Type _e)
		{
			e = _e;
			next = NULL;
		}
	};
public:
	bool insert(Type e,int pos);
	bool add(Type e);
	bool erase(int pos);
	void clear();
	bool isEmpty();
	int getLength();
	void print();
private:
	Node* m_head;
	int m_nLength;
};

template<typename Type> Lists<Type>::Lists()
{
	Node *pPos = new Node;
	pPos->next = NULL;
	m_head = pPos;
	m_nLength = 0;
}

template<typename Type> Lists<Type>::Lists(int nSize)
{
	m_nLength = nSize;
	Node *pPos = new Node;
	pPos->next = NULL;
	m_head = pPos;
	m_nLength = nSize;
	
	for (int i=0;i<m_nLength;i++)
	{
		pPos->next = new Node;
		pPos = pPos->next;
	}
}

template<typename Type> bool Lists<Type>::insert(Type e,int pos)
{
	if (pos >= m_nLength)
	{
		cout<<"The position to insert out of range"<<endl;
		return false;
	}
	else
	{
		Node *ptr = m_head;
		int cnt = 0;
		while(cnt <= pos)
		{
			ptr = ptr->next;
			cnt++;
		}
		Node *pNew = new Node(e);
		pNew->next = ptr->next;
		ptr->next = pNew;

		m_nLength++;
		return true;
	}
}

template<typename Type> bool Lists<Type>::add(Type e)
{
	Node *pNew = new Node(e);
	if (pNew == NULL)
	{
		cout<<"allocate memory for new node failed"<<endl;
		return false;
	}
	else
	{
		Node *ptr = m_head;
		while(ptr->next != NULL)
		{
			ptr = ptr->next;
		}
		ptr->next = pNew;
		pNew->next = NULL;
		m_nLength++;
		return true;
	}
}

template<typename Type> bool  Lists<Type>::erase(int pos)
{
	if (pos >= m_nLength)
	{
		cout<<"The position to delete out of range"<<endl;
		return false;
	}
	else
	{
		Node *ptr = m_head;
		int i = 0;
		while( i<pos )
		{
			ptr = ptr->next;
			i++;
		}
		Node *pdel = ptr->next;
		ptr->next = ptr->next->next;
		delete pdel;
		pdel = NULL;
		m_nLength--;

		return true;
	}
}

template<typename Type> void Lists<Type>::print()
{
	if(m_nLength ==0)
		cout<<"The list is empty"<<endl;
	Node *ptr = m_head;
	while(ptr->next!= NULL)
	{
		ptr = ptr->next;
		cout<<"element value:"<<ptr->e<<endl;
	}
}

template<typename Type> int Lists<Type>::getLength()
{
	return m_nLength;
}
template<typename Type> bool Lists<Type>::isEmpty()
{
	return m_head->next == NULL;
}

template<typename Type> void Lists<Type>::clear()
{
	Node *ptr = m_head->next;
	Node *ptmp = NULL;
	while(ptr != NULL)
	{
		ptmp = ptr;
		ptr = ptr->next;
		delete ptmp;
		ptmp = NULL;
		m_nLength--;
	}
	m_head->next = NULL;

}
template<typename Type> Lists<Type>::~Lists()
{
	Node *ptr = m_head;
	Node *ptmp = NULL;
	while(ptr->next != NULL)
	{
		ptmp = ptr;
		ptr = ptr->next;
		delete ptmp;
		ptmp = NULL;
	}
	delete ptr;
	ptr = NULL;
}
#endif

//main.cpp

#include <iostream>
#include "Lists.h"
using namespace std;
void main()
{
	cout<<"*************************Test list init***************************"<<endl;
	Lists<int> Lisa;
	cout<<"List's length is:"<<Lisa.getLength()<<endl;
	cout<<"List is empty:"<<Lisa.isEmpty()<<endl;

	cout<<"*************************Test list add element********************"<<endl;
	Lisa.add(1);
	Lisa.add(2);
	Lisa.add(3);
	Lisa.add(4);
	Lisa.add(5);
	Lisa.print();
	cout<<"List's length is:"<<Lisa.getLength()<<endl;
	cout<<"List is empty:"<<Lisa.isEmpty()<<endl;

	cout<<"*************************Test list insert*************************"<<endl;
	Lisa.insert(-1,0);
	Lisa.insert(11,2);
	Lisa.insert(100,6);
	Lisa.print();
	cout<<"List's length is:"<<Lisa.getLength()<<endl;
	cout<<"List is empty:"<<Lisa.isEmpty()<<endl;

	cout<<"*************************Test list erase**************************"<<endl;
	Lisa.erase(0);
	Lisa.erase(2);
	Lisa.erase(5);
	Lisa.print();
	cout<<"List's length is:"<<Lisa.getLength()<<endl;
	cout<<"List is empty:"<<Lisa.isEmpty()<<endl;

	cout<<"*************************Test list clear**************************"<<endl;
	Lisa.clear();
	Lisa.print();
	cout<<"List's length is:"<<Lisa.getLength()<<endl;
	cout<<"List is empty:"<<Lisa.isEmpty()<<endl;
}

程序运行结果:





  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的单向链表模板的设计: ```c++ template<typename T> class LinkedListNode { public: T data; // 存储节点数据 LinkedListNode<T>* next; // 存储指向下一个节点的指针 LinkedListNode(T data) { this->data = data; this->next = nullptr; } }; template<typename T> class LinkedList { private: LinkedListNode<T>* head; // 存储指向链表头节点的指针 public: LinkedList() { this->head = nullptr; } ~LinkedList() { LinkedListNode<T>* current = this->head; while (current != nullptr) { LinkedListNode<T>* next = current->next; delete current; current = next; } } void add(T data) { LinkedListNode<T>* node = new LinkedListNode<T>(data); if (this->head == nullptr) { this->head = node; } else { LinkedListNode<T>* current = this->head; while (current->next != nullptr) { current = current->next; } current->next = node; } } void remove(T data) { LinkedListNode<T>* current = this->head; LinkedListNode<T>* previous = nullptr; while (current != nullptr) { if (current->data == data) { if (previous == nullptr) { this->head = current->next; } else { previous->next = current->next; } delete current; return; } previous = current; current = current->next; } } void print() { LinkedListNode<T>* current = this->head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } }; ``` 该链表类具有添加节点、删除节点和打印链表等基本功能。其中,节点类`LinkedListNode`包含了节点数据和指向下一个节点的指针;链表类`LinkedList`包含了指向链表头节点的指针,以及添加、删除和打印链表的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值