使用C++实现顺序链表

 更新时间:2020年4月25日 17:27  点击:1525

这是创建的LIst.h头文件

#ifndef LIST_H
#define LIST_H
class List
{
public:
List(int size);
~List();
void DestroyList();
void ClearList();
bool ListEmpty();
int ListLength();
bool GetElem(int i, int *e);
int LocateElem(int *e);
bool ListInsert(int i, int *e);
bool ListDelete(int i, int *e);
bool PriorElem(int *currentElem, int *preElem);
bool NextElem(int *currentElem, int *NextElem);
void ListTraverse();
private:
int *m_pList;
int m_iSize;
int m_iLength;
};
#endif

创建的list.cpp文件

#include"LIst.h"
#include<iostream>
using namespace std;
List::List(int size)
{
m_iSize = size;
m_pList = new int[m_iSize]; //申请堆内存 
m_iLength = 0;
}
List::~List()
{
delete []m_pList;
m_pList = 0; //不为NULL就等于0;
}
void List::DestroyList()
{
m_iLength=0;
}
void List::ClearList()
{
m_iLength = 0;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
else
return false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem(int i, int *e)
{
if (i<0 || i>=m_iLength)
{
return false;
}
else
{
*e = m_pList[i];
}
}
int List::LocateElem(int *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (*e == m_pList[i])
return i;
}
return -1;
}
bool List::ListInsert(int i, int *e)
{
if (i<0 || i>m_iLength)
{
return false;
}
else 
{
for (int k = m_iLength - 1; k >= i; k--)
{
m_pList[k + 1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;
}
}
bool List::ListDelete(int i, int *e)
{
if (i<0 || i>m_iLength)
{
return false;
}
else
{
for (int k = i; k < m_iLength - 1; k++)
{
m_pList[k ]=m_pList[k + 1]; //直接覆盖了前面的元素 
}
m_iLength--;
}
}
bool List::PriorElem(int *currentElem, int *preElem)
{
/*for (int i = 0; i < m_iLength; i++)
{
if (*currentElem == m_pList[i])
*preElem = m_pList[i - 1];
}
return true;*/   //错误
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem = m_pList[temp - 1];
return true;
}
}
}
bool List::NextElem(int *currentElem, int *NextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == m_iLength)
{
return false;
}
else
{
*NextElem= m_pList[temp + 1];
return true;
}
}
}
void List::ListTraverse()
{
for (int i = 0; i < m_iLength; i++)
{
cout << m_pList[i]<<endl;
}
}

这是测试函数ListMain.cpp

#include<iostream>
#include"LIst.h"
/*
线性表--顺序表 以这个为例
3 5 7 2 9 1 8
bool InitList();   //创建线性表
void DestroyList(); //销毁线性表
void ClearList();  //清空线性表
bool ListEmpty(); //判断线性表是否为空
int ListLength(); //获取线性表的长度
bool GetElem(int i,Elem *e);//获取指定的元素
int LocateElem(Elem *e);//寻找第一个满足e的数据元素的位序
bool PriorElem(Elem *currentElem,Elem *preElem); //获取指定元素的前驱
bool NextElem(Elem *currentElem,Elem *NextElem);//获取指定元素的后继
bool ListInsert(int i,Elem *e); //在第i个插入元素
bool ListDelete(int i,Elem *e); //删掉第i个位置的元素
void ListTraverse();  //遍历线性表
*/
using namespace std;
int main()
{//3 5 7 2 9 1 8
int temp = 0;
int e1 = 3;
int e2 = 5;
int e3 = 7;
int e4 = 2;
int e5 = 9;
int e6 = 1;
int e7 = 8;
List *list = new List(10);
//cout << list->ListLength() << endl;
list->ListInsert(0, &e1);
//cout << list->ListLength() << endl;
list->ListInsert(1, &e2);
//cout << list->ListLength() << endl;
list->ListInsert(2, &e3);
list->ListInsert(3, &e4);
list->ListInsert(4, &e5);
list->ListInsert(5, &e6);
list->ListInsert(6, &e7);
list->ListTraverse();
/*list->GetElem(2,&temp);
cout <<"temp="<< temp << endl;
temp = 5;
cout << list->LocateElem(&temp) << endl;*/
list->PriorElem(&e4,&temp);
cout << "temp=" << temp << endl;
list->NextElem(&e4, &temp);
cout << "temp=" << temp << endl;
/*list->ListDelete(0, &temp);
if (!list->ListEmpty())
{
cout << "not empty!"<< endl;
}
else
{
cout << "empty!" << endl;
}
list->ClearList();
if (!list->ListEmpty())
{
 cout << "not empty!" << endl;
}
 else
{
 cout << "empty!" << endl;
}
cout << "#" << temp << endl;*/
//list->ListTraverse();
//cout << m_iLength << endl; //错误
delete list;
system("pause");
return 0;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对猪先飞的支持。如果你想了解更多相关内容请查看下面相关链接

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • c++优先队列(priority_queue)用法详解

    这篇文章主要介绍了c++优先队列(priority_queue)用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25