首先,我们来搞明白几个概念吧(参考自网站数据结构及百度百科)。

  线性表

  线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。在实现线性表数据元素的存储方面,一般可用顺序存储结构链式存储结构两种方法。

  顺序表

  用顺序存储方法存储的线性表简称为顺序表(Sequential List)。顺序表的存储方法是把线性表的结点按逻辑次序依次存放在一组地址连续的存储单元里。

  链表

  链接方式存储的线性表简称为链表(Linked List)。

  顺序表和链表的比较如下:

  "《算法导论》之‘线性表’":基于静态分配的数组的顺序表 

  注:存储密度Storage Density)是指结点数据本身所占的存储量和整个结点结构所占的存储量之比,即“存储密度=(结点数据本身所占的存储量)/(结点结构所占的存储总量)”。

  下文将具体讲述如何实现基于静态分配的数组的顺序表:

  具体算法可以参考网页顺序表上实现的基本运算及Larry Nyhoff的《数据结构与算法分析》。我的做法基本跟他们是一样的,只是我又额外多了一部分:Boost单元测试。对于如何进行Boost单元测试可以参考本人之前写过的一篇文章如何在VS2013中进行Boost单元测试

  我设计的顺序表类如下:

 // seqlist.h
1
#ifndef SEQLIST 2 #define SEQLIST 3 4 #include <iostream> 5 #include <cassert> 6 #include <algorithm> 7 8 using namespace std; 9 10 const int CAPACITY = 1024; 11 typedef int ElementType; 12 13 class SeqList 14 { 15 public: 16 SeqList(); 17 virtual ~SeqList(); 18 bool empty() const; 19 void clear(); 20 bool insert(const int pos, const ElementType val); 21 bool erase(const int pos); 22 void display() const; 23 bool setSeqList(const ElementType *tmpList, const int len); 24 int getLenOfList() const; 25 ElementType getItem(const int pos); 26 ElementType * getSeqList(); // 保留,不推荐使用,因为在使用过程中无法进行越界检查 27 28 private: 29 int lenOfList;         // 线性链表长度 30 ElementType seqList[CAPACITY]; 31 32 }; 33 34 #endif
  1 // seqlist.cpp
  2 #include "seqlist.h"
  3 
  4 SeqList::SeqList()
  5 {
  6     // initialization
  7     lenOfList = 0;
  8     fill(seqList, seqList + CAPACITY - 1, 0);
  9     // memset(SeqList, 1, CAPACITY*sizeof(int));
 10 }
 11 
 12 SeqList::~SeqList()
 13 {
 14 
 15 }
 16 
 17 bool SeqList::empty() const
 18 {
 19     return lenOfList == 0;
 20 }
 21 
 22 void SeqList::clear()
 23 {
 24     lenOfList = 0;
 25     fill(seqList, seqList + CAPACITY - 1, 0);
 26 }
 27 
 28 bool SeqList::insert(const int pos, const ElementType val)
 29 {
 30     bool success = false;
 31     // assert(lenOfList != CAPACITY);    // 这里的assert分成两行写,是为了方便定位错误发生的地方
 32     // assert(0 <= pos <= lenOfList);
 33     if (lenOfList == CAPACITY)
 34     {
 35         cerr << "No space for insertion!" << endl;
 36     }
 37     else if (pos < 0 || pos > lenOfList)
 38     {
 39         cerr << "The position " << pos << 
 40             " you want to insert is less than zero or exceeds the length of the list!" << endl;
 41         throw out_of_range("throw out_of_range");    // 抛出一个越界异常
 42     }
 43     else
 44     {
 45         int tmpCount = lenOfList - pos;
 46         for (int i = 0; i < tmpCount; i++)
 47         {
 48             seqList[lenOfList - i] = seqList[lenOfList - i - 1];
 49         }
 50         seqList[pos] = val;
 51         lenOfList++;
 52         success = true;
 53     }
 54     return success;
 55 }
 56 
 57 bool SeqList::erase(const int pos)
 58 {
 59     bool success = false;
 60     // assert(lenOfList != 0);
 61     // assert(0 <= pos <= lenOfList);
 62     if (lenOfList == 0)
 63     {
 64         cerr << "There is no elements in the list!" << endl;
 65     }
 66     else if (pos < 0 || pos > lenOfList)
 67     {
 68         cerr << "The position " << pos << 
 69             " you want to erase is less than zero or exceeds the length of the list!" << endl;
 70         throw out_of_range("throw out_of_range");    // 抛出一个越界异常
 71     }
 72     else
 73     {
 74         int tmp = lenOfList - pos;
 75         for (int i = 0; i < tmp - 1; i++)
 76         {
 77             seqList[pos + i] = seqList[pos + i + 1];
 78         }
 79         seqList[lenOfList - 1] = 0;
 80         lenOfList--;
 81         success = true;
 82     }
 83     return success;
 84 }
 85 
 86 void SeqList::display() const
 87 {
 88     cout << "***Start Displaying***" << endl;
 89     if (lenOfList == 0)
 90     {
 91         cerr << "There is no element in the the list!" << endl;
 92     }
 93     else
 94     {
 95         for (int i = 0; i < lenOfList; i++)
 96         {
 97             cout << i << " : " << seqList[i] << endl;
 98         }
 99         cout << "***End Displaying***" << endl;
100     }
101 }
102 
103 bool SeqList::setSeqList(const ElementType *tmpList, const int len)
104 {
105     // assert(len <= CAPACITY);
106     bool success = false;
107     if (len <= CAPACITY)
108     {
109         for (int i = 0; i < len; i++)
110         {
111             seqList[i] = *(tmpList++);
112         }
113         lenOfList = len;
114         success = true;
115     }
116     else
117     {
118         cerr << "The length of the array you set exceeds the CAPACITY." << endl;
119         throw out_of_range("throw out_of_range");    // 抛出一个越界异常
120     }
121     return success;
122 }
123 
124 int SeqList::getLenOfList() const
125 {
126     return lenOfList;
127 }
128 
129 ElementType SeqList::getItem(const int pos)
130 {
131     // assert(0 <= pos <= lenOfList);
132     if (pos < 0 || pos > lenOfList)
133     {
134         cerr << "The item at " << pos << " you want to get does not exist!" << endl;
135         throw out_of_range("throw out_of_range");    // 抛出一个越界异常
136     }
137     else
138     {
139         return seqList[pos];
140     }
141 }
142 
143 ElementType * SeqList::getSeqList()
144 {
145     return seqList;
146 }
seqlist.cpp

相关文章:

  • 2021-12-30
  • 2021-12-21
  • 2021-06-14
  • 2021-04-18
  • 2021-06-20
  • 2021-10-15
  • 2021-10-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-22
相关资源
相似解决方案