Linked Lists Chapter 4.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
© 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.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
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.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
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
C++ Programming:. Program Design Including
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
UNIT-3 LINKED LIST.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
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
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
LINKED LISTS CSCD Linked Lists.
Array Lists Chapter 6 Section 6.1 to 6.3
Linked Lists Chapter 4.
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Lists CSE 373 Data Structures.
Data Structures and Algorithms
Problem Understanding
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists Chapter 5 (continued)
Lists CSE 373 Data Structures.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Problem Understanding
Presentation transcript:

Linked Lists Chapter 4

Chapter 4 -- Linked Lists This chapter introduces you to C++ pointers and the data structure linked list. You will see algorithms for fundamental linked list operations such as insertion and deletion. The chapter also describes several variations of the basic linked list. As you will see you can use a linked list and its variations when implementing many of the ADTs that appear throughout the remainder of this book CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Preliminaries Issues with Array based implementations of Lists: Insert and Delete require shifting CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Pointers A pointer is a data type A variable can be declared to be a pointer. int *p; It stores an address. Therefore you have to get the address of something int q; p= &q; It has operations. *p=6; CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Example: (a) declaration (b) & (c) * (d) new CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Example (cont): CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Example (cont): CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Example: Pointers also have issues Creation of Garbage Dangling Pointers CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Dynamic Allocation of Arrays Normal allocation (automatic or static) const int MAX_SIZE = 50; double anArray[MAX_SIZE]; Dynamic Allocation int arraySize=50; double *anArray = new double[arraySize]; CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Deleting the memory delete [ ] anArray; The name of the array is equivalent to the address of the first element. anArray == &(anArray[0]) *anArray == anArray[0] How do you allocate a 2D array dynamically? CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Pointer-Based Linked Lists Nodes: Declaration struct Node { int item; Node *next }; Allocation Node *p; p = new Node; CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Usage (*p).item = 3 p->item = 3 Head Node Declaration: Node *head; CS 308 Chapter 4 -- Linked Lists

Programming with Linked Lists The previous section illustrated how you can use pointer variables to implement a linked list This section begins by developing algorithms for displaying the data portions of such a linked list. For inserting and deleting items from a list. CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Displaying the Contents of a Linked List Suppose we have a list How do we display it? The key is making a traversal of the nodes CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Deleting a Specified Node from a Linked List General case Special case (prev == NULL) CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Inserting a Node into a Specified Position of a Linked List General case Special Case (prev == NULL) CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Special Case (curr == NULL) CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists A Pointer Based Implementation of the ADT List Code was covered last semester, and in Lab 9 Key is shallow vs deep copy CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Comparing Array-Based and Pointer-Based Implementations Storage: Arrays are easy to use, but they have a fixed size An array based implementation is good for a small list. Access You can access array items directly with equal access time. You must traverse a linked list to access its ith node Insertion and Deletion Insertion and deletion from a linked list do not require you to shift the data (they do require traversal) CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Saving and Restoring a Linked List by Using a File. Notes: Do not write the pointers to the file Write only its data to a file. Saving: Traverse the list and write out the data Restoring Read the data and insert at the end of the list CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Passing a Linked List to a Function A function with access to a linked list’s head pointer has access to the entire list. Pass the head pointer as a reference argument Note: a linked list passed as an argument is not copied, even if its head pointer is passed as a value argument. CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Processing Linked Lists Recursively Traversal void writeList(Node * nodeptr) Insertion void linkedListInsert(Node * & headPtr, ListItemType newItem); CS 308 Chapter 4 -- Linked Lists

Variations of the Linked List This section introduces several variations of the linked list that you have just seen. Circular Linked Lists Dummy Head nodes Doubly Linked Lists CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Circular Linked Lists CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Dummy Head Nodes CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Doubly Linked Lists CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists Doubly Linked Lists Insertion Deletion CS 308 Chapter 4 -- Linked Lists

Chapter 4 -- Linked Lists CS 308 Chapter 4 -- Linked Lists