CS 225
Data Structures
Se Septembe ber 18 18 – Li List Implementation
G G Carl Evans
CS 225 Data Structures Se Septembe ber 18 18 Li List - - PowerPoint PPT Presentation
CS 225 Data Structures Se Septembe ber 18 18 Li List Implementation G G Carl Evans Li List I Implementation ons 1. 2. Li Linked M Memor ory 2 2 C S 5 List.h 28 class ListNode { 29 T data; 30 ListNode * next;
Data Structures
Se Septembe ber 18 18 – Li List Implementation
G G Carl Evans
1. 2.
C S 2 2 5
Ø
class ListNode { T data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } };
List.h
28 29 30 31 32
#pragma once template <typename T> class List { public: /* ... */ private: class ListNode { public: T data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } }; ListNode *head_; };
List.h
1 2 3 4 5 … 19 20 21 22 23 24 25 26 27 28 … #include "List.h" template <typename T>
void List::insertAtFront(const T& d) {
}
List.hpp
9 … 14 15 16 17 18 19 20 21 22
C S 2 2 5
Ø
head_
template <typename T> typename List<T>::ListNode *& List<T>::_index(unsigned index) { }
List.hpp
57 58 59 60 61 62 63 64 65
C S 2 2 5
Ø
head_
// Iterative Solution: template <typename T> typename List<T>::ListNode *& List<T>::_index(unsigned index) { if (index == 0) { return head; } else { ListNode *thru = head; for (unsigned i = 0; i < index - 1; i++) { thru = thru->next; } return thru->next; } }
List.hpp
C S 2 2 5
Ø
head_
template <typename T> T & List<T>::operator[](unsigned index) { }
List.cpp
48 49 50 51 52 53 54 55 56 57 58
C S 2 2 5
Ø
head_
template <typename T> void List<T>::insert(const T & t, unsigned index) { }
List.cpp
90 91 92 93 94 95 96 97 98 99
C S 2 2 5
Ø
head_
template <typename T> T List<T>::remove(unsigned index) { }
List.cpp
103 104 105 106 107 108 109 110 111 112
C S 2 2 5
Ø
head_
C S 2 2 5
Ø
head_
2.
#pragma once template <typename T> class List { public: /* ... */ private: };
List.h
1 2 3 4 5 … 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
C S 2 2 5 [0] [1] [2] [3] [4]
C S 2 2 5 [0] [1] [2] [3] [4]
insertAtFront:
C S 2 2 5 [0] [1] [2] [3] [4] T* arr_: T* zero_
C S 2 2 5 [0] [1] [2] [3] [4] T* arr_: T* zero_
C S 2 2 5 [1] [2] [3] [4] [5] W T* arr: T* zero [0]
C S 2 2 5 [1] [2] [3] [4] [5] W T* arr: T* zero [0]
Singly Linked List Array Insert/Remove at front Insert at given element Remove at given element Insert at arbitrary location Remove at arbitrary location