CS 225 Data Structures Se Septembe ber 18 18 Li List - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

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;


slide-1
SLIDE 1

CS 225

Data Structures

Se Septembe ber 18 18 – Li List Implementation

G G Carl Evans

slide-2
SLIDE 2

Li List I Implementation

  • ns

1. 2.

slide-3
SLIDE 3

Li Linked M Memor

  • ry

C S 2 2 5

Ø

slide-4
SLIDE 4

class ListNode { T data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } };

List.h

28 29 30 31 32

slide-5
SLIDE 5

#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

slide-6
SLIDE 6

Li Linked M Memor

  • ry

C S 2 2 5

Ø

head_

slide-7
SLIDE 7

template <typename T> typename List<T>::ListNode *& List<T>::_index(unsigned index) { }

List.hpp

57 58 59 60 61 62 63 64 65

slide-8
SLIDE 8

Li Linked M Memor

  • ry

C S 2 2 5

Ø

head_

slide-9
SLIDE 9

// 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

slide-10
SLIDE 10

Li Linked M Memor

  • ry

C S 2 2 5

Ø

head_

slide-11
SLIDE 11

template <typename T> T & List<T>::operator[](unsigned index) { }

List.cpp

48 49 50 51 52 53 54 55 56 57 58

slide-12
SLIDE 12

Li Linked M Memor

  • ry

C S 2 2 5

Ø

head_

slide-13
SLIDE 13

template <typename T> void List<T>::insert(const T & t, unsigned index) { }

List.cpp

90 91 92 93 94 95 96 97 98 99

slide-14
SLIDE 14

Li Linked M Memor

  • ry

C S 2 2 5

Ø

head_

slide-15
SLIDE 15

template <typename T> T List<T>::remove(unsigned index) { }

List.cpp

103 104 105 106 107 108 109 110 111 112

slide-16
SLIDE 16

Li Linked M Memor

  • ry

C S 2 2 5

Ø

head_

slide-17
SLIDE 17

Se Sentinel N Nod

  • de

C S 2 2 5

Ø

head_

slide-18
SLIDE 18

Li List I Implementation

  • ns
  • 1. Linked List

2.

slide-19
SLIDE 19

#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

slide-20
SLIDE 20

Arr Array I Implementation

C S 2 2 5 [0] [1] [2] [3] [4]

slide-21
SLIDE 21

Arr Array I Implementation

C S 2 2 5 [0] [1] [2] [3] [4]

insertAtFront:

slide-22
SLIDE 22

Re Resize Stra rategy – De Details ails

slide-23
SLIDE 23

Re Resize Stra rategy – De Details ails

slide-24
SLIDE 24

Arr Array I Implementation

C S 2 2 5 [0] [1] [2] [3] [4] T* arr_: T* zero_

slide-25
SLIDE 25

Arr Array I Implementation

C S 2 2 5 [0] [1] [2] [3] [4] T* arr_: T* zero_

slide-26
SLIDE 26

Arr Array I Implementation

C S 2 2 5 [1] [2] [3] [4] [5] W T* arr: T* zero [0]

slide-27
SLIDE 27

Arr Array I Implementation

C S 2 2 5 [1] [2] [3] [4] [5] W T* arr: T* zero [0]

slide-28
SLIDE 28

Arr Array I Implementation

Singly Linked List Array Insert/Remove at front Insert at given element Remove at given element Insert at arbitrary location Remove at arbitrary location