 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.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Advertisements

Ceng-112 Data Structures I Chapter 5 Queues.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Data Structures: A Pseudocode Approach with C
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
1 Linked List (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Helpful C++ Transitions
Data Structures Using C++ 2E
 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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Introduction to Data Structures Systems Programming Concepts.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 16: Linked Lists.
Chapter 20 Custom Templatized Data Structures
C++ Programming:. Program Design Including
Chapter 12 – Data Structures
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
Linked Lists Chapter 6 Section 6.4 – 6.6
12 C Data Structures.
CISC181 Introduction to Computer Science Dr
Intro to Data Structures
Chapter 4 Linked Lists.
Traversing a Linked List
Introduction to Data Structures
Pointers and Linked Lists
Array Lists Chapter 6 Section 6.1 to 6.3
Helpful C++ Transitions
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
DATA STRUCTURE QUEUE.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Review & Lab assignments
Chapter 4 Linked Lists.
Linked List and Selection Sort
Chapter 17: Linked Lists.
Linked List Functions.
Andy Wang Object Oriented Programming in C++ COP 3330
21 Data Structures.
Presentation transcript:

 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 ListNode object in the linked list Declare class List as a friend

 2008 Pearson Education, Inc. All rights reserved. 2

3 private data members firsrtPtr (a pointer to the first ListNode in a List ) and lastPtr (a pointer to the last ListNode in a List ) This private utility function is the key to the ‘friendship’.

 2008 Pearson Education, Inc. All rights reserved. 4 Initialize both pointers to 0 (null) Ensure that all ListNode objects in a List object are destroyed when that List object is destroyed

 2008 Pearson Education, Inc. All rights reserved. 5 Places a new node at the front of the list Use function getNewNode to allocate a new ListNode containing value and assign it to newPtr If the list is empty, then both firstPtr and lastPtr are set to newPtr Thread the new node into the list so that the new node points to the old first node and firstPtr points to the new node Places a new node at the back of the list Use function getNewNode to allocate a new listNode containing value and assign it to newPtr If the list is empty, then both firstPtr and lastPtr are set to newPtr Thread the new node into the list so that the old last node points to the new node and lastPtr points to the new node

 2008 Pearson Education, Inc. All rights reserved. 6 Removes the front node of the list and copies the node value to the reference parameter Return false if an attempt is made to remove a node from an empty list Save a pointer to the first node, which will be removed If the list has only one element, leave the list empty Set firstPtr to point to the second node (the new first node) Copy the removed node ’ s data to reference parameter value delete the removed node

 2008 Pearson Education, Inc. All rights reserved. 7 Removes the back node of the list and copies the node value to the reference parameter Return false if an attempt is made to remove a node from an empty list Save a pointer to the last node, which will be removed If the list has only one element, leave the list empty Assign currentPtr the address of the first node to prepare to “ walk the list ” “ Walk the list ” until currentPtr points to the node before the last node, which will be the new last node Make the currentPtr node the new last node Copy the removed node ’ s data to reference parameter value delete the removed node

 2008 Pearson Education, Inc. All rights reserved. 8 Determine whether the List is empty Return a dynamically allocated ListNode object

 2008 Pearson Education, Inc. All rights reserved. 9 Iterate through the list and output the value in each node

 2008 Pearson Education, Inc. All rights reserved. 10

 2008 Pearson Education, Inc. All rights reserved. 11

 2008 Pearson Education, Inc. All rights reserved. 12

 2008 Pearson Education, Inc. All rights reserved. 13

 2008 Pearson Education, Inc. All rights reserved. 14

 2008 Pearson Education, Inc. All rights reserved. 15