今天来到数据结构的线性表啦,学习就是要敲代码哦,加油!
依旧还是C++实现的,记录代码方便以后学习
就开始吧~
1、List.h
#ifndef LIST_H
#define LISH_H
class List
{
public:
List(int size);
~List();
void ClearList(); //清空线性表
bool ListEmpty(); //判空
int ListLength(); //获取长度
bool GetElem(int i, int *e); //获取指定元素
int LocateElem(int *e); //找到第一个满足e数据的位序
bool PriorElem(int *currentElem, int *preElem);
bool NextElem(int *currentElem, int *nextElem);
void ListTraverse();//遍历
bool ListInsert(int i, int *e);
bool ListDelete(int i, int *e);
private:
int *m_pList;
int m_iSize;
int m_iLength; //当前线性表已经放入的元素个数
};
#endif
2、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 = NULL;
}
void List::ClearList()
{
m_iLength = 0;
}
bool List::ListEmpty()
{
if(m_iLength == 0)
return true;
else
return false;
// return m_iLength == 0 ? true : false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem(int i, int *e)
{
if (i<0 || i>=m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
}
int List::LocateElem(int *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (m_pList[i] == *e)
{
return i;
}
}
return -1;
}
bool List::PriorElem(int *currentElem, int *preElem)
{
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 - 1)
{
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;
}
}
bool List::ListInsert(int i, int *e)
{
if (i < 0 || i > m_iLength)
{
return false;
}
//先移动,后插入
for (int k = m_iLength - 1; k >= i; k--)
{
m_pList[k+1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;
return true;
}
bool List::ListDelete(int i, int *e)
{
if (i < 0 || i >= m_iLength)
{
return false;
}
//先删除,后插入
*e = m_pList[i];
for (int k = i + 1; k < m_iLength; k++)
{
m_pList[k-1] = m_pList[k];
}
m_iLength--;
return true;
}
3、测试代码demo
注意,测试代码里面注释的部分其实是可以运行的,只是在测试不同函数时为了方便查看效果,就把其他部分代码注释了,如果去掉注释应该也是能运行成功的,如果你能看清结果的话。
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "List.h"
int main(int argc, char** argv)
{
//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 *list1 = new List(10);
// cout << "length: "<< list1->ListLength() << endl;
//
// list1->ListInsert(0, &e1);
//
// cout << "length: "<< list1->ListLength() << endl;
list1->ListInsert(0, &e1);
list1->ListInsert(1, &e2);
list1->ListInsert(2, &e3);
list1->ListInsert(3, &e4);
list1->ListInsert(4, &e5);
list1->ListInsert(5, &e6);
list1->ListInsert(6, &e7);
list1->ListTraverse();
list1->PriorElem(&e4, &temp);
cout << "temp: " << temp << endl;
list1->NextElem(&e4, &temp);
cout << "temp: " << temp << endl;
// list1->GetElem(0, &temp);
// cout << "temp: " << temp << endl;
//
// temp = 5;
//
// cout << list1->LocateElem(&temp) << endl;
// list1->ListDelete(0, &temp);
// if(!list1->ListEmpty())
// {
// cout << "not empty" << endl;
// }
//
// list1->ClearList();
//
// if(list1->ListEmpty())
// {
// cout << "empty" << endl;
// }
//
// list1->ListTraverse();
//
// cout << "#" << temp << endl;
//
delete list1;
list1 = NULL;
return 0;
}