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

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Linked Lists Geletaw S..
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 1 The Study of Body Function Image PowerPoint
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
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.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists Chapter 4.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Data Structures Using C++
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
CHP-5 LinkedList.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSE Lecture 12 – Linked Lists …
The List ADT Textbook Sections
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Data Structures Using C++ 2E
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
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 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 5 Linked Lists II
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
10/07/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 4 Link Based Implementations
Link Based Implementations
Link-Based Implementations
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Preliminaries Options for implementing an ADT List –Array has a fixed size Data must be shifted during insertions and deletions –Linked list is able to grow in size as needed Does not require the shifting of items during insertions and deletions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Preliminaries Figure 4-1 (a) A linked list of integers; (b) insertion; (c) deletion Linked Lists

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointers A pointer contains the location, or address in memory, of a memory cell –Declaration of an integer pointer variable p int *p; Initially undefined, but not NULL Static allocation Figure 4-2 A pointer to an integer

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointers The expression *p represents the memory cell to which p points To place the address of a variable into a pointer variable, you can use –The address-of operator & p = &x; –The new operator p = new int; Dynamic allocation of a memory cell that can contain an integer If the operator new cannot allocate memory, it throws the exception std::bad_alloc (in the header)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointers The delete operator returns dynamically allocated memory to the system for reuse, and leaves the variables contents undefined delete p; –A pointer to a deallocated memory ( *p ) cell is possible and dangerous p = NULL; // safeguard

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointers Figure 4-3 (a) Declaring pointer variables; (b) pointing to statically allocated memory; (c) assigning a value; (d) allocating memory dynamically; (e) assigning a value

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointers Figure 4-3 continued (f) copying a pointer; (g) allocating memory dynamically and assigning a value; (h) assigning NULL to a pointer variable; (i) deallocating memory

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Dynamic Allocation of Arrays You can use the new operator to allocate an array dynamically int arraySize = 50; double *anArray = new double[arraySize ]; An array name is a pointer to the arrays first element The size of a dynamically allocated array can be increased double *oldArray = anArray; anArray = new double[2*arraySize];

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointer-Based Linked Lists A node in a linked list is usually a struct struct Node { int item Node *next; }; // end Node The head pointer points to the first node in a linked list Figure 4-6 A node Figure 4-7 A head pointer to a list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointer-Based Linked Lists Can also represent a node with a class Class Node { public: // methods private: int item Node *next; }; // end Node Figure 4-6 A node Figure 4-7 A head pointer to a list The head pointer points to the first node in a linked list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointer-Based Linked Lists Must provide getters and setters: Class Node { public: // methods Node(); // the default constructor Node(nodeItem newItem); // parameterized constr bool setItem(nodeItem newItem); nodeItem getItem(); Node* getNext(); bool setNext(Node* newNext); private: int item Node *next; }; // end Node 12

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointer-Based Linked Lists If head is NULL, the linked list is empty A node is dynamically allocated Node *p; // pointer to node p = new Node; // allocate node

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointer-Based Linked Lists Executing the statement head = new Node before head = NULL will result in a lost cell Figure 4-8 A lost cell

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Displaying the Contents of a Linked List Reference a node member with the -> operator p->getItem(); A traverse operation visits each node in the linked list –A pointer variable cur keeps track of the current node for (Node *cur = head; cur != NULL; cur = cur->getNext()) cout getItem()<< endl;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Displaying the Contents of a Linked List Figure 4-9 The effect of the assignment cur = cur->next

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Deleting a Specified Node from a Linked List Deleting an interior node prev->setNext(cur->getNext()); Figure 4-10 Deleting a node from a linked list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Deleting a Specified Node from a Linked List Figure 4-11 Deleting the first node Deleting the first node head = head->getNext();

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Deleting a Specified Node from a Linked List Return deleted node to system cur->setNext(NULL); delete cur; cur = NULL;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Inserting a Node into a Specified Position of a Linked List To insert a node between two nodes newPtr->setNext(cur); prev->setNext(newPtr); Figure 4-12 Inserting a new node into a linked list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Inserting a Node into a Specified Position of a Linked List To insert a node at the beginning of a linked list newPtr->setNext(head); head = newPtr; Figure 4-13 Inserting at the beginning of a linked list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Inserting a Node into a Specified Position of a Linked List Inserting at the end of a linked list is not a special case if cur is NULL newPtr->setNext(cur); prev->setNext(newPtr); Figure 4-14 Inserting at the end of a linked list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Inserting a Node into a Specified Position of a Linked List Finding the point of insertion or deletion for a sorted linked list of objects Node *prev, *cur; for (prev = NULL, cur = head; (cur != NULL) && (newValue > cur->getItem()); prev = cur, cur = cur->getNext());

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Pointer-Based Implementation of the ADT List Figure 4-17 A pointer-based implementation of a linked list

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Pointer-Based Implementation of the ADT List Public methods –isEmpty –getLength –insert –remove –retrieve Private method –find Private data members –head –size Local variables to methods –cur –prev

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Constructors and Destructors Default constructor initializes size and head A destructor is required for dynamically allocated memory List::~List() { while (!isEmpty()) remove(1); } // end destructor

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Constructors and Destructors Copy constructor creates a deep copy –Copies size, head, and the linked list –The copy of head points to the copied linked list In contrast, a shallow copy –Copies size and head –The copy of head points to the original linked list If you omit a copy constructor, the compiler generates one –But it is only sufficient for implementations that use statically allocated arrays

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Shallow Copy vs. Deep Copy Figure 4-18 Copies of the linked list in Figure 4-17; (a) a shallow copy; (b) a deep copy

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Comparing Array-Based and Pointer-Based Implementations Size –Increasing the size of a resizable array can waste storage and time –Linked list grows and shrinks as necessary Storage requirements –Array-based implementation requires less memory than a pointer-based one for each item in the ADT