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

线性表的实现和运用

时间:2019-10-10 22:24:42      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:null   构造函数   max   position   mys   iostream   for   nbsp   复制   

摘要:本文主要是线性表顺序存储结构的具体实现和运用,具体实现了线性表类,并且做了相应的测试。

Sqlist.h

 1 #ifndef _SQLIST_
 2 #define _SQLIST_
 3 
 4 template<class ElemType>
 5 class Sqlist {
 6 public:
 7     Sqlist(int size);   //构造函数
 8     virtual ~Sqlist();   //析构函数
 9     int Lence();   //返回线性表的长度
10     void Mypush_back(ElemType elem);  //向线性表中添加元素
11     void Myshow();  //线性表的遍历
12     bool Empty();  //判断线性表是否为空
13     void Clear();  //清空线性表
14     bool Getelem(int position,ElemType &e);  //检查线性表某个位置是否存在值,如果存在,就将该值传递给e值
15     bool Setelem(int position,ElemType &e);  //将线性表的某个位置的元素设置为e值
16     bool Insert(int position,ElemType &e);  //在线性表的某个位置插入一个元素
17     bool Delete(int position, ElemType &e);  //删除线性表某一个位置的元素
18     Sqlist(const Sqlist<ElemType> &copy);   //拷贝构造函数
19     Sqlist<ElemType>&operator=(const Sqlist<ElemType> &copy);
20 
21 protected:
22     int count;   //实际元素个数
23     int maxSize;  //最大元素个数
24     ElemType *elems;   //线性表的名称
25 };
26 
27 #endif

 

Sqlist.cpp

  1 #include "Sqlist.h"
  2 #include<iostream>
  3 
  4 using namespace std;
  5 
  6 template<class ElemType>
  7 Sqlist<ElemType>::Sqlist(int size) {
  8     count = 0;
  9     maxSize = size;
 10     elems = new ElemType[maxSize];
 11 }
 12 
 13 template<class ElemType>
 14 Sqlist<ElemType>::~Sqlist() {
 15     delete[] elems;
 16 }
 17 
 18 template<class ElemType>
 19 int Sqlist<ElemType>::Lence() {
 20     return count;
 21 }
 22 
 23 template<class ElemType>
 24 void Sqlist<ElemType>::Mypush_back(ElemType elem) {
 25     if(count<maxSize){ 
 26     elems[count] = elem;
 27     count++;
 28     }
 29 }
 30 
 31 template<class ElemType>
 32 void Sqlist<ElemType>::Myshow() {
 33     if (count > 0) {
 34     for (int i=0;i<Lence();i++)
 35     {
 36         cout << elems[i] << endl;
 37     }
 38     }
 39     else {
 40         cout << "线性表中没有元素" << endl;
 41     }    
 42 }
 43 
 44 template<class ElemType>
 45 bool Sqlist<ElemType>::Empty() {
 46     return count == 0;
 47 }
 48 
 49 template<class ElemType>
 50 void Sqlist<ElemType>::Clear() {
 51     count = 0;
 52 }
 53 
 54 template<class ElemType>
 55 bool Sqlist<ElemType>::Getelem(int position, ElemType &e) {
 56     if (position<1 || position>Lence()) {
 57         return false;
 58     }
 59     else {
 60         e = elems[position-1];
 61         return true;
 62     }
 63 }
 64 
 65 template<class ElemType>
 66 bool Sqlist<ElemType>::Setelem(int position, ElemType &e) {
 67     if (position<1 || position>Lence()) {
 68         return false;
 69     }
 70     else {
 71         elems[position - 1]=e;
 72         return true;
 73     }
 74 }
 75 
 76 template<class ElemType>
 77 bool Sqlist<ElemType>::Insert(int position, ElemType &e) {
 78     ElemType tmp;
 79     if (count == maxSize) {
 80         return false;
 81     }
 82     else if (position<1 || position>Lence()+1) {
 83         return false;
 84     }
 85     else {
 86         count++;
 87         for (int pos = Lence(); pos >= position;pos--) {
 88             Getelem(pos,tmp);
 89             Setelem(pos+1,tmp);
 90         }
 91         Setelem(position,e);
 92         return true;
 93     }
 94 }
 95 
 96 template<class ElemType>
 97 bool Sqlist<ElemType>::Delete(int position,ElemType &e) {
 98     ElemType tmp;
 99     if (position<1 || position>Lence()+1) {
100         return false;
101     }
102     else {
103         Getelem(position,e);
104         for (int pos = position + 1; pos < Lence() + 1;pos++) {
105             Getelem(pos,tmp);
106             Setelem(pos-1,tmp);
107         }
108     }
109 }
110 
111 template<class ElemType>
112 Sqlist<ElemType>::Sqlist(const Sqlist<ElemType> &copy) {
113     maxSize = copy.maxSize;
114     elems = new ElemType[maxSize];
115     count = copy.count;
116     for (int pos = 1; pos <= Lence();pos++) {
117         elems[pos-1] = copy.elems[pos-1];
118     }
119 }
120 
121 template<class ElemType>
122 Sqlist<ElemType>& Sqlist<ElemType>::operator=(const Sqlist<ElemType> &copy) {
123     //此处判断是为了防止新的对象和被复制对象处于同一个内存空间
124     if (&copy!=this) {
125         maxSize = copy.maxSize;
126         count = copy.count;
127         if (elems!=NULL) {  //释放存储空间
128             delete[] elems;
129         }
130         elems = new ElemType[maxSize];
131     }
132     for (int pos = 1; pos <= Lence(); pos++) {
133         elems[pos - 1] = copy.elems[pos - 1];
134     }
135     return *this;
136 }

main.cpp

 1 #include<iostream>
 2 #include "Sqlist.cpp"
 3 #include<string>
 4 
 5 
 6 using namespace std;
 7 
 8 void test() {
 9     Sqlist<string> sqlist(5);
10     sqlist.Mypush_back("abc");
11     sqlist.Mypush_back("qyw");
12     sqlist.Mypush_back("shda123");
13     sqlist.Mypush_back("gdsha");
14     sqlist.Mypush_back("ashaj");
15     //string a = "bughf";
16     //sqlist.Insert(2, a);
17     //sqlist.Clear();
18     //cout << sqlist.Lence() << endl;
19     sqlist.Myshow();
20     //string c;
21     //sqlist.Delete(2,c);
22     //sqlist.Myshow();
23     //cout << sqlist.Empty() << endl;
24     //cout << a << endl;
25     cout << "----------------------------------" << endl;
26     Sqlist<string> list(sqlist);
27     list.Myshow();
28     Sqlist<string> s(5);
29     s = sqlist;
30     cout << "----------------------------------" << endl;
31     s.Myshow();
32 }
33 
34 int main() {
35     test();
36 
37     system("pause");
38     return 0;
39 }

线性表的实现和运用

标签:null   构造函数   max   position   mys   iostream   for   nbsp   复制   

原文地址:https://www.cnblogs.com/lzy820260594/p/11650658.html

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