1. Trang chủ
  2. » Tất cả

Cấu trúc dữ liệu và thuật toán dsa ch5 lists

61 3 0

Đ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

Lists (cont) - Linked List Dr Nguyen Ho Man Rang Chapter Lists (cont) - Linked List Singly linked list Other linked lists Data Structures and Algorithms Comparison of implementations of list Dr Nguyen Ho Man Rang Faculty of Computer Science and Engineering University of Technology, VNU-HCM 5.1 Outcomes Lists (cont) - Linked List Dr Nguyen Ho Man Rang • L.O.2.1 - Depict the following concepts: (a) array list and linked list, including single link and double links, and multiple links • L.O.2.2 - Describe storage structures by using pseudocode for: (a) array list and linked list, including single link and double links, and multiple links • L.O.2.3 - List necessary methods supplied for list and describe them using pseudocode • L.O.2.4 - Implement list using C/C++ Singly linked list Other linked lists Comparison of implementations of list 5.2 Outcomes Lists (cont) - Linked List Dr Nguyen Ho Man Rang • L.O.2.5 - Use list for problems in real-life, and choose an appropriate implementation type (array vs link) • L.O.2.6 - Analyze the complexity and develop experiment (program) to evaluate the efficiency of methods supplied for list • L.O.8.4 - Develop recursive implementations for methods supplied for the following structures: list • L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational complexity of algorithms composed by using the following control structures: sequence, branching, and iteration (not recursion) Singly linked list Other linked lists Comparison of implementations of list 5.3 Contents Lists (cont) - Linked List Dr Nguyen Ho Man Rang Singly linked list Singly linked list Other linked lists Other linked lists Comparison of implementations of list Comparison of implementations of list 5.4 Lists (cont) - Linked List Dr Nguyen Ho Man Rang Singly linked list Singly linked list Other linked lists Comparison of implementations of list 5.5 Lists (cont) - Linked List Linked List Dr Nguyen Ho Man Rang Singly linked list Hình: Singly Linked List Other linked lists Comparison of implementations of list list // Linked Implementation of List head < pointer > count < integer > // number of elements ( optional ) end list 5.6 Nodes Lists (cont) - Linked List Dr Nguyen Ho Man Rang The elements in a linked list are called nodes A node in a linked list is a structure that has at least two fields: • the data, • the address of the next node Singly linked list Other linked lists Comparison of implementations of list 5.7 Lists (cont) - Linked List Nodes Dr Nguyen Ho Man Rang Hình: Linked list node structure Singly linked list Other linked lists Comparison of implementations of list node data < dataType > link < pointer > end node // General dataType : dataType key < keyType > field1 < > field2 < > fieldn < > end dataType 5.8 Example Lists (cont) - Linked List Dr Nguyen Ho Man Rang Singly linked list Other linked lists Comparison of implementations of list 5.9 Lists (cont) - Linked List Example Dr Nguyen Ho Man Rang Singly linked list Other linked lists Comparison of implementations of list Hình: List representing polynomial 5.10 Linked list implementation in C++ Lists (cont) - Linked List Dr Nguyen Ho Man Rang How to use Linked List data structure? // int value ; L i n k e d L i s t ∗ m y L i s t = m y L i s t −>C l o n e ( ) ; c o u t G e t I t e m ( , v a l u e ) ; c o u t c o u n t ) Node ∗ newPtr , ∗ pPre ; newPtr = new Node ( ) ; i f ( newPtr == NULL) return 0; Singly linked list Other linked lists Comparison of implementations of list newPtr−>d a t a = v a l u e ; i f ( head == NULL) { head = newPtr ; newPtr−> l i n k = NULL ; } e l s e i f ( p o s i t i o n == ) { newPtr−> l i n k = head ; head = newPtr ; } 5.48 Sample Solution: Insert Lists (cont) - Linked List Dr Nguyen Ho Man Rang else { // F i n d t h e p o s i t i o n o f pPre pPre = t h i s −>head ; f o r ( i n t i = ; i < p o s i t i o n −1; i ++) pPre = pPre−>l i n k ; // I n s e r t new node newPtr−>l i n k = pPre−>l i n k ; pPre−>l i n k = newPtr ; Singly linked list Other linked lists Comparison of implementations of list } t h i s −>c o u n t ++; return 1; } 5.49 Sample Solution: Delete Lists (cont) - Linked List Dr Nguyen Ho Man Rang i n t L i n k e d L i s t : : DeleteItem ( i n t p o s i t i o n ){ i f ( p o s i t i o n < | | p o s i t i o n > t h i s −>c o u n t ) return 0; Node ∗ d l t P t r , ∗ pPre ; Singly linked list i f ( p o s i t i o n == ) { d l t P t r = head ; head = head−> l i n k ; } else { pPre= t h i s −>head ; f o r ( i n t i = ; i < p o s i t i o n −1; i ++) pPre = pPre−> l i n k ; d l t P t r = pPre−> l i n k ; pPre−> l i n k = d l t P t r −> l i n k ; } Other linked lists Comparison of implementations of list delete dltPtr ; t h i s −>c o u n t −−; return 1; } 5.50 Sample Solution: Clone Lists (cont) - Linked List Dr Nguyen Ho Man Rang L i n k e d L i s t ∗ L i n k e d L i s t : : Clone (){ L i n k e d L i s t ∗ r e s u l t = new L i n k e d L i s t ( ) ; Singly linked list Node∗ p = t h i s −>head ; w h i l e ( p != NULL) { r e s u l t −>I n s e r t L a s t ( p−>d a t a ) ; p = p−>l i n k ; } Other linked lists Comparison of implementations of list r e s u l t −>c o u n t = t h i s −>c o u n t ; return r e s u l t ; } 5.51 Reverse a linked list Exercise Lists (cont) - Linked List Dr Nguyen Ho Man Rang void L i n k e d L i s t : : Reverse (){ // } Singly linked list Other linked lists Comparison of implementations of list 5.52 Lists (cont) - Linked List Dr Nguyen Ho Man Rang Singly linked list Other linked lists Other linked lists Comparison of implementations of list 5.53 Lists (cont) - Linked List Doubly Linked List Dr Nguyen Ho Man Rang Hình: Doubly Linked List allows going forward and backward Singly linked list Other linked lists Comparison of implementations of list node data < dataType > next < pointer > previous < pointer > end node list c u r r e n t

end l i s t 5.54 Doubly Linked List Lists (cont) - Linked List Dr Nguyen Ho Man Rang Hình: Doubly Linked List allows going forward and backward Singly linked list Other linked lists Comparison of implementations of list Hình: Insert an element in Doubly Linked List 5.55 Lists (cont) - Linked List Circularly Linked List Dr Nguyen Ho Man Rang Singly linked list Other linked lists Comparison of implementations of list node data < dataType > link < pointer > end node list c u r r e n t

end l i s t 5.56 Lists (cont) - Linked List Double circularly Linked List Dr Nguyen Ho Man Rang Singly linked list Other linked lists Comparison of implementations of list node data < dataType > next < pointer > previous < pointer > end node list c u r r e n t

end l i s t 5.57 Lists (cont) - Linked List Dr Nguyen Ho Man Rang Comparison of implementations of list Singly linked list Other linked lists Comparison of implementations of list 5.58 Arrays: Pros and Cons Lists (cont) - Linked List Dr Nguyen Ho Man Rang • Pros: • Access to an array element is fast since we can compute its location quickly Singly linked list • Cons: • If we want to insert or delete an element, we have to shift subsequent elements which slows our computation down • We need a large enough block of memory to hold our array Other linked lists Comparison of implementations of list 5.59 Linked Lists: Pros and Cons Lists (cont) - Linked List Dr Nguyen Ho Man Rang • Pros: • Inserting and deleting data does not require us to move/shift subsequent data elements • Cons: • If we want to access a specific element, we need to traverse the list from the head of the list to find it which can take longer than an array access Singly linked list Other linked lists Comparison of implementations of list 5.60 Comparison of implementations of list Lists (cont) - Linked List Dr Nguyen Ho Man Rang • Contiguous storage is generally preferable when: • the entries are individually very small; • the size of the list is known when the program is written; • few insertions or deletions need to be made except at the end of the list; and • random access is important Singly linked list Other linked lists Comparison of implementations of list • Linked storage proves superior when: • the entries are large; • the size of the list is not known in advance; and • flexibility is needed in inserting, deleting, and rearranging the entries 5.61 ... linked lists Comparison of implementations of list 5.3 Contents Lists (cont) - Linked List Dr Nguyen Ho Man Rang Singly linked list Singly linked list Other linked lists Other linked lists Comparison... implementations of list 5.4 Lists (cont) - Linked List Dr Nguyen Ho Man Rang Singly linked list Singly linked list Other linked lists Comparison of implementations of list 5.5 Lists (cont) - Linked... Other linked lists Comparison of implementations of list 5.7 Lists (cont) - Linked List Nodes Dr Nguyen Ho Man Rang Hình: Linked list node structure Singly linked list Other linked lists Comparison

Ngày đăng: 25/03/2023, 07:21

Xem thêm:

w