顺序线性表的c++实现
2015-03-17 23:30
344 查看
//第一次发代码啊,新手而已
<span style="color:#ff0000;">//list.h</span>
#ifndef LIST_H #define LIST_H class 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相同的元素,如有则返回下标,若无则返回-1 int 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的元素前插入e int LiseDelete(int index,int &e);//删除下标为index的元素并返回该元素 void MergeList(List la, List lb);//使用前需创建一个listsize=la.length+lb.length的lc private: 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; else return 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; }
相关文章推荐
- 线性表的顺序存储实现c++
- c++模板类实现线性表顺序存储
- 线性表之顺序表C++实现
- C++实现顺序结构线性表的基本操作
- c++实现线性表的顺序结构
- C++实现线性表之顺序表
- 数据结构(3)线性表之顺序表C++实现
- 线性表的顺序表示与实现(C++版)
- 线性表的顺序表示实现-C++版
- 线性表的顺序表示实现-C++版
- 顺序存储线性表的C++实现——严蔚敏版《数据结构》
- 线性表顺序存储(C++ 实现)
- 线性表的顺序实现
- 线性表顺序实现
- 《线性表的顺序表示和实现》
- 线性表的顺序实现
- C++实现CVPR2010 LLC(局部约束线性编码)
- 顺序栈的c++实现
- 线性表的两种实现 -- 顺序表和链表(C++)
- 线性表的顺序表示和实现 - 数据结构