码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构_顺序表

时间:2017-09-02 14:20:54      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:test   else   结构   测试文件   ble   ring   pil   over   seq   

线性表的顺序存储是指在内存中用地址连续的一块存储空间顺序存放线性表的各元素,用这种存储形式存储的线性表称为顺序表。

顺序表实现的头文件SeqList.h:

  1 #ifndef SEQLIST_H
  2 #define SEQLIST_H
  3 
  4 #include <iostream>
  5 
  6 using namespace std;
  7 
  8 template<class T>
  9 class SeqList
 10 {
 11 public:
 12     SeqList(int size = 3);
 13     ~SeqList();
 14     int getSLSize();
 15     int getCurrentSize();
 16     bool isEmpty();
 17     T get(int index);
 18     bool slInsert(int index, T t);
 19     bool slDelete(int index);
 20 private:
 21     T *pdata;
 22     int size;
 23     int last;
 24 };
 25 
 26 template<class T>
 27 SeqList<T>::SeqList(int size)
 28 {
 29     pdata = new T[size];
 30     this->size = size;
 31     last = -1;
 32 }
 33 
 34 template<class T>
 35 SeqList<T>::~SeqList()
 36 {
 37     delete [] pdata;
 38     pdata = NULL;
 39 }
 40 
 41 template<class T>
 42 int SeqList<T>::getSLSize()
 43 {
 44     return this->size;
 45 }
 46 
 47 template<class T>
 48 int SeqList<T>::getCurrentSize()
 49 {
 50     return this->last;
 51 }
 52 
 53 template<class T>
 54 bool SeqList<T>::isEmpty()
 55 {
 56     if(last == -1) return true;
 57     else return false;
 58 }
 59 
 60 template<class T>
 61 T SeqList<T>::get(int index)
 62 {
 63     if(index < 0 || index > last)
 64     {
 65         cout << "Get value failed! The index is out of bound!" << endl;
 66 //        return ;
 67     }
 68 
 69     return pdata[index];
 70 }
 71 
 72 //Element t is at index(start from 0) after inserting
 73 template<class T>
 74 bool SeqList<T>::slInsert(int index, T t)
 75 {
 76     if(last == size - 1)
 77     {
 78         cout << "Table is full!" << endl;
 79         return false;
 80     }
 81 
 82     if(index < 0 || index > last + 1)
 83     {
 84         cout << "Location error!" << endl;
 85         return false;
 86     }
 87 
 88     //If the table is empty
 89     if(last == -1)
 90     {
 91         pdata[0] = t;
 92         last++;
 93         return true;
 94     }
 95 
 96     //If the location is at last
 97     if(index == last + 1)
 98     {
 99         pdata[index] = t;
100         last++;
101         return true;
102     }
103 
104     //For common situation
105     for(int j = last; j >= index; j--)
106     {
107         pdata[j + 1] = pdata[j];
108     }
109     pdata[index] = t;
110     last++;
111     return true;
112 }
113 
114 template<class T>
115 bool SeqList<T>::slDelete(int index)
116 {
117     if(index < 0 || index > last)
118     {
119         cout << "Location error!" << endl;
120         return false;
121     }
122 
123     if(last == -1)
124     {
125         cout << "Table is empty!" << endl;
126         return false;
127     }
128 
129     //If the location is at last
130     if(index == last)
131     {
132         last--;
133         return true;
134     }
135 
136     //For common situation
137     for(int j = index; j < last; j++)
138     {
139         pdata[j] = pdata[j + 1];
140     }
141     last--;
142     return true;
143 }
144 
145 #endif

顺序表的测试文件TSeqList.cpp:

  1 #include <iostream>
  2 #include <SeqList.h>
  3 #include <string>
  4 
  5 using namespace std;
  6 
  7 struct Student
  8 {
  9     string name;
 10     int id;
 11 };
 12 
 13 class A
 14 {
 15 public:
 16     A()
 17     {
 18         f = 0;
 19     }
 20     void setv(int v = 1);
 21     int f;
 22 };
 23 
 24 //void A::setv(int v = 3) //Compile error
 25 void A::setv(int v)
 26 {
 27     this->f = v;
 28 }
 29 
 30 void ObjectTest()
 31 {
 32     Student stu[] =
 33     {
 34         {"Sky", 11},
 35         {"Rose", 12},
 36         {"Jack", 13},
 37     };
 38 
 39     cout << endl << "----object test----" << endl;
 40     SeqList<Student> *pslist = new SeqList<Student>();
 41     cout << "size = " << pslist->getSLSize() << endl;
 42 
 43     cout << "Check location:" << endl;
 44     pslist->slInsert(-1, stu[0]);
 45 
 46     cout << "Insert when table is empty" << endl;
 47     pslist->slInsert(0, stu[0]);
 48 
 49     cout << "Insert at last" << endl;
 50     pslist->slInsert(1, stu[1]);
 51 
 52     cout << "Insert at first" << endl;
 53     pslist->slInsert(0, stu[2]);
 54 
 55     Student p;
 56     int cuSize = pslist->getCurrentSize();
 57     for(int i = 0; i <= cuSize; i++)
 58     {
 59         p = pslist->get(i);
 60         cout << "pslist[" << i << "] = " << p.name << ", " << p.id << endl;
 61     }
 62 
 63     cout << "Check overflow:" << endl;
 64     pslist->slInsert(0, stu[0]);
 65 
 66     cout << "Check deleting location:" << endl;
 67     pslist->slDelete(3);
 68 
 69     cout << "Delete the first one:" << endl;
 70     pslist->slDelete(0);
 71     cuSize = pslist->getCurrentSize();
 72     for(int i = 0; i <= cuSize; i++)
 73     {
 74         p = pslist->get(i);
 75         cout << "pslist[" << i << "] = " << p.name << ", " << p.id << endl;
 76     }
 77 
 78     cout << "Check deleting the last one" << endl;
 79     pslist->slDelete(1);
 80     cuSize = pslist->getCurrentSize();
 81     for(int i = 0; i <= cuSize; i++)
 82     {
 83         p = pslist->get(i);
 84         cout << "pslist[" << i << "] = " << p.name << ", " << p.id << endl;
 85     }
 86 
 87     int k = 0;
 88     while(!pslist->isEmpty())
 89     {
 90         pslist->slDelete(0);
 91         k++;
 92     }
 93     cout << "After deleting size = " << k << endl;
 94 
 95     A a;
 96     a.setv();
 97 //    a.setv(6);
 98     cout << "f = " << a.f << endl;
 99 
100     delete pslist;
101     return;
102 }
103 
104 int main()
105 {
106     ObjectTest();
107     return 0;
108 }

输出结果:

 1 ----object test----
 2 size = 3
 3 Check location:
 4 Location error!
 5 Insert when table is empty
 6 Insert at last
 7 Insert at first
 8 pslist[0] = Jack, 13
 9 pslist[1] = Sky, 11
10 pslist[2] = Rose, 12
11 Check overflow:
12 Table is full!
13 Check deleting location:
14 Location error!
15 Delete the first one:
16 pslist[0] = Sky, 11
17 pslist[1] = Rose, 12
18 Check deleting the last one
19 pslist[0] = Sky, 11
20 After deleting, size = 1
21 f = 1

顺序表插入操作注意事项:

1)判断表是否满,若满了不作插入;

2)检验插入位置的有效性;

3)若为空表,直接插入首位置;

4)若在表末端插入,无需移动元素。

顺序表删除操作注意事项:

1)判断表是否为空;

2)检验删除位置的有效性;

3)若删除最后一个元素,则无需移动其它元素。

 

数据结构_顺序表

标签:test   else   结构   测试文件   ble   ring   pil   over   seq   

原文地址:http://www.cnblogs.com/pursuiting/p/7466483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!