/*
* @Description:
* @版本: 2.0
* @作者: jianwen
* @Date: 2019-10-26 16:32:44
* @LastEditors: jianwen
* @LastEditTime: 2019-10-28 10:43:43
*/
# include<iostream>
using namespace std;
typedef int DataType;
#define Node ElemType
#define ERROR NULL
//构建一个节点类
class Node
{
public:
int data; //数据域
Node * next; //指针域
};
class LinkList
{
public:
LinkList(); //构建一个单链表;
~LinkList(); //销毁一个单链表;
void CreateLinkList(int n); //创建一个单链表
void TravalLinkList(); //遍历线性表
int GetLength(); // 获取线性表长度
bool IsEmpty(); //判断线性表是否为空
ElemType *Find(DataType data); // 查找节点
void InsertElemAtEnd(DataType data);//在尾部插入指定元素
void InsertElemAtHead(DataType data); //在头部插入指定元素
void InsertElemAtIndex(DataType Data, int n); //在指定位置插入元素
void DeleteElemAtEnd(); //在尾部删除元素
void DeleteElemAtHead(); //在头部删除元素
void DeleteElemAtIndex(DataType Data); //删除指定的数据
void DeleteAll();//删除所有数据
private:
ElemType * head; //头结点
};
LinkList::LinkList()
{
head = new ElemType; //开辟一个存放ElemType类型的存储空间,返回一个指向该存储空间的地址(即指针)
head ->data = 0; // ->前面放的是指针,而.前面跟的是变量,将头结点的数据域定义为0
head ->next = NULL; //头结点的下一个定义为NULL
}
LinkList::~LinkList()
{
delete head; //删除头结点
}
//创建一个单链表(创建两个指针,一个指针用来创建节点,一个指针用来连接结点)
void LinkList::CreateLinkList(int n)
{
ElemType *pnew, *ptemp; //定义两个指针,其中ptemp指向头结点
ptemp = head; //head只是一个地址
if (n < 0) { //当输入的值有误时,处理异常
cout << "输入的节点个数有误" << endl;
}
for (int i = 0; i < n;i++) { //将值一个一个插入单链表中
pnew = new ElemType; // pnew是一个地址(指针)为结点开辟一个新空间
cout << "请输入第" << i + 1 << "个值: " ;
cin >> pnew->data;
pnew->next = NULL; //新节点的下一个地址为NULL
ptemp->next = pnew; //当前结点的下一个地址设为新节点
ptemp = pnew; //将当前结点设为新节点
}
}
//遍历整个链表
void LinkList::TravalLinkList()
{
if (head == NULL || head ->next == NULL)
cout << "链表为空表" << endl;
ElemType *p;
p = head;
while (p->next != NULL)
{
p = p->next;
cout << p->data << " ";
}
}
int LinkList::GetLength()
{
ElemType *p;
p = head;
int length = 0;
while (p->next != NULL)
{
length++;
p = p ->next;
}
return length;
}
bool LinkList::IsEmpty(){
if (head ->next == NULL)
cout << "链表为空表" << endl;
return true;
return false;
}
ElemType *LinkList::Find(DataType data)
{
ElemType * p = head;
if (p == NULL) { //当为空表时,报异常
cout << "此链表为空链表" << endl;
return ERROR;
}
else
{
while (p->next != NULL) //循环每一个节点
{
if (p->data == data) {
return p; //返回指针域
}
p = p->next;
}
return NULL; //未查询到结果
}
}
void LinkList::InsertElemAtEnd(DataType data){
ElemType *p = head;
ElemType *e;
e = new ElemType;
e ->data = data;
e ->next = NULL;
if (head == NULL)
head = e;
else{
while (p ->next != NULL)
{
p = p->next;
}
p ->next = e;
}
}
//在头部插入指定元素
void LinkList::InsertElemAtHead(DataType data){
ElemType *newdata;
newdata = new ElemType;
newdata ->data = data;
newdata ->next = NULL;
ElemType *p = head;
if (head == NULL)
head = newdata;
else{
newdata ->next = p ->next;
p ->next = newdata;
}
}
//在指定位置插入元素
void LinkList::InsertElemAtIndex(DataType data, int n){
ElemType *newdata = new ElemType;//创建一个新的节点
newdata ->data = data; //定义数据域
newdata ->next = NULL;
ElemType *p = head;
int j=1;
if (n<1 || n>GetLength()) //输入有误报异常
cout << "输入的值错误" << endl;
else {
while (n > j)
{
p = p ->next;
j++;
}
newdata ->next = p ->next;
p ->next = newdata;
}
}
void LinkList::DeleteElemAtEnd(){
ElemType *p = head;
ElemType *ptemp = new ElemType;//创建一个新的节点
ptemp ->next = NULL;
if (p->next == NULL){
cout << "链表为空链表"<< endl;
}else{
while (p ->next != NULL)
{
ptemp = p;
p = p->next;
}
delete p; //删除节点
p = NULL;
ptemp->next = NULL;
}
}
void LinkList::DeleteElemAtHead(){
ElemType *p = head;
if (p->next == NULL){
cout << "链表为空链表"<<endl;
}else{
ElemType *ptemp; //定义两个结点
ElemType *q;
q = p->next;
ptemp = q ->next; //将头结点的下下个节点指向占位节点
p->next= ptemp; //头结点的指针更换
delete q; //删除头结点的下一个节点
q = NULL;
}
}
//删除指定数据
void LinkList::DeleteElemAtIndex(DataType data){
ElemType *p = head;
if (p == NULL || p->next == NULL) { //判断是否为空表,报异常
cout << "该链表为空表" << endl;
}else{
while(p ->next != NULL){
ElemType *ptemp, *q; //定义两个结点
ptemp = p;
p = p ->next;
if (p ->data == data){
q = p;
ptemp ->next = p ->next;
delete q;
q = NULL;
}
}
}
}
void LinkList::DeleteAll()
{
ElemType * p = head->next;
ElemType * ptemp = new ElemType;
while (p != NULL) //在头结点的下一个节点逐个删除节点
{
ptemp = p;
p = p->next;
head->next = p;
ptemp->next = NULL;
delete ptemp;
}
head->next = NULL; //头结点的下一个节点指向NULL
}
int main(){
LinkList list;
list.CreateLinkList(5);
list.TravalLinkList();
cout << endl;
cout << "线性表的长度为:"<< list.GetLength() << endl<<endl;
cout << list.Find(5) << endl;
list.InsertElemAtEnd(9);
list.TravalLinkList();
cout << endl<<endl;
list.InsertElemAtHead(8);
list.TravalLinkList();
cout << endl<<endl;
list.InsertElemAtIndex(6,5);
list.TravalLinkList();
cout << endl<<endl;
list.DeleteElemAtEnd();
list.TravalLinkList();
cout << endl<<endl;
list.DeleteElemAtHead();
list.TravalLinkList();
cout << endl<<endl;
cout << "删除任意位置数据"<< endl;
list.DeleteElemAtIndex(6);
list.TravalLinkList();
cout << endl<<endl;
cout << "删除全部"<< endl;
list.DeleteAll();
list.TravalLinkList();
cout << endl<<endl;
return 0;
}
线性表链表存储结构C++实现
最新推荐文章于 2022-12-30 19:17:20 发布