顺序线性表的c++实现

来源:互联网 发布:洮南幼儿带网络监控的 编辑:程序博客网 时间:2024/05/23 11:31
//第一次发代码啊,新手而已
<span style="color:#ff0000;">//list.h</span>
#ifndef LIST_H#define LIST_Hclass List{public:List();//创建最大长度为100的线性表List(int size);//创建最大长度为size的线性表'~List();//删除线性表void CreatList(int length);//向线性表输入length个元素void GetList()const;//查看线性表中的元素bool ListEmpty();//检查线性表是否为空int ListLength()const;//返回线性表的长度int GetElem(int i, int &e)const;//返回下标为i的元素int LocateElem(int e);//检查是否有与e相同的元素,如有则返回下标,若无则返回-1int PriorElem(int cur_e, int &pre_e)const;//返回cur_e的前一个元素int NextElem(int cur_e, int &next_e)const;//返回cur_e的后一个元素void ListInsert(int index,int e);//在下标为index的元素前插入eint LiseDelete(int index,int &e);//删除下标为index的元素并返回该元素void MergeList(List la, List lb);//使用前需创建一个listsize=la.length+lb.length的lcprivate:int *p;//数组int length;//当前长度int listsize;//最大长度};#endif<pre class="cpp" name="code"> 
 
 
 
 
 
 
 
 
<span style="color:#ff0000;">//主函数</span>#include "stdafx.h"#include<iostream>#include<iomanip>#include"List.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){List la(2);List lb(3);int a;la.ListInsert(-1, 1);la.CreatList(2);lb.CreatList(3);List lc(6);lc.MergeList(la, lb);int e;lc.GetElem(0,e);la.ListInsert(1, 2);la.GetList();lb.GetList();lc.GetList();cout << setw(3)<<e<<endl;la.~List();lb.~List();lc.~List();system("pause");return 0;}
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;"></span> 
<span style="color:#ff0000;">//list.cpp</span>#include "stdafx.h"#include<iostream>#include<iomanip>#include<cassert>#include"List.h"using  namespace std;List::List(){p = new int[100];for (int i = 0; i < 100; i++)p[i] = 'null';length = 0;listsize = 100;}List::List(int a){p = new int[a];for (int i = 0; i < a; i++)p[i] = 'null';length = 0;listsize = a;}List::~List(){}void List::CreatList(int length){assert(length <= listsize);this->length = length;cout << "请从小到大输入"<<endl;for (int i = 0; i < length; i++){cout << "请输入第" << i << "个元素:";cin >> p[i];}}void List::GetList() const{for (int i = 0; i < length; i++)cout << setw(3) << p[i];cout << endl;}bool List::ListEmpty(){if (p[0] == 'null')return true;elsereturn false;}int List::ListLength() const{return length;}int List::GetElem(int i, int &e) const{e = p[i];return e;}int List::LocateElem(int e){for (int i = 0; i < length; i++){if (p[i] == e)return i;}return -1;}int List::PriorElem(int cur_e, int &pre_e) const{assert(p[0] == cur_e);for (int i = 1; i < length; i++){if (p[i] == cur_e){pre_e = p[i - 1];return pre_e;}}}int List::NextElem(int cur_e, int &next_e) const{assert(p[length - 1] == cur_e);for (int i = 0; i < length - 1; i++){if (p[i] == cur_e){next_e = p[i + 1];return next_e;}}}void List::ListInsert(int index, int e){if (index > length || index<0){cout << "下标不在线性表有效范围之内" << endl;cout << "有效下标为:" << 0 << "到" << length << endl;return;}if (length == listsize){int *q = new int(listsize + 1);for (int i = 0; i<length; i++)q[i] = p[i];p = q;listsize += 1;}for (int i = length - 1; i >=index; i--)*(p + i+1)=*(p+i);*(p + index) = e;length += 1;}int List::LiseDelete(int index, int &e){assert(index >= length&&index < 0);e = *(p + index);for (int i = index; i < length; i++)*(p + i) = *(p + i + 1);length -= 1;return e;}void List::MergeList(List la, List lb){int i = 0, j = 0;int k;for (k=0; i < la.length&&j < lb.length;k++){if (la.p[i] <= lb.p[j]){p[k] = la.p[i];i++;}else{p[k] = lb.p[j];j++;}}while (i < la.length){p[k] = la.p[i];i++;k++;}while (j < lb.length){p[k] = lb.p[j];j++;k++;}length = la.length + lb.length;}


0 0