Bài giảng Cấu trúc dữ liệu: Danh sách liên kết cung cấp cho người học những kiến thức như: Danh sách sử dụng mảng; Xóa từ một danh sách liên tục; Duyệt qua các phần tử; Danh sách sử dụng con trỏ; Giải thuật tìm vị trí trên danh sách liên kết đơn; Phương thức InsertAt;... Mời các bạn cùng tham khảo!
Trang 1TS 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
Trang 4Thiết kế Class List
const int MAX= 20;
int GetSize(); //trả về số phần tử của list
bool IsEmpty(); //kiểm tra list có 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
T GetItem( int pos); //truy cập phần tử có 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 và thực
hiện hàm visit với các phần tử
private :
int count;
T data[MAX];
};
Trang 5void 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;}
}
Trang 6Thêm vào một danh sách liên tục
Trang 7Thêm vào danh sách
if (( pos <0)||( pos >count)){
throw exception("Index is out of range");
Trang 8Xóa từ một danh sách liên tục
Trang 9throw exception("Index is out of range");
Trang 10Duyệt qua các phần tử
template < class T >
void List < T >::Traverse( void (* visit )( T & item)){
for ( int i=0; i<count;i++)(* visit )(data[i]); }
Trang 11List < int > list;
for ( int i=5;i>=1;i-=2)
Trang 13Danh sách liên kết đơn (DSLK đơn)
Trang 14{ this->next = nullptr; }
Trang 15Thiết kế Class List
void InsertAt( int pos, const T & item);
void Insert( const T & item);
void RemoveAt( int pos);
int IndexOf( const T & item);
void Remove( const T & item);
int GetSize() const ;
void Clear(); //xóa sạch phần tử của list
void Traverse( void (*visit)( T & item)) const ;
Trang 17Giải thuật tìm vị trí trên DSLK đơn
Algorithm Set position
Input: position là vị trí cần tìm Output: con trỏ chỉ đến phần tử tại vị trí cần tìm
1 set q to head
2 for index =0 to position //Thi hành position bước
2.1 advance q to the next element //Trỏ q đến phần tử kế tiếp
Trang 19Thêm vào một DSLK đơn
Trang 20Phương thức InsertAt
template < class T >
void List < T >::InsertAt( int pos , const T & item ){
if ( pos <0|| pos >count){
throw exception("Index is out of range");
} else {
Node < T >* previous, *following, *newNode;
if ( pos >0){
previous = SetPosition( pos -1);
following = previous ->next;
} else following = head;
newNode = new (nothrow) Node < T >( item , following);
if (newNode== nullptr ){
throw exception("Not enough memory");
} else
{
if ( pos ==0)head= newNode;
else previous->next = newNode;
count++;
}
}
}
Trang 22Xóa bỏ từ một DSLK đơn
bây giờ, phần tử này
có vị trí position
phần tử tại vị trí positionphần tử tại vị trí position+1
Trang 23if ( pos <0 || pos >count-1){
throw exception( "Index is out of range" );
} else {
Node < T >* previous, *following;
if ( pos >0){
previous = SetPosition( pos -1);
following = previous -> next;
previous -> next = following ->next;
Trang 24Phương thức Clear và destructor
Trang 26template < class T >
void Print( T & item ){
cout << item << " " ; }
}
Thử nghiệm
Trang 27DSLK kép (Doubly linked list)
Trang 28template <class T>
class List {
public:
// Add specications for methods of the list ADT.
// Add methods to replace compiler generated defaults.
protected:
// Data members for the doubly-linked list implementation follow: int count;
mutable int current_position;
mutable Node<T> *current;
// The auxiliary function to locate list positions follows:
void SetPosition(int position) const;
};
Định nghĩa DSLK kép
Các hàm hằng (const) có thể thay đổi giá trị của các biến mutable này
Trang 29Định nghĩa Node cho DSLK kép
Trang 31Thêm vào trong DSLK kép
Trang 32CÁM ƠN VÌ ĐÃ LẮNG NGHE!