TS. Lê Minh Trung
ThS Lương Trần Ngọc Khiết
Khoa Công nghệ Thông tin, Đại học Sư phạm TP. HCM
Danh sách (List)
Sử dụng mảng
Sử dụng con tr
Danh sách liên kết đôi
Thiết kế Class List
const int MAX= 20;
template<class T>
class List
{
public:
List(void);
~List(void);
int GetSize(); //trả về số phần tử của list
bool IsEmpty(); //kiểm tra list rỗng không
bool IsFull(); //kiểm tra xem list có đầy không
void SetItem(int pos, const T& item); //thiết lập giá trị item cho phần tử thứ pos
TGetItem(int pos); //truy cập phần tử vị trí pos
void Insert(const T&item); //thêm vào vị trí đầu tiên
void InsertAt(int pos, const T& item); //thêm item vào vị trí pos
void Remove(const T& item); //xóa phần tử đầu tiên có giá trị item
void RemoveAt(int pos); //xóa phần tử tại vị trí pos
int IndexOf(const T& item); //trả về vị trí lần đầu tiên tìm thấy item
void Traverse(void (*visit)(T& item)); //duyệt qua các phần tử của list thực
hiện hàm visit với các phần tử
private:
int count;
Tdata[MAX];
};
Một số phương thức
template<class T>
List<T>::List(void)
{
count =0;
}
template<class T>
bool List<T>::IsEmpty(){
return count==0;
}
template<class T>
bool List<T>::IsFull()
{
return count==MAX;
}
template<class T>
T List<T>::GetItem(int pos)
{
if((pos<0)||(pos>count-1)){
throw exception("Index is out
of range");
}else
{
return data[pos];
}
}
template<class T>
void List<T>::SetItem(int pos,
const T& item){
if((pos<0)||(pos>count-1)){
throw exception("Index is
out of range");
}else
{
data[pos]= item;
}
}