#include
#include
using namespace std;
class List
{
public:
List(int size)
{
m_iSize = size;
m_pList = new int[m_iSize];
m_iLength = 0;
} ~List()
{
delete[]m_pList;
m_pList = NULL;
} void ClearList()
{
m_iLength = 0;
} bool ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
else
{
return false;
}
} int ListLength()
{
return m_iLength;
} bool GetElem(int i, int* e)
{
if (i < 0 || i >= m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
} int LocateElem(int* e)
{
for (int i = 0;
i < m_iLength;
i++)
{
if (m_pList[i] == *e)
{
return i;
}
}
return -1;
} bool 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 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 ListTraverse()
{
for (int i = 0;
i < m_iLength;
i++)
{
cout << m_pList[i] << endl;
}
} bool 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 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;
}private:
int* m_pList;
int m_iSize;
int m_iLength;
};
int main(void)
{
//3 5 7 2 9 1 8
int e1 = 3;
int e2 = 5;
int e3 = 7;
int e4 = 2;
int e5 = 9;
int e6 = 1;
int e7 = 8;
int temp = 0;
List *list1 = new List(10);
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;
delete list1;
system("pause");
return 0;
}
【数据结构——线性表(c 实现)】
推荐阅读
- 笔记|C语言数据结构——二叉树的顺序存储和二叉树的遍历
- C语言学习(bit)|16.C语言进阶——深度剖析数据在内存中的存储
- 数据结构和算法|LeetCode 的正确使用方式
- 先序遍历 中序遍历 后序遍历 层序遍历
- 数据结构|C++技巧(用class类实现链表)
- 数据结构|贪吃蛇代码--c语言版 visual c++6.0打开
- 算法|算法-二分查找
- 数据结构学习指导|数据结构初阶(线性表)
- leetcode题解|leetcode#106. 从中序与后序遍历序列构造二叉树
- java|ObjectOrientedProgramming - 面向对象的编程(多态、抽象类、接口)- Java - 细节狂魔