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

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 5 Linked Lists II
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
Linked Lists and Generics Written by J.J. Shepherd.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked List, Stacks Queues
C++ Programming:. Program Design Including
Cpt S 122 – Data Structures Abstract Data Types
Data Structure Dr. Mohamed Khafagy.
Doubly Linked List Review - We are writing this code
EEL 4854 IT Data Structures Linked Lists
Linked Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Doubly Linked List Implementation
Recursive Linked List Operations
Chapter 17: Linked Lists.
CSC 143 Java Linked Lists.
Doubly Linked List Implementation
Linked Lists.
The List Container and Iterators
Presentation transcript:

1 Joe Meehean

 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

 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

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

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

 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

ant bat n: cat jar tmp: 7 phead:

ant bat n: cat jar tmp: 8 phead:

ant bat n: cat jar tmp: 9 phead:

ant phead: bat cat jar n: 10

ant bat cat jar n: 11 phead:

ant bat cat jar n: 12 phead:

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

ant phead: bat cat 14 tmp:

ant phead: bat cat 15 tmp:

ant phead: bat cat 16 tmp:

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

ant phead: bat cat 18 tmp:

ant phead: bat cat 19 tmp:

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

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

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

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

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

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

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

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

Header phead: ant bat 29 ptail:

Header phead: ant bat 30 ptail:

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

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:

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

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

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

Header phead: bat Tail 36 ptail:

Header phead: bat Tail 37 ptail:

Header phead: bat Tail 38 ptail:

Header phead: bat Tail 39 ptail:

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

 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

 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

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

44

 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

 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

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

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

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

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

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

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

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

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

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

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

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

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

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

 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

 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