慕课网学习笔记之数据机构线性表一顺序表(C++)

来源:互联网 发布:免费的cms 编辑:程序博客网 时间:2024/05/19 13:06

什么是线性表?——线性表是n个元素的优先序列。


顺序表的优点
支持随机访问,访问速度快
顺序表的缺点
插入和删除需要大量的移动操作

/************************************************************//*线性表-顺序表    3 5 7 2 9 1 8*/ #ifndef LIST_H#define LIST_Hclass 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);          //在第i个位置插入元素     bool ListDelete(int i,int *e);          //删除第i个位置的元素 private:    int  *m_pList;    int  m_iSize;           int  m_iLength;//当前长度 };#endif
// List.cpp : 定义控制台应用程序的入口点。//#include<iostream>#include"List.h"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 (0 == m_iLength)        return true;    else        return 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 (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 (-1 == temp)        return false;    else    {        if (0 == temp)            return false;        else        {            *preElem = m_pList[temp - 1];            return true;        }    }}bool List::NextElem(int *currentElem, int *nextElem){    int  temp = LocateElem(currentElem);    if (-1 == temp)        return false;    else    {        if ((m_iLength - 1) == temp)            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;}

以上两段代码分别为List.h文件和List.cpp文件。
代码的验证在以下domo.cpp文件中

#include<stdlib.h>#include<iostream>#include"List.h"using namespace std;int main(){    //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);    //  cout <<"#"<<list1->ListLength()<<endl;    list1->ListDelete(1,&temp);    list1->ListTraverse();    //  cout << "#"<<temp<<endl;    //  list1->ListDelete(e1,&temp);    delete list1;    list1=NULL;     return 0;}

顺序表是比较简单的,可以认为就是个连续存储的数组。数组下表就是索引,在执行插入和删除时,要注意分别进行判满和判空操作,而在操作时注意整个表的顺序,注意是怎样移动的,插入就要先按次序向后依次移动,应该先移走尾部,向后覆盖,而删除则相反,先取出要删除的元素,然后向前覆盖。

0 0
原创粉丝点击