Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.

Similar presentations


Presentation on theme: "1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without."— Presentation transcript:

1 1 Joe Meehean

2  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without copying constant time inserts and removes just (un)hook into/from chain item 1 phead: item N … 2

3  Private nested class nested class: class defined within a class private nested: not visible outside outer class  Member variables public: T data_; Node *next_;  Constructor Node( const T& data = T(), Node * n = NULL) item 1 3

4 Node * phead; phead: phead = new Node (“ant”); ant phead: phead->next = new Node (“bat”); ant phead: bat 4

5  Given phead and a pointer n  What expression gets node containing: ant bat cat ant bat n: cat 5 phead:

6  Given head and a pointer n  What expression gets node containing: ant => *phead bat => *(phead->next), *n cat => *(phead->next->next), *(n->next) ant phead: bat n: cat 6

7 ant bat n: cat jar tmp: 7 phead:

8 ant bat n: cat jar tmp: 8 phead:

9 ant bat n: cat jar tmp: 9 phead:

10 ant phead: bat cat jar n: 10

11 ant bat cat jar n: 11 phead:

12 ant bat cat jar n: 12 phead:

13  LCLinkedList  Member Variables int numItems; Node * phead;  Methods same ADT as ArrayList also has push_front, pop_front same public methods 13

14 ant phead: bat cat 14 tmp:

15 ant phead: bat cat 15 tmp:

16 ant phead: bat cat 16 tmp:

17 void push_front(const T& newItem){ Node* tmp = new Node(newItem); tmp->next = phead; phead = tmp; numItems++; } 17

18 ant phead: bat cat 18 tmp:

19 ant phead: bat cat 19 tmp:

20 void push_back(const T& newItem){ // list is empty (special case) if( phead == NULL ){ phead = new Node(newItem); }else{ // find the end of the list Node* cursor = phead; while(cursor->next != NULL){ cursor = cursor->next; } // add the item cursor->next = new Node(newItem); // increment the count numItems++; }} 20

21 21

22 void push_front(const T& newItem){ Node* tmp = new Node(newItem); tmp->next = phead; phead = tmp; numItems++; } 22

23 void push_back(const T& newItem){ // list is empty (special case) if( phead == NULL ){ phead = new Node(newItem); }else{ // find the end of the list Node* cursor = phead; while(cursor->next != NULL){ cursor = cursor->next; } // add the item cursor->next = new Node(newItem); // increment the count numItems++; }} 23

24 Header phead: ant bat  Eliminates special 1 st node case for add and remove  Create header at initialization  Don’t count in size  Ignore for contains 24

25 void push_back(const T& newItem){ // find the end of the list Node* cursor = phead; while(cursor->next != NULL){ cursor = cursor->next; } // add the item cursor->next = new Node(newItem); // increment the count numItems++; } 25

26  Can you modify LCLinkedList to push_back(…) in O(1)? Hint: You can add member variables 26

27 Header phead: ant bat  push_back now constant time  but, we must update tail pointer when adding to end 27 ptail:

28 Header phead:  tail pointer points at header if list is empty 28 ptail:

29 Header phead: ant bat 29 ptail:

30 Header phead: ant bat 30 ptail:

31 void pop_back(){ if( phead != ptail ){ // find the node just before ptail Node* cursor = phead; while(cursor->next != ptail){ cursor = cursor->next; } // remove the node delete cursor->next; cursor->next = NULL; ptail = cursor; // decrement the count numItems--; }} 31

32 Header phead: ant bat  Tail pointer close to the right position  Still must iterate over nearly entire list  Can we make remove at end O(1)? 32 ptail:

33  Can you modify LCLinkedList to pop_back() in O(1)? Hint: You can modify the structure of the list 33

34 Header phead: bat ant  Node  Member variables public: T data; Node *next; Node *prev 34 ptail:

35 Header phead: bat Tail  Tail node removes special cases just like header node tail and header node called sentinels 35 ptail:

36 Header phead: bat Tail 36 ptail:

37 Header phead: bat Tail 37 ptail:

38 Header phead: bat Tail 38 ptail:

39 Header phead: bat Tail 39 ptail:

40 void pop_back(){ if( phead->next != ptail ){ ptail->prev = ptail->prev->prev; delete ptail->prev->next; ptail->prev->next = ptail; // decrement the count numItems--; } 40

41  Space linked needs two extra pointers per entry linked needs two extra nodes array may be up to N/2-1 too large array may be way too big if some elements removed array better small data items (e.g., ints) linked way better for big items (e.g., classes) too close to call for medium items (e.g., 3 ints) 41

42  Time MethodArray ListLinked List add to endO(N) avg: O(1) O(1) add to frontO(N)O(1) remove at endO(1) remove at front O(N)O(1) get at nO(1)O(N) 42

43  Ease implementation fairly close linked list has more special cases and work- arounds for special cases 43

44 44

45  What if we want to remove an item from the middle of a linked list? remove at i traverse list from beginning until we reach i fix up the pointers O(N) 45

46  What if we were already moving through the list? e.g., remove all even numbers if we did this internally (inside the list class), we would have a pointer to the node to remove O(1) 46

47  LCList::Iterator  Public nested class in LCList  Member data Node * currNode;  Friends LCList 47

48 Iterator::Iterator() : currNode(NULL){} Iterator::Iterator(Node* pNode) : currNode(pNode){} void Iterator::operator ++(){ currNode = currNode->next; } void Iterator::operator –-(){ currNode = currNode->prev; } T& Iterator::operator *(){ return currNode->data; } 48

49 Iterator LCList::begin(){ return Iterator(phead->next); } Iterator LCList::end(){ return Iterator(ptail); } 49

50 Head phead: bat Tail 50 ptail: cat ant rat iter:

51 Head phead: bat Tail 51 ptail: cat ant rat iter:

52 Head phead: bat Tail 52 ptail: cat ant rat iter:

53 Head phead: bat Tail 53 ptail: cat ant rat iter:

54 Iterator LCList::remove(Iterator& itr){ Node* pNode = itr.currNode; // make a new iterator to return back Iterator retVal = (pNode->next); // update the pointers pNode->prev->next = pNode->next; pNode->next->prev = pNode->prev; // clean up the memory delete pNode; // decrement the count nrItems--; return retVal; } 54

55 Head phead: bat Tail 55 ptail: cat ant rat iter:

56 Head phead: bat Tail 56 ptail: cat ant rat iter:

57 Head phead: bat Tail 57 ptail: cat ant rat iter:

58 Head phead: bat Tail 58 ptail: cat ant rat iter:

59 void LCList::insert(Iterator& iter, const T& t){ // extract the node from the iterator Node* pNode = iter.currNode // make a new node to store the item Node* newNode = new Node(t, pNode->prev, pNode); // fix up the pointers pNode->prev->next = newNode; pNode->prev = newNode; // increment the count nrItems++; } 59

60  Potential errors in remove and insert uninitialized iterators iterators for other lists  Fixes check iterator’s current node for NULL add a new member value to Iterator: List * myList 60

61  Time for iterative work MethodArray ListLinked List remove at n (with iterator) O(N)O(1) insert at n (with iterator) O(N)O(1) get at n (with iterator) O(1) get at n (without an iterator) O(1)O(N) 61

62 62


Download ppt "1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without."

Similar presentations


Ads by Google