Recursive Linked Lists

Slides:



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

COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
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.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
C++ Classes and Data Structures Jeffrey S. Childs
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
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.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Linked Lists and Generics Written by J.J. Shepherd.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
MEMORY REPRESENTATION OF STACKS
Lecture No.06 Data Structures Dr. Sohail Aslam
CISC181 Introduction to Computer Science Dr
Programming Abstractions
Linked Lists with Tail Pointers
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
Introduction to Data Structures
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Evaluation of List Implementations
Circularly Linked Lists
Linked List Intro CSCE 121 J. Michael Moore.
Stack and Queues Stack implementation using Array
Linked Lists [AJ 15].
Programming Abstractions
Linked Lists with Tail Pointers
Linked Lists.
Introduction to Programming
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Building Java Programs
Recursive Linked Lists
Linked Lists Chapter 4.
A list-size member variable
Evaluation of List Implementations
Lecture 16 Section 6.2 Thu, Mar 1, 2007
More Data Structures (Part 1)
CS148 Introduction to Programming II
Data Structures & Algorithms
List Iterator Implementation
General List.
Recursive Linked Lists
slides created by Marty Stepp
CS148 Introduction to Programming II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked List Improvements
Linked Lists Dr. Jose Annunziato.
Abstract Data Types Stacks CSCI 240
Linked Lists.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Recursive Linked Lists Lecture 35 Sections 10.3 – 10.4 Fri, Apr 20, 2007

Recursive Linked Lists A linked list is a naturally recursive structure. The “linked list” is a pointer to a node. (Ignore the mSize data member.) Furthermore, each node contains a pointer to a node. Therefore, each node contains a “linked list.” However, the sublists do not have an mSize data member, so they are not exactly LinkedList objects. 5/24/2019 Recursion

Two-Part Implementation of Recursive Member Functions The first function is public and non-recursive. Type Class::Function(parameters) { // Do some special things… // Call the second function, // passing the head of the list Function(head, parameters); // Do more special things… return; } 5/24/2019 Recursion

Implementation of Recursive Member Functions The second function is private and recursive. Type Class::Function(LinkedListNode* node, parameters) { if (condition) // Handle the recursive case return Function(node->next, parameters); } else // Handle the non-recursive case 5/24/2019 Recursion

Example: The insert() Function There are two things that the non-recursive insert() function should do only once. Check that pos is valid. Increment mSize. Then it makes a call to the recursive insert() function, passing it the head pointer. 5/24/2019 Recursion

The Non-Recursive insert() Function void insert(int pos, const T& value) { // Do something special assert(pos >= 1 && pos <= mSize); // Call the recursive function insert(head, pos, value); mSize++; return; } 5/24/2019 Recursion

The Recursive insert() Function The recursive insert() function must distinguish two cases. Recursive case The insertion takes place at a later node. Non-recursive case The insertion takes place at the current node, which is the “head” of the (sub)list. 5/24/2019 Recursion

The Recursive Case The recursive case Is distinguished by the condition that pos > 1. Passes a pointer to the next node. Decrements the value of pos. insert(node->next, pos – 1, value); 5/24/2019 Recursion

The Non-Recursive Case Is distinguished by the condition pos == 1. Inserts the new node at the head of the (sub)list. Because node is modified, it must be passed by reference. LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->next = node; node = new_node; 5/24/2019 Recursion

The Recursive insert() Function void insert(LinkedListNode<T>*& node, int pos, const T& value) { // Recursive case if (pos > 1) Insert(node->next, pos – 1, value); // Non-recursive case else LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->next = node; node = new_node; } return; 5/24/2019 Recursion

Recursive Linked List Implementation Example recurlinkedlist.h ListTest.cpp 5/24/2019 Recursion

Evaluation of Lists Type of List Get Element (seq) (rand) Insert Delete Push Front Pop Back Array List w Iterator 0.05 0.29 14.6 15.9 28.2 31.0 0.15 0.23 Linked List w Iterator 0.10 8.91 16.8 20.1 0.69 32.5 62.6 Circ Linked List w Iterator 0.13 5.00 8.28 9.28 0.75 0.63 0.76 0.61 Recur Linked List 35.1 35.7 71.2 57.9 0.73 0.71 147.6 115.5 5/24/2019 Recursion