数据结构-------列表二
来源:互联网 发布:郑源网络歌手 编辑:程序博客网 时间:2024/05/15 00:57
基于动态数组的列表和基于静态数组的列表的类相差无几。
1.去掉限定数组容量的常量声明,添加一个变量数据成员来存储在对象声明时由用户指定的数组容量
2.修改数组对象成员为一个指针,他将指向运行时为数组分配的存储空间的地址;
其中的empty ,display ,delet 成员函数无需修改。在insert 中也只需改动一个限定容量的常量。
分析可知列表ADT内容如下:
{
数据元素集合:
数据元素的一个有限序列,所有元素都是同一类型。
基本操作:
* 构造函数 :创建一个空列表
* 判空函数 :检查列表是否为空
* 插入函数 :在列表中增加一项
* 删除函数 :在列表中删除一项
* 遍历函数 : 对于在这个列表中或列表部分,按顺序访问和处理元素,这个操作也被称为在列表上的迭代操作
}
写出其中的遍历,插入和删除的算法过程:
//遍历算法for 下标 i 从0到 size-1 处理array[i];//插入算法insert (ElementType item,int pos){if (没有多余的空间)程序终止if (pos位置非法)终止程序elsefor (i=size back to pos+1)array[i] = array[i-1];array[pos] = item;size++;}//删除算法delet (int pos){if (列表为空 || POS 位置非法)程序终止elsefor (i=pos+1 to size)array[i-1] = array[i];size--;}
类声明代码:
#ifndef LIST#define LISTtypedef int ElementType;class List{public :List (int max = 1024);~List ();List (const List & alist);const List & operator = (const List & alist);bool empty () const;void display (ostream &out) const;void insert (ElementType item,int pos);void delet (int pos);private :int mySize; //列表长度int count; //数组容量int * myArray; //指向动态数组的指针};#endif
类实现代码:
#include <iostream>#include "List.h"using namespace std;List::List (int max) : mySize(0),count (max){myArray = new(nothrow) ElementType[max];}//类析构函数。List::~List (){delete [] myArray;}//复制构造函数List::List (const List &alist) : mySize(alist.mySize),count(alist.count){//为复制获取新的数组myArray = new(nothrow) ElementType[count];if (myArray != 0)//将alist的元素复制到这个新数组中。for (int i=0; i<mySize; i++)myArray[i] = alist.myArray[i];elsecerr << "Inadequate memery to allocate storage for list\n";}//赋值运算符的定义const List & List::operator = (const List & right){if (this != &right) //确认不是自我复制{//如果两个数组的容量不相等,则分配一个新数组if (count != right.count){delete [] myArray;count = right.count;myArray = new(nothrow) ElementType[count];if (myArray == 0)cerr << "Inadequate memery to allocate stack****\n";}mySize = right.mySize;for (int i=0; i<mySize; i++)myArray[i] = right.myArray[i];}return *this;}bool List::empty () const{return mySize == 0;}void List::display (ostream & out) const{for (int i=0; i<mySize; i++)out << myArray[i] << " ";}ostream & operator << (ostream & out,const List & alist){alist.display (out);return out;}void List::insert (ElementType item,int pos){if (pos<0 || pos>mySize)cerr << "Error";for (int i=mySize; i>pos; i--)myArray[i] = myArray[i-1];myArray[pos] = item;mySize++;}void List::delet (int pos){if (mySize==0)cerr << "Empty List**\n";else if (pos<0 || pos>mySize)cerr << "Not Space\n";for (int i=pos+1;i<mySize;i++)myArray[i-1] = myArray[i];}
类测试代码:
#include <iostream>#include "List.h"using namespace std;void print (List alist){cout << alist << endl;}int main (){int limit;cout << "Enter the limit of list element: \n";cin >> limit;List list1(limit);for (int i=0; i<limit; i++){cout << "*****Next statement: list1.insert ();\n";list1.insert (i,i);}print (list1);cout << "*** Next Statement: List list2 = list2;\n";List list2 = list1;cout << "*** Next Statement: print list2 \n";print (list2);return 0;}
0 0
- 数据结构-------列表二
- 数据结构 列表
- 列表-数据结构
- 数据结构 列表
- 数据结构[二]
- 数据结构(二)
- 二:数据结构
- 数据结构二
- Java数据结构应用——无序列表学习总结(二)
- 学习JavaScript数据结构与算法(七)——散列表(二)
- 数据结构---哈希表/散列表
- 数据结构学习列表
- 数据结构1 顺序列表
- 【数据结构】散列表_HashTable
- 数据结构-列表实现
- 数据结构---列表一
- python数据结构的-----列表
- Python之列表数据结构
- [Android] AsyncTask使用实例---加载网络图片
- bzoj2662: [BeiJing wc2012]冻结 分层图最短路
- 让人死去活来的cocos2d-x安卓开发环境搭建(windows+eclipse+ndk)
- MSP430F5529 (六)定时器Timer_A
- 日期比较
- 数据结构-------列表二
- Java多线程的使用
- sql server 查询存储过程定义
- JNI学习三(Local references & Global references 以及JNI内存泄露)
- opencv实现图像旋转
- 学习
- IDA 调试SO
- iOS:UITableView 方法 属性 详解
- 将View转换成Bitmap