链表类的实现

接着上面那篇,下面给出链表类的实现,不晓得为啥谢了Node类的析构函数就会出现重复释放的问题,可能是因为List类析构时会析构那个节点,然后在节点析构时又析构了一次,重复释放了。
对于以上那个问题,我已经解决了,请各位一定一定不要犯我的错误,有指针!=开辟空间,不是说有指针就要重写析构防止内存泄漏或者拷贝函数防止浅拷贝,有指针,不代表你申请了空间,你都没有申请空间干嘛要手动释放?或者说,你指针都没指向一片空间,就是NULL,干嘛需要你去重写拷贝构造函数?
各位一定一定要注意这个错误,有指针不一定要重写析构和拷贝构造,而是要看你有没有自己申请堆区空间。
(附带:代码非常粗糙,几乎没有考虑任何链表为空时的状态,权当练手,有缘人可以自己扩充。哈哈哈哈!)

//1.初始化**
//2.增:1.头插法2.尾插法3.序号插**
//3.删除:1.按序号删2.按照内容删除**
//4.查:1.按照序号查2.按照内容查看**
//5.改:1.按照序号改2.按照内容改
//6.判空**
//7.遍历**
//8.清空链表**
//9.显示链表长度**
//10.链表排序

#include<iostream>
using namespace std;
class Node//创建节点类
{
public:
	Node()//构造函数
	{
		this->point = 0;
		this->next = NULL;
	}
	///不是说有指针就要释放空间,是看程序里有没有手动地开辟空间,若是手动开辟空间了就要手动释放,如果光是有指针成员函数,但是没手动开辟空间,那还释放什么啊?所以这里不需要重写析构和拷贝构造函数。
	/*~Node()
	{
		if (this->next != NULL)
		{
			delete this->next;
		}
		this->next = NULL;
	}*/
	//拷贝构造函数, 深拷贝
		//Node(const Node& another)
		//{
		//	this->point = point;
		//	this->next=new Node;//new 出一个空间,返回一个指针出来
		//}
		int point;
	    Node* next;
};
//创建链表类,由于成员函数可以直接操作或者调用头指针属性,所以不用像普通函数那样传入二级指针或者一级指针
class List
{
public:
	List()//构造函数,不是初始化链表函数,直接操作头指针属性,但我也直接构造出一个无意义的头结点出来
	{
		this->head = new Node;
		this->head->next = NULL;
		head->point = 0;
	}
	~List()//析构函数,对整个链表进行删除,成员函数直接操作并调用头指针属性,所以不用传入二级指针进来
	{
		Node* del_p = NULL;
		while (head->next != NULL)
		{
			del_p = head->next;
			delete head;
			head = del_p;
		}
		delete head;
		this->head = NULL;
	}
	//链表初始化,成员函数指直接调用并修改头指针属性,所以不用传入二级指针
	//函数传入参数的原因是要使用或者修改这个量,成员函数都可以直接访问并修改成员属性,那我要传个屁啊
	void ListInit()//链表初始化,创建一个无意义的头结点
	{
		this->head = new Node;
		this->head->next = NULL;
		head->point = 0;
	}
	//清空链表,不是删除链表
	void clearList()
	{
		Node* del_p = this->head->next;
		Node* temp_q = NULL;
		while (head->next != NULL)
		{
			temp_q = del_p->next;
			delete del_p;
			del_p = temp_q;
		}
		this->head->next = NULL;
	}
	//判断是否为空链表,由于有一个无意义的头结点
	bool isEmpty()
	{
		if (this->head->next == NULL || this->head == NULL)
		{
			return true;
		}
		return false;
	}
	void Print()
	{
		Node* temp;
		temp = this->head->next;
		while (temp != NULL)
		{
			cout << temp->point << endl;
			temp = temp->next;
		}
	}
	int  LengthofList()
	{
		int i = 0;//计数
		Node* temp;
		temp = this->head->next;
		i = 1;
		while (temp)
		{
			temp = temp->next;
			++i;
		}
		return i;
	}
	//头插法,有头结点的
	void ListheadInsert()
	{
		//这里我就不自己输入了,直接用循环来存储数据
		for (int i = 0; i < 5; i++)
		{
			Node* p = new Node;
			p->point = i + 1;
			p->next = this->head->next;
			this->head->next = p;
		}
	}
	//尾插法,先找到尾结点,然后,不断迭代,存在头结点,所以注意下
	void ListTailInsert()
	{
		Node* p = this->head;
		while (p->next != NULL)
		{
			p = p->next;
		}
		//p是尾结点了
		for (int i = 0; i < 5; i++)
		{
			Node* temp = new Node;
			temp->point = i + 1;
			temp->next = NULL;
			p->next = temp;
			p = temp;
		}
	}
	//序号插法
	void NumLInsert(int i)
	{
		int j = 1;
		Node* p = this->head->next;
		while (j < i && (p))
		{
			++j;
			p = p->next;
		}
		for (i = 0; i < 5; i++)
		{
			Node* temp = new Node;
			temp->point = i + 1;
			temp->next = p->next;
			p->next = temp;
		}
	}
	//序号删
	void Numdelete(int i)
	{
		int j = 1;
		Node* p = this->head->next;
		while (j < i - 1 && (p))
		{
			p = p->next;
			++j;
		}
		p->next = p->next->next;
		delete p->next;
	}
	//按照内容删除
	void intdelete(int point)
	{
		Node* p = this->head;
		Node* q = p->next;
		while (q->point != point)
		{
			p = q;
			q = q->next;
		}
		p->next = q->next;
		delete q;
	}
	//按照序号查
	void Numfind(int i)
	{
		int j = 1;
		Node* p = this->head->next;
		while (j < i && (p))
		{
			++j;
			p = p->next;
		}
		cout << p->point << endl;
	}
	//按照内容查
	void intfind(int i)
	{
		Node* p = this->head->next;
		while (p->point != i)
		{
			p = p->next;
		}
		cout << p->point << endl;
	}
	void sortList()
	{

	}

	Node* head;//需要头指针来维护这一个链表的存在
	int size;//表示链表的长度
};
void test()
{
	List a1;
	a1.ListInit();
	a1.ListheadInsert();
	a1.Print();
	a1.ListheadInsert();
	a1.Print();
}
int main()
{
	test();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值