📄 list.h
字号:
#ifndef LIST_H
#define LIST_H
//contiguous list
// Prep: Clist<List_entry>中的List_entry必须重载赋值操作符=
#include "ErrorCode.h"
//const int INITQUANTITY = 1000000; //array len when initilizing, you can change it as needed
const int INCREMENT = 100; //increment count when there is not enough space for the entry, you can change it as needed
template<class List_entry>
class List{
public:
// methods of the List ADT
List();
int size() const;
bool empty() const;
void clear();
void traverse(void(*visit)(List_entry &));
Error_code retrieve(int position,List_entry &x) const; //0<=position<n
Error_code replace(int position,const List_entry &x); //0<=position<n
Error_code remove(int position,List_entry &x); //0<=position<n
Error_code insert(int position,const List_entry &x); //0<=position<=n
protected:
// protected member function
void inflate(int increase); //allocate new space(increase*sizeof(List_entry)
protected:
// data members for a contiguous list implementation
int count; //the entry count in the list
int quantity; //total quantity of the list
List_entry *parray;
};
template<class List_entry>
List<List_entry>::List(){
//constructor,create an empty list
parray = NULL;
count = 0;
quantity = 0;
}
template<class List_entry>
int List<List_entry>::size() const{
/* Post: The function returns the number of entries in the list.*/
return count;
}
template<class List_entry>
bool List<List_entry>::empty() const{
if(count==0)
return true;
else
return false;
}
template<class List_entry>
void List<List_entry>::clear(){
delete []parray;
parray = NULL;
count = 0;
quantity = 0;
}
template<class List_entry>
void List<List_entry>::traverse(void(*visit)(List_entry &)){
/* Post: The action specified by function (* visit) has been
performed on every entry of the List,beginning at
position 0 and doing each in turn */
for(int i=0;i<count;i++)
(*visit)(parray[i]);
}
template<class List_entry>
Error_code List<List_entry>::retrieve(int position,List_entry &x) const{
if (count==0)
return underflow;
if ( position<0 || position>=count )
return outof_range;
x = parray[position];
return success;
}
template<class List_entry>
Error_code List<List_entry>::replace(int position,const List_entry &x){
if ( count == 0 )
return underflow;
if ( position<0 || position>=count )
return outof_range;
parray[position] = x ;
return success;
}
template<class List_entry>
Error_code List<List_entry>::remove(int position,List_entry &x){
if ( count == 0 )
return underflow;
if ( position<0 || position>=count )
return outof_range;
//save the entry
x = parray[position];
//move the entry after the position forward a step
for( int i=position; i<count-1; i++ )
parray[i]=parray[i+1];
count--;
return success;
}
template<class List_entry>
Error_code List<List_entry>::insert(int position,const List_entry &x)
{
if ( position<0 || position>quantity ) //0<=position<=n
return outof_range;
//check the quantity to see if it is enough
if ( count >= quantity )
inflate(INCREMENT);
if ( count == 0 ){ //the first element
parray[count] = x;
count++;
return success;
}
// when :count=1 , position=0
for( int i=count; i>position; i-- )
parray[i]=parray[i-1];
parray[position]=x;
count++;
return success;
}
template<class List_entry>
void List<List_entry>::inflate(int increase)
{
int newquantity = quantity + increase; //new total quantity of the list
List_entry *pnewarray = new List_entry[newquantity];
for ( int i=0; i<count; i++ )
{//copy old data to new array
pnewarray[i] = parray[i];
}
delete []parray;
parray = pnewarray;
quantity = newquantity;
}
#endif ///:~ LIST_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -