1 Linked List (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 20 - Data Structures Outline 20.1 Introduction 20.2 Self-Referential Classes 20.3 Dynamic Memory.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to Data Structures Systems Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
 2008 Pearson Education, Inc. All rights reserved. 1 Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Introduction to Data Structures Systems Programming Concepts.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - Data Structures Outline 17.1 Introduction 17.2 Self-Referential Classes 17.3 Dynamic Memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Templates Lecture 10 March 23, 2004.
Programming Practice 3 - Dynamic Data Structure
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
CISC181 Introduction to Computer Science Dr
Intro to Data Structures
Introduction to Data Structures
Chapter 4 Linked Lists
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Programming Abstractions
Chapter 4 Linked Lists.
Linked Lists.
CSI 1340 Introduction to Computer Science II
Andy Wang Object Oriented Programming in C++ COP 3330
List as an Abstract Data Type
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 20 - Data Structures
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
21 Data Structures.
Presentation transcript:

1 Linked List (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series

2 Self-Referential Classes self-referential class –class that contains a pointer to a class object of the same type –can be linked together to form useful data structures such as lists, queues, stacks and trees –terminated with a NULL pointer (0) Two self-referential class objects linked together 1015 NULL pointer (points to nothing) Data member and pointer

3 Node Class class Node { public: Node( const int & ); // constructor int getData() const; // return data in the node int data; // data. Actually, it can be other types or classes Node *nextPtr; // next node in the list }; // Constructor Node::Node( const int &info ) : data( info ), nextPtr( 0 ) { } // Return a copy of the data in the node Node::getData() const { return data; }

4 Linked Lists linked list –linear collection of self-referential class objects, called nodes, connected by pointer links –accessed via a pointer to the first node of the list –subsequent nodes are accessed via the link-pointer member –the link pointer in the last node is set to null to mark the list’s end Use a linked list instead of an array when –the number of data elements is unpredictable –the list needs to be sorted

5 Linked List H E L L firstPtr lasttPtr O

6 class List { public: List(); // constructor ~List(); // destructor voidinsertAtFront( const int & ); voidinsertAtBack( const int & ); boolremoveFromFront( int & ); boolremoveFromBack( int & ); boolisEmpty() const; voidprint() const; int Length() const; Node&Search( const int& data) const; Node&Index(const int& index); Node&operator[](const int& index); boolinsertAfter( const int& data, const int& pos); private: Node *firstPtr; // pointer to first node Node *lastPtr; // pointer to last node Node *getNewNode( const int & ); // Utility to allocate a new node };

7 // constructor List::List() : firstPtr( 0 ), lastPtr( 0 ) { // empty } // Is the List empty? bool List::isEmpty() const { return firstPtr == 0; }

8 // Return a pointer to a newly allocated node Node *List::getNewNode( const int &value ) { Node *ptr = new Node( value ); // look at this! assert( ptr != 0 ); return ptr; } // Display the contents of the List void List::print() const { if ( isEmpty() ) { cout << "The list is empty\n\n"; return; } Node *currentPtr = firstPtr; cout << "The list is: "; while ( currentPtr != 0 ) { cout data << ' '; currentPtr = currentPtr->nextPtr; // like the increment operation } cout << "\n\n"; }

9 void List::insertAtFront( const int & value) { Node *newPtr = getNewNode(value); if ( isEmpty() ) // List is empty: trivial case firstPtr = lastPtr = newPtr; else { // List is not empty: non-trivial case newPtr->nextPtr = firstPtr; // step 1 firstPtr = newPtr; // step 2 } void List::insertAtBack( const int& value) { Node *newPtr = getNewNode(value); if ( isEmpty() ) // List is empty: trivial case firstPtr = lastPtr = newPtr; else { // List is not empty: non-trivial case lastPtr->nextPtr = newPtr; // step 1 lastPtr = newPtr; // step 2 }

10 insertAtFront 5 newPtr 79 firstPtr if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; }

11 insertAtBack 5 newPtr 79 firstPtr lastPtr if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; }

12 bool List::removeFromFront( int & value) { if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { Node *tempPtr = firstPtr; // step I: remember what to delete if ( firstPtr == lastPtr ) // trivial case firstPtr = lastPtr = 0; else // non-trivial case firstPtr = firstPtr->nextPtr; // step II value = tempPtr->data; // step III: retrieve data (option) delete tempPtr; // step IV: recycle return true; // delete successful } Question: what if I don’t do step I?

13 firstPtrlastPtr 7953 removeFromFront tempPtr if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { Node *tempPtr = firstPtr; // step I if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; // step II value = tempPtr->data; // step III: data being removed delete tempPtr; // step IV return true; // delete successful }

14 bool List::removeFromBack( int & value) { if ( isEmpty() ) return false; // delete unsuccessful else { Node *tempPtr = lastPtr; // step I: remember what to delete if ( firstPtr == lastPtr ) // trivial case firstPtr = lastPtr = 0; else { // non-trivial case Node *currentPtr = firstPtr; // step II: find “next to last” while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; // step III: currentPtr->nextPtr = 0; // step IV: “housekeeping” } value = tempPtr->data; // step V: retrieve data deleted delete tempPtr; // step VI: recycle return true; // delete successful } Note: The most important part is step II.

15 removeFromBack firstPtrlastPtr 7953 tempPtrcurPtr if ( isEmpty() ) return false; // delete unsuccessful else { Node *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { Node *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } value = tempPtr->data; delete tempPtr; return true; // delete successful }

16 Questions How to delete the list? How do I know the length of the list? How to index? How can we search a node? How to insert a node in between?

17 Destructor Can we just ? List::~List() { delete firstPtr; }

18 curPtr 7953 Destruction tempPtr if ( !isEmpty() ) { // List is not empty Node *curPtr = firstPtr, *tempPtr; while ( curPtr != 0 ) { tempPtr = curPtr; curPtr = curPtr->nextPtr; delete tempPtr; }

19 List::~List() { if ( !isEmpty() ) { // List is not empty cout << "Destroying nodes...\n"; Node *currentPtr = firstPtr, *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes tempPtr = currentPtr; cout data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } cout << "All nodes destroyed\n\n"; } ~List()

20 void main() { List mylist; instructions(); int choice, value; do { cout > choice; switch ( choice ) { case 1: cin >> value; mylist.insertAtFront( value ); mylist.print(); break; case 2: cin >> value; mylist.insertAtBack( value ); mylist.print(); break; case 3: if ( mylist.removeFromFront( value ) ) cout << value << " removed from list\n"; mylist.print(); break; case 4: if ( mylist.removeFromBack( value ) ) cout << value << " removed from list\n"; mylist.print(); break; } } while ( choice != 5 ); } void instructions() { cout << "Enter one of the following:\n" << " 1 to insert at beginning of list\n" << " 2 to insert at end of list\n" << " 3 to delete from beginning of list\n" << " 4 to delete from end of list\n" << " 5 to end list processing\n"; }

21 Testing a List of integer values Enter one of the following: 1 to insert at beginning of list 2 to insert at end of list 3 to delete from beginning of list 4 to delete from end of list 5 to end list processing ? 1 1 The list is: 1 ? 1 2 The list is: 2 1 ? 2 3 The list is: ? 2 4 The list is: ? 3 2 removed from list The list is: ? 3 1 removed from list The list is: 3 4 ? 4 4 removed from list The list is: 3 ? 4 3 removed from list The list is empty ? 5 End list test

22 The Length? int List::Length( ) { int length = 0; Node *curPtr = firstPtr; while(curPtr != NULL){ length ++; curPtr = curPtr->nextPtr; } return length; }

23 Index the list? Node& List::Index(const int& index) { int count = index; Node *curPtr = firstPtr; Node *dPtr = NULL;// WHY? while(curPtr != NULL){ if (count == 0){ dPtr = curPtr; break; } count --; curPtr = curPtr->nextPtr; } if (dPtr == NULL)cout << “out of range!\n” ; return (*dPtr);// WHY? }

24 A more intuitive way Node& List::operator[](const int & index) { return Index(index); } Why don’t I overload an operator?

25 Search for a node? Give a specific data Want to check if the data is inside the list Return the address of the node if inside Return NULL otherwise

26 Search? Node& List::Search( const int& data) { Node *dPtr = 0; // NULL is 0 Node *curPtr = firstPtr; while(curPtr != NULL){ if ( curPtr->data == data){ cout << “found!” << endl; dPtr = curPtr; break; } curPtr = curPtr->nextPtr; } return (*dPtr); }

27 Insert in between? Given: –A piece of data –A position of the list Insert the data after the position of the list

28 bool List::insertAfter(const int& data, const int& pos) { Node* curPtr = & (Index(pos)); if (!curPtr){ cout << “Error: insertAfter\n”; return false; } else{ Node* newPtr = getNewNode(data); Node* nPtr = curPtr->nextPtr; curPtr->nextPtr = newPtr; newPtr->nextPtr = nPtr; if ( nPtr == NULL) // i.e., curPtr is the last one lastPtr = newPtr; return true; } Insert? Note: this one has several very good ideas, please study hard on this function!

29 void main() { List mylist; instructions(); int choice, value, pos; Node *dPtr; do { cout << "? "; cin >> choice; switch ( choice ) { case 1: cin >> value; mylist.insertAtFront( value ); mylist.print(); break; case 2: cin >> value; mylist.insertAtBack( value ); mylist.print(); break; case 3: if ( mylist.removeFromFront( value ) ) cout << value << " removed from list\n"; mylist.print(); break;

30 case 4: if ( mylist.removeFromBack( value ) ) cout << value << " removed from list\n"; mylist.print(); break; case 5: cin >> value; dPtr = &mylist.Search( value); if (dPtr) cout getData() << " found in the list\n"; else cout << value << " is not inside the list\n"; break; case 6: cin >> pos; // for indexing dPtr = &mylist[pos]; if(dPtr) cout << "List["<<pos<<"]="<< mylist[pos].getData() << endl; else out << "wrong!" << endl; break;

31 case 7: cin >> value >> pos; if (mylist.insertAfter(value, pos)){ cout << "insert " << value << " after " << pos << " done\n"; mylist.print(); } else cout << "can not be insert!\n"; break; default: break; } } while ( choice != 8 ); }

32 Summary What motivates the use of linked list? What defines a Node? What is a List? How to destruct a list? How to search a list? How to insert data into a list?

33 Question to think about What if I want a list for other data types?