Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Data Structures Using C++
Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.
Data Structure Lecture-5
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Review Learn about linked lists
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Chapter 4 ADT Sorted List.
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Linked Lists Dr. Youssef Harrath
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Data Structures Using C++ 2E
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
C++ Classes and Data Structures Jeffrey S. Childs
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
Data Structures Using Java1 Chapter 4 Linked Lists.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
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.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Data Structures Using C++1 Chapter 5 Linked Lists.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
Chapter 11 Binary Trees Dr. Youssef Harrath
1 What is a Circular Linked List? l A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first”
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
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.
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.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
C++ Programming:. Program Design Including
C++ Plus Data Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
Recursion.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Doubly Linked Lists CS 308 – Data Structures

Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info

Node data (cont.) template struct NodeType { ItemType info; NodeType * next; NodeType * back; };

Finding a List Item We no longer need to use prevLocation (we can get the predecessor of a node using its back member)

Finding a List Item (cont.)

Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;

FindItem(listData, item, location, found) RetrieveItem, InsertItem, and DeleteItem all require a search ! Write a general non-member function FindItem that takes item as a parameter and returns location and found. InsertItem and DeleteItem need location (ignore found) RetrieveItem needs found (ignores location)

Finding a List Item (cont.) template void FindItem(NodeType * listData, ItemType item, NodeType * &location, bool &found) { // precondition: list is not empty bool moreToSearch = true; location = listData; found = false; while( moreToSearch && !found) { if(item info) moreToSearch = false; else if(item == location->info) found = true; else { if(location->next == NULL) moreToSearch = false; else location = location->next; }

How can we distinguish between the following two cases?

Special case: inserting in the beginning

Inserting into a Doubly Linked List template void SortedType ::InsertItem(ItemType item) { NodeType * newNode; NodeType * location; bool found; newNode = new NodeType ; newNode->info = item; if (listData != NULL) { FindItem(listData, item, location, found); if (location->info > item) { newNode->back = location->back; newNode->next = location; if (location != listData) // special case (location->back)->next = newNode; else listData = newNode; location->back = newNode; } (1) (2) (3) (4) (3)

Inserting into a Doubly Linked List (cont.) else { // insert at the end newNode->back = location; location->next = newNode; newNode->next = NULL; } else { // insert into an empty list listData = newNode; newNode->next = NULL; newNode->back = NULL; } length++; }

Deleting from a Doubly Linked List Be careful about the end cases!!

Headers and Trailers Special cases arise when we are dealing with the first or last nodes How can we simplify the implementation? –Idea: make sure that we never insert or delete the ends of the list –How? Set up dummy nodes with values outside of the range of possible values

Headers and Trailers (cont.) Header Node: contains a value smaller than any possible list element Trailer Node: contains a value larger than any possible list element

A linked list as an array of records What are the advantages of using linked lists? (1) Dynamic memory allocation (2) Efficient insertion-deletion (for sorted lists) Can we implement a linked list without dynamic memory allocation ?

A linked list as an array of records (cont.)

Case Study: Implementing a large integer ADT The range of integer values varies from one computer to another For long integers, the range is [-2,147,483,648 to 2,147,483,647] How can we manipulate larger integers?

Case Study: Implementing a large integer ADT (cont.) - A special list ADT

Exercises 1, 6, 8, 10, 12