Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 36 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
36
Dung lượng
2,35 MB
Nội dung
[...].. .Linked Ordered List Suppose elements are arranged in ascending order ADT class LinkedOrderedList { public: LinkedOrderedList(); ~ LinkedOrderedList(); void Insert(DataField value); bool Delete(DataField value); //return false if value is not found bool IsEmpty(); private: ListNode *first; }; Initialization The constructor of ListNode: ListNode::ListNode(DataField value) { data = value;... ListNode::ListNode(DataField value) { data = value; link = NULL; } The constructor of LinkedOrderedList: LinkedOrderList::LinkedOrderList() { first = NULL; } Algorithm of Insert() 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 void LinkedOrderList::Insert(DataField value) { curr = first; while (curr != NULL) { if (curr- >data >= value) { ListNode *a = new ListNode(value); a->link = curr; previous->link = a; break; } previous... insertion can always be performed between two nodes Improvement Using Two Dummy Nodes Maintain two dummy nodes at each end of the list first class LinkedList { private: ListNode * first, *last; }; LinkedOrderList::LinkedOrderList() { first = new ListNode(); last = new ListNode(); first->link = last; last->link = NULL; } last No need to update the pointer first Boundary conditions are eliminated... 16 17 void LinkedOrderList::Insert(DataField value) { previous = first; curr = first->link; while (curr != NULL) { if (curr == last || curr- >data >= value) { ListNode *a = new ListNode(value); a->link = curr; previous->link = a; break; } previous = curr; curr = curr->link; } } Algorithm of Delete() 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 bool LinkedOrderList::Delete(DataField value)... Nodes Remember to deallocate each node in the destructor 01 02 03 04 05 06 07 08 09 10 LinkedOrderList ::~ LinkedOrderList() { curr = first; while (curr != NULL) { next = curr->link; Deallocate curr; curr = next; } } Performance Analysis Suppose there are n nodes in a linked list Space complexity: ○ A linked list uses an exact amount of memory space to store these n nodes ○ Space complexity to... rear = NULL; }; bool LinkedQueue::IsEmpty() { if (front is NULL and rear is NULL) return true; return false; } Implementation of Linked Queue Datafield LinkedQueue::Pop() { if (IsEmpty()) output error; else { delNode = front; value = front- >data; front = front->link; deallocate delNode; return value; } }; void LinkedQueue::Push(Datafield value) { if (IsEmpty()) front = rear = new ListNode(value); else... end of the list Therefore, the complexity is O(n) ○ Excessive request of allocation or deallocation of memory space for a node increases loading for the OS system (and may lower efficiency) Linked Stack data top link Pop Pop Push D B D A C E 0 Linked Queue Deletion takes place at front; insertion at rear front B rear C Pop Pop Push E D A E 0 Implementation of Linked Queue LinkedQueue:: LinkedQueue()... compact The time complexity is also O(1) But the memory request increase overhead Polynomial Representation class ListNode { friend class LinkedList; public: ListNode(int c, int e); ~ListNode(); private: int coef, exp; ListNode *link; }; f(x) =3x2+1 first 3 2 coef exp class Polynomial { private: ListNode *first; }; 1 0 Adding Polynomial Example Consider the following two polynomials: a(x) =3x14+2x8+1... ListNode(value); else rear = rear->link = new ListNode(value); }; LinkedQueue::~ LinkedQueue() { while (!IsEmpty()) Pop(); }; Comparison Compare stack/queue implemented using array and linked stack/queue Array Memory space The length of an array is fixed; Resize() is required if the stack is full Execution time for The time complexity is Push() and Pop() O(1) Linked list Memory space can be dynamically allocated... previous->link = a; break; } previous = curr; curr = curr->link; } } Insertion Insert EAT into an ordered linked list previous 8 previous BAT first CAT curr 3 FAT curr a 03 04 05 06 07 08 09 10 11 12 13 14 15 6 4 curr EAT 4 curr = first; while (curr != NULL) { if (curr- >data >= value) { ListNode *a = new ListNode(value); a->link = curr; previous->link = a; break; } previous = curr; curr = curr->link; } 0 . public: ListNode(); ListNode(DataField value); ~ListNode(); private: DataField data; ListNode *link; }; class LinkedList { private: ListNode * first; }; first class LinkedList { private: ListNode. ListNode * first; }; class ListNode { friend class LinkedList; public: ListNode(); ListNode(DataField value); ~ListNode(); private: DataField data; ListNode *link; }; data link