线性表链式存储

本文介绍了如何使用C++模板类实现单链表,包括创建链表、显示元素、元素定位、插入与删除操作。通过实例展示了前插法创建链表、根据序号获取和修改元素,以及删除特定位置和全部节点的功能。

 单链表

#pragma once
#include<iostream>
using namespace std;

const int ERROR = 0;
const int OK = 1;

template<class Elemtype>
class LNode
{
public:
	Elemtype data;
	LNode* next;
};

template<class Elemtype>
class Mylist
{
public:
	Mylist();
	~Mylist();
	void CreatList(int i);//创建链表
	void Show_List();//显示链表元素
	void GetElem(int i, Elemtype& record);//根据序号i的获取元素的值,并以引用的方式返回该结点的元素
	LNode<Elemtype > * LocateElem(Elemtype & record);//查找链表中一个数据域为data的元素,并且将他的结点指针返回
	void InsertElemAtIndex(int n, Elemtype& record);   //在指定位置插入指定元素
	void Delete_index(int i);//删除链表中第i个结点
	void Delete_all();//删除所有数据,但是保留链表

private:
	LNode<Elemtype> * head;//单链表头节点
};

template<class Elemtype>
Mylist<Elemtype>::Mylist()
{
	this->head = new LNode<Elemtype>();
	head->next = NULL;
	cout << "定义头节点!" << endl;
}

template<class Elemtype>
void Mylist<Elemtype>::CreatList(int i)//前插法
{
	for (int j = 0; j < i; j++)
	{
		LNode<Elemtype>* p;
		p = new LNode<Elemtype>;
		cout << "输入数据:";
		cin >> p->data;
		p->next = this->head->next;
		head->next = p;
	}
}

template<class Elemtype>
void Mylist<Elemtype>::Show_List()
{
	LNode<Elemtype>* p;
	p = head->next;
	cout << "--------------显示数据!---------------" << endl;
	while (p)
	{
		cout << p->data << "\t";
		p = p->next;
	}
}

template<class Elemtype>
void Mylist<Elemtype>::GetElem(int i, Elemtype& record)
{
	LNode<Elemtype> *p;
	p = this->head->next;
	int j = 1;
	while (p&&j != i)//p不为空,或者j不等于i的时候,p继续往下执行
	{
		p = p->next;
		j++;
	}
	//循环结束后,不是啥都要你的data,一定要验证p此时不为空
	if(p)
		record = p->data;
	else
	{
		cout << "未找到!" << endl;
		exit(ERROR);
	}
}

template<class Elemtype>
LNode<Elemtype>* Mylist<Elemtype>:: LocateElem(Elemtype& record)
{
	LNode<Elemtype>* p;
	p = head->next;
	while (p->data!=record&&p)
	{
		p = p->next;
	}
	return p;//不用担心p的问题,因为如果跳出循环,是查找成功,则一切顺利返回p,如果p为空,直接返回NULL,也没事
}

template<class Elemtype>
void Mylist<Elemtype>::InsertElemAtIndex(int i, Elemtype& record)
{
	if (i == 1)
	{
		LNode<Elemtype>* p;
		p = new LNode<Elemtype>;
		p->data = record;
		p->next = this->head->next;
		head->next = p;
	}
	else
	{
		Elemtype temp;
		this->GetElem(i - 1, temp);
		LNode<Elemtype>* p;
		p = this->LocateElem(temp);
		if (!p)
			exit(ERROR);
		LNode<Elemtype>* q;
		q = new LNode<Elemtype>;
		q->data = record;
		q->next = p->next;
		p->next = q;
	}
}

template<class Elemtype>
void Mylist<Elemtype>::Delete_index(int i)
{
	Elemtype temp;
	this->GetElem(i-1, temp);
	LNode<Elemtype>* p;
	p = this->LocateElem(temp);
	if (!p)
		exit(ERROR);
	LNode<Elemtype>* q;
	q = p->next;
	p->next = q->next;
	delete q;
}

template<class Elemtype>
void Mylist<Elemtype>::Delete_all()
{
	LNode<Elemtype>* p;
	LNode<Elemtype>* q;
	p = head->next;
	q = p;
	while (p)
	{
		q = p->next;
		delete p;
		p = q;
	}
	this->head->next = NULL;
}

template<class Elemtype>
Mylist<Elemtype>::~Mylist()
{
	if(!this->head->next)
		this->Delete_all();
	delete this->head;
	this->head = NULL;
	cout << "析构函数" << endl;
}

 主程序

#include<iostream>
#include<string>
#include"线性表的链式存储.hpp"
using namespace std;

void test()
{
	Mylist<int> L;
	LNode<int>* node;
	int c = 5;
	L.CreatList(c);
	int temp1 = 99;
	L.InsertElemAtIndex(1, temp1);
	int result;
	L.GetElem(1, result); cout << "第1个元素是:" << result << endl;
	L.Delete_index(2);
	node = L.LocateElem(result);
	cout << "第二个元素的值是:" << node->next->data << endl;
}


int main()
{
	
	test();

	
	system("pause");
	return 0;
}

可以复制下来,自行验证!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值