基于数组的列表实现
来源:互联网 发布:手机淘宝怎样发送链接 编辑:程序博客网 时间:2024/06/17 15:53
//--------List.h----------#include<iostream>#ifndef LIST#define LISTconst int CAPACITY=1024;typedef int ElementType;class List{public:List();bool empty() const;void insert(ElementType item,int pos);void erase(int pos);void display(ostream & out) const;private: int mySize;ElementType myArray[CAPACITY];};ostream & operator<<(ostream & out,const List & aList);#endif
//-------List.cpp----------#include<cassert>using namespace std;#include"List.h"List::List():mySize(0){}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(mySize==CAPACITY){ cerr<<"***No space for list element.***\n";exit(1);}if(pos<0||pos>mySize){cerr<<"***Illegal location to insert.***\n";return;}for(int i=mySize;i>pos;i--)myArray[i]=myArray[i-1];myArray[pos]=item;mySize++;}void List::erase(int pos){if(mySize==0){cerr<<"***List is empty.***\n";return;}if(pos<0||pos>=mySize){cerr<<"***Illegal location to delete***.\n";return;}for(int i=pos;i<mySize;i++)myArray[i]=myArray[i+1]; mySize--;}
//------------main.cpp------------#include<iostream>using namespace std;#include"List.h"int main(){List intList;cout<<"Construction intList\n";if(intList.empty())cout<<"Empty List: \n" <<intList<<endl;for(int i=0;i<10;i++){cout<<"Inserting "<<i<<"at position "<<i/2<<endl;intList.insert(i,i/2);cout<<intList<<endl;}cout<<"List empty? "<<(intList.empty() ? "Yes":"No")<<endl; cout<<"\nTry to insert at position -1"<<endl; intList.insert(0,-1); cout<<"\nTry to insert at position -1"<<endl; intList.insert(0,10);int index;cout<<endl; while(!intList.empty()) { cout<<"Give an index of a list element to remove: "; cin>>index; intList.erase(index); cout<<intList<<endl; } cout<<"List is empty"<<endl; cout<<"\nInserting "<<CAPACITY<<"integers\n"; for(int i=0;i<CAPACITY;i++) intList.insert(i,i); cout<<"Attempt to insert one more integers:\n"; intList.insert(-1,0);}
阅读全文
1 0
- 基于数组的列表实现
- 简单列表的实现(基于数组)
- 基于动态数组的列表实现
- JavaScript实战-基于数组的级联列表实现
- 散列表的C程序实现,基于数组实现可以实现增删改查操作,
- 数组列表的简单实现
- 基于数组级联列表
- 实现有序列表(基于数组,二分查找)
- 基于数组的队列实现
- 基于数组的堆栈实现
- 基于数组的栈实现
- 基于数组的队列实现
- 基于列表控件的属性页实现
- 基于jquery的下拉列表实现
- 基于链表的列表实现
- java 基于数组的栈的实现
- (四)基于数组的栈的实现
- 基于数组的队列实现(C语言)
- ubuntu16.04设置静态IP
- Android 7.0 行为变更 通过FileProvider在应用间共享文件吧
- 指针和引用的区别
- SDUT-2671 3-2 构造函数的调用
- java面向对象
- 基于数组的列表实现
- Spring 松耦合实例
- G
- Linux学习掌握(三):CentOS中安装(卸载)Mysql以及数据库的导入导出
- Scikit-learn使用总结
- Hrbust 2302 Another Tree【思维+Dfs】
- 一个理解装饰器很好的例子
- java实现https请求
- Httpclient多线程连接池封装