C++实现线性表之顺序表
2017-02-06 23:03
471 查看
C++实现线性表之顺序表
List.h#ifndef LIST_H #define LIST_H #include <iostream> using namespace std; 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 ¤tElem, int &preElem); //获取指定元素的前驱 bool NextElem(int ¤tElem, int &nextElem); //获取指定元素的后继 bool ListInsert(int n, int &e); //在第n个位置上插入元素 bool ListDelete(int n, int &e); //删除第n个位置上的元素 void ListTraverse(); //便利线性表 private: int *m_pList; int m_iSize; int m_iLength; }; 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; } else { e = m_pList[i]; return true; } } int List::LocateElem(int &e) { for (size_t i = 0; i < m_iLength; i++) { if (e == m_pList[i]) { return i; } } return -1; } bool List::PriorElem(int ¤tElem, int &preElem) { int temp = LocateElem(currentElem); if (temp != -1) { if (temp != 0) { preElem = m_pList[temp - 1]; return true; } else { return false; } } else { return false; } } bool List::NextElem(int ¤tElem, int &nextElem) { int temp = LocateElem(currentElem); if (temp != -1) { if (temp != m_iLength - 1) { nextElem = m_pList[temp + 1]; return true; } else { return false; } } else { return false; } } void List::ListTraverse() { for (size_t i = 0; i < m_iLength; i++) { cout << m_pList[i] << endl; } } bool List::ListInsert(int n, int &e) { if (n < 0 || n > m_iLength) { return false; } else { for (int i = m_iLength - 1; i >= n ; i--) { m_pList[i + 1] = m_pList[i]; } m_pList = e; m_iLength++; return true; } } bool List::ListDelete(int n, int &e) { if (n < 0 || n >= m_iLength) { return false; } else { e = m_pList ; for (size_t i = n + 1; i < m_iLength; i++) { m_pList[i - 1] = m_pList[i]; } m_iLength--; return true; } } #endif // !LIST_H
main.cpp
#include "List.h" int main() { List *list1 = new List(10); int e = 1; int a = 2; int b = 3; int c = 4; int d = 5; int f = 6; int g = 7; int h = 0; list1->ListInsert(0, e); list1->ListInsert(1, a); list1->ListInsert(2, b); list1->ListInsert(3, c); list1->ListInsert(4, d); list1->ListInsert(5, f); list1->ListInsert(3, g); //list1->ClearList(); list1->ListTraverse(); list1->GetElem(2, h); list1->NextElem(d, h); cout << "$" << h << " "<<list1->ListLength() << endl; delete list1; list1 = NULL; system("pause"); return 0; }
相关文章推荐
- 顺序线性表的c++实现
- 线性表的顺序存储实现c++
- 数据结构(3)线性表之顺序表C++实现
- C++实现顺序结构线性表的基本操作
- 线性表的顺序表示实现-C++版
- 线性表的顺序表示实现-C++版
- 线性表的顺序表示与实现(C++版)
- 顺序存储线性表的C++实现——严蔚敏版《数据结构》
- c++模板类实现线性表顺序存储
- 线性表顺序存储(C++ 实现)
- 线性表之顺序表C++实现
- c++实现线性表的顺序结构
- 线性表---顺序存储实现
- 顺序栈的定义、初始化、出栈、入栈等操作 C++代码实现
- 数据结构之顺序表C++实现
- 线性表的顺序储存及运算实现
- 二叉树用顺序表实现 C++代码实现
- 线性表的顺序表示和实现
- 线性表的顺序实现(C语言)
- 线性表顺序实现