Bài giảng Cấu trúc dữ liệu: Danh sách liên kết - TS. Lê Minh Trung & Th.S Lương Trần Ngọc Khiết

32 19 0
Bài giảng Cấu trúc dữ liệu: Danh sách liên kết - TS. Lê Minh Trung & Th.S Lương Trần Ngọc Khiết

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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!

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 trỏ  Danh sách liên kết đôi Thiết kế Class List const int MAX= 20; template class List { public: List(void); ~List(void); int GetSize(); //trả số phần tử 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í 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ử có giá trị item void RemoveAt(int pos); //xóa phần tử vị trí pos int IndexOf(const T& item); //trả vị trí lần tìm thấy item void Traverse(void (*visit)(T& item)); //duyệt qua phần tử list thực hàm visit với phần tử private: int count; T data[MAX]; }; Một số phương thức template bool List::IsEmpty(){ return count==0; } template List::List(void) { count =0; } template bool List::IsFull() { return count==MAX; } template T List::GetItem(int pos) { if((poscount-1)){ throw exception("Index is out of range"); }else { return data[pos]; } } template void List::SetItem(int pos, const T& item){ if((poscount-1)){ throw exception("Index is out of range"); }else { data[pos]= item; } } Thêm vào danh sách liên tục z a b c d e f g h count=9 count=8 InsertAt(3, ‘z’) Thêm vào danh sách template void List::InsertAt(int pos, const T& item){ if(IsFull()){ throw exception("List is full"); }else { if((poscount)){ throw exception("Index is out of range"); }else { for(int i=count -1; i>=pos;i )data[i+1]=data[i]; data[pos] = item; count ++; } } } template void List::Insert(const T& item){ InsertAt(0,item); } Xóa từ danh sách liên tục a b c d e f g h count=7 count=8 RemoveAt(3) Xóa phần tử từ danh sách template void List::RemoveAt(int pos){ if(IsEmpty()){ throw exception("List is empty"); }else { if((poscount-1)){ throw exception("Index is out of range"); }else { for(int i=pos+1; inext; }else following = head; newNode = new(nothrow) Node(item, following); if(newNode==nullptr){ throw exception("Not enough memory"); }else { if(pos==0)head= newNode; else previous->next = newNode; count++; } } } Phương thức Insert template void List::Insert(const T& item){ InsertAt(0,item); } Xóa bỏ từ DSLK đơn previous_node following_node x y z phần tử vị trí position-1 phần tử vị trí position phần tử vị trí position+1 bây giờ, phần tử có vị trí position Phương thức RemoveAt template void List::RemoveAt(int pos){ if(count ==0){ throw exception("List is empty"); return; } if(poscount-1){ throw exception("Index is out of range"); }else{ Node* previous, *following; if(pos>0){ previous = SetPosition(pos -1); following = previous -> next; previous -> next = following ->next; }else { following = head; head = following ->next; } delete following; count ; } } Phương thức Clear destructor template void List::Clear(){ while(count>0)RemoveAt(0); } template List::~List(void) { Clear(); } Phương thức Traverse template void List::Traverse(void (*visit)(T& item)) const{ Node* q = head; while(q !=nullptr){ (*visit)(q->item); q = q -> next; } } Thử nghiệm template void Print(T& item){ cout

Ngày đăng: 09/08/2021, 17:43

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan