Recursive Linked List Operations

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
More on the STL vector list stack queue priority_queue.
1 Standard Containers: Lists Gordon College Resource:
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.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Lecture 7 : Intro. to STL (Standard Template Library)
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Lab - 11 Keerthi Nelaturu. Ordered Structure Using Doubly linked list All elements in the list are arranged in an order Implement the Ordered Structure.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Standard Template Library a collection of useful tools.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Recursion Powerful Tool
Standard Template Library
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
C++ Programming:. Program Design Including
Cpt S 122 – Data Structures Abstract Data Types
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Doubly linked lists.
Recursion.
Object Oriented Programming COP3330 / CGS5409
Recursion & Linked Lists
Chapter 18: Linked Lists.
priority_queue<T>
Chapter 9 One-Dimensional Arrays
Chapter 17: Linked Lists Starting Out with C++ Early Objects
CSCI 3333 Data Structures Linked Lists
Doubly Linked List Implementation
Linked List and Selection Sort
Chapter 17: Linked Lists.
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Evaluation of List Implementations
Doubly Linked List Implementation
The List Container and Iterators
Data Structures & Programming
Presentation transcript:

Recursive Linked List Operations A non-empty linked list consists of a head node followed by the rest of the nodes The rest of the nodes form a linked list that is called the tail of the original list

Recursive Linked List Operations Many linked list operations can be broken down into the smaller problems of processing the head of the list and then recursively operating on the tail of the list

Recursive Linked List Operations To find the length (number of elements) of a list If the list is empty, the length is 0 (base case) If the list is not empty, find the length of the tail and then add 1 to obtain the length of the original list

Recursive Linked List Operations Using recursion to display a list: void displayList(ListNode *myList) { if (myList != NULL) cout << myList->data << " "; displayList(myList->next); } See pr17-07.cpp

Other Recursive Linked List Operations Insert and remove operations can be written to use recursion General design considerations: Base case is often when the list is empty Recursive case often involves the use of the tail of the list (i.e., the list without the head). Since the tail has one fewer entry than the list that was passed in to this call, the recursion eventually stops. See NumberList2.cpp, NumberList2.h, and pr17-08.cpp

The STL list Container template version of a doubly linked list can insert/add elements more quickly than vectors can add efficiently add elements to the end of the list, because of the built-in pointer to the last element See pr17-09.cpp

list Member Functions back returns a reference to the last element in the list erase erases element (or erase elements from first to last) empty returns true if list is empty; false otherwise end returns an iterator to the end of the list front returns a reference to the first element of the list insert inserts an element into the list merge for two sorted lists, insert each element of list 2 into list 1 pop_back removes the last element of the list pop_front removes the first element of the list push_back inserts an element at the end of the list push_front inserts an element at the beginning of the list reverse reverses the order in which the elements appear in the list size returns the number of elements in the list swap swaps the elements stored in two lists unique eliminates duplicate values by eliminating any element that has the same value as the element before it See pr17-09.cpp