Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
1 Linked List (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
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.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Introduction to Data Structures Systems Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
 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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
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.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
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
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
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.
© 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.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
Chapter 20 Custom Templatized Data Structures
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
CSCI-255 LinkedList.
Linked Lists Chapter 6 Section 6.4 – 6.6
12 C Data Structures.
CISC181 Introduction to Computer Science Dr
Intro to Data Structures
Introduction to Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 16-2 Linked Structures
Linked List (Part I) Data structure.
Review & Lab assignments
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
21 Data Structures.
Presentation transcript:

Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series

What have we learned so far? C/C++ data type C/C++ control structures Pointer and reference Dynamic memory allocation Array Function C++ class and object Constructor and destructor

Create an array Suppose the my machine only has 100 bytes of memory. I want to store 10 integers It is easy, right? You can use int numbers[10]; Hold for numbers[]

What if … If some of the memory are being used by other programs, such that I can not find 40 consecutive bytes for the array. Thus, I can not create int number[10] Since there are many memory fragments, I do have more than 40 bytes in the memory! being used

Motivation: drawbacks of array Characteristics of array –Located in consecutive memory spaces –Allocated at compile time –Fixed size of memory space once allocated Pros of array –Fast and convenient data access Cons of array –If we do not know how large size we want, we generally allocate a much larger array, which will be a huge waste. –Array can be full –The size of the array can not be changed

Motivation: question When I am going to implement an array or a list, if I do not know the size in advance and it can be large or small, I want a mechanism of “memory-on- demand”. i.e., when I need some, I will get some from the system; when I am done, I will return the memory back. I will save memory! How? Free lunch? What are sacrificed?

Motivation: computation Two important computational resources –CPU time –Memory Computational complexity –Computation involved –Memory used

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

class Node { public: Node( const int & ); // constructor int getData() const; // return data in the node // private: // should not be private int data; // data 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 int Node::getData() const { return data; } nextPtr - points to an object of type Node –referred to as a link – ties one Node to another Node

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

List H E L L firstPtr lasttPtr O

class List { public: List() : firstPtr(0), lastPtr(0) {}; ~List(); // destructor void insertAtFront(Node & ); void insertAtBack(Node & ); bool removeFromFront(int & ); bool removeFromBack(int & ); bool isEmpty() const { return firstPtr == 0; }; void print() const; private: Node *firstPtr; // pointer to first node Node *lastPtr; // pointer to last node // Utility function to allocate a new node Node *getNewNode( const Node & ); };

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

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

firstPtrlastPtr 7953 removeFromFront tempPtr bool List::removeFromFront(int & value) { 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 }

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 }

Question to think about How can we search a node? How to insert a node in between? How to delete the whole list?