在《数据结构与算法分析C++语言描述中》
使用静态数组存储的列表类:
List头文件: List.h
/* * List.h * * Created on: 2015-11-17 * Author: John * * 这个头文件定义了用于处理列表的List数据类型 * 基本包括: * 头早函数 * empty: 检查列表是否为空 * intert: 插入一个项 * erase: 删除一个项 * display: 输出列表 * <<: 输错操作符 * */ #include<iostream> using namespace std; #ifndef LIST_H_ #define LIST_H_ const 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; /*前置条件: ostream out 已经被打开 * 后置条件:这个List对象所表示的列表被插入到out中 * */ private: int mySize; // 当前存储在myArray中的列表的大小 ElementType myArray[CAPACITY]; // 存储列表元素的数组 }; // 输出操作符的原型 ostream & operator<< (ostream & out, const List & aList); #endif /* LIST_H_ */
List类实现:List.cpp
/* * List.cpp * * Created on: 2015-11-17 * Author: John */ #include <cassert> using namespace std; #include "List.h" List::List() :mySize(0){ // TODO Auto-generated constructor stub } // 判断是否为空empty() 的定义 bool List::empty() const{ return mySize ==0; } void List::display(ostream &out) const { for(int i=0;i < mySize;++i){ out << myArray[i]<<" "; } } void List::insert(ElementType item,int pos){ if(mySize == CAPACITY){ cerr << "NO SPACE\n"; exit(1); } if(pos <0 || pos>mySize){ cerr << "Illegal location\n"; return; } // 从右往左将所有在pos前面的元素向右移动 for(int i = mySize; i> pos; --i){ myArray[i] = myArray[i-1]; } // 将item插入到pos位置处,自增列表大小 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\n"; return; } // 从左至右,将pos右方所有的值覆盖存储到左侧 for(int i = pos; i<mySize;++i){ myArray[i] = myArray[i+1]; } mySize--; } ostream &operator<< (ostream & out, const List & aList){ aList.display(out); return out; }
List类测试驱动:List_test.cpp
/* ******** List_test.cpp ********** */ #include <iostream> using namespace std; #include "List.h" int main(){ // 测试驱动 List intList; cout<< "Constructing intList\n"; // 测试empty(),并输出列表 if( intList.empty()){ cout<<"Empty List\n"; } // 测试insert for( int i=0; i<9;++i){ intList.insert(i,i/2); } // 输出列表内容 // 因为我们在前面重载了<<运算符 // ostream &operator<< (ostream & out, const List & aList) // 在函数中我们调用了display()函数; aList.display(out); // 在display()函数中,我们使用循环,将所有的数组内容使用重载运算符"<<"插入到out流中, // out流是ostream类型;最终可以用cout进行输出(cout输出的是ostream类型数据) cout<<intList<<endl; // 测试列表现在是否为空 cout<<"List is Empty? "<< (intList.empty()? "Yes":"No")<<endl; // 在-1和10这两个非法的位置插入数0 intList.insert(0,-1); intList.insert(0,10); cout<<"After insertion:\n"<<intList<<endl; // 擦除指定位置的数 intList.erase(5); cout<<"6th item Erased:"<< intList<<endl; while(!intList.empty()){ cout<<"remove first element:"; intList.erase(0); 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<<"Attempting to insert one more integer:\n"; intList.insert(10,0); return 0; }
输出:
<span style="color:#FF0000;">Illegal location Illegal location</span> Constructing intList Empty List 1 3 5 7 8 6 4 2 0 List is Empty? No After insertion: 1 3 5 7 8 6 4 2 0 6th item Erased:1 3 5 7 8 4 2 0 remove first element:3 5 7 8 4 2 0 remove first element:5 7 8 4 2 0 remove first element:7 8 4 2 0 remove first element:8 4 2 0 remove first element:4 2 0 remove first element:2 0 remove first element:0 remove first element: List is empty Inserting 1024 integers Attempting to insert one more integer: <span style="color:#FF0000;">NO SPACE</span>