Recursive Linked Lists

Slides:



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

COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Linear Lists – Linked List Representation
Chapter 24 Lists, Stacks, and Queues
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Data Structure Lecture-5
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.
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.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
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 4 Prepared by İnanç TAHRALI.
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.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
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)
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
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
Data Structures and Algorithms
CISC181 Introduction to Computer Science Dr
Stacks and Queues CMSC 202.
Linked Lists with Tail Pointers
LinkedList Class.
Doubly linked lists.
Introduction to Data Structures
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Evaluation of List Implementations
Circularly Linked Lists
Stack and Queues Stack implementation using Array
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Linked Lists with Tail Pointers
Linked Lists.
Introduction to Programming
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Recursive Linked Lists
Data Structures and Algorithms
Linked Lists Chapter 4.
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
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
List Iterator Implementation
Recursive Linked Lists
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked List Improvements
Linked Lists Dr. Jose Annunziato.
Linked Lists.
Variable Storage Memory Locations (Logical) Variable Classes Stack
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Recursive Linked Lists Lecture 29 Sections 10.3 – 10.4 Wed, Apr 2, 2008

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/4/2019 Recursion

Two-Part Implementation of Recursive Member Functions We implement the recursive functions in two parts. 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/4/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/4/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/4/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/4/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/4/2019 Recursion

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

The Non-Recursive Case Is distinguished by the condition pos == 0 (this node). 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/4/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/4/2019 Recursion

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

Evaluation of Lists Type of List Get Element (seq) (rand) Insert Delete Push Front Pop Back Array List 0.020 0.116 39.5 40.9 50.4 47.8 0.035 0.030 Circ Array List 0.015 0.074 12.3 12.2 0.027 0.032 Linked List 23.2 21.5 47.0 57.9 0.183 0.144 52.4 99.2 Linked List w Tail 23.3 21.7 57.1 0.178 0.143 0.163 52.3 Doubly Linked List 13.1 12.7 25.0 30.1 0.171 0.121 0.165 Circ Linked List 13.0 12.8 24.7 29.7 0.168 0.115 0.117 Recur Linked List 53.5 49.3 89.4 91.4 0.132 148 135 5/4/2019 Recursion