Lecture%2012%20-%20%20Doubly%20Linked%20Lists%20and%20Recursion

Lecture%2012%20-%20%20Doubly%20Linked%20Lists%20and%20Recursion

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 12 Doubly Linked Lists (with Recursion) In this lecture Introduction to Doubly linked lists What is recursion? Designing a node of a DLL Recursion and Linked Lists o Finding a node in a LL (recursively) o Printing a LL (recursively) o Appending a node to a LL (recursively) o Reversing a LL (recursively) Further Readings Exercises Introduction to Doubly Linked Lists As we learnt before, singly linked list is a structure that contains at least two fields, data field and a pointer to the next node. Singly linked lists are flexible structures where memory can be allocated in “small” blocks as needed. Also, when deleting or inserting nodes from a singly linked list, the overhead is relatively low compared to array insertions where array elements have to be moved with a greater cost. This makes singly linked lists (LL) preferred data structures to use in many applications. However, one drawback in LL is that list can only be navigated in one direction and all navigations must start from the head node. But imagine an application where nodes needs to be inserted quickly, in order, and the data file typically provides clusters of data that needs to be inserted close to each other. It is quite common in data sets to have related data close to each other. For example, think of a user database where names are almost sorted and therefore needs to be inserted next to each other. In this case, it makes sense for the current pointer to remain in place until the next value is read from the data set. Based on the value of the date set, the current pointer can move forward or backwards to insert data in correct place. A doubly linked list is a collection of objects linked together by references from one object to another object, both forward and backward. By convention these objects are named as nodes . So the basic DLL is a collection of nodes where each node contains one or more data fields AND references to next node and previous node . The next pointer of the last node points to a NULL reference and the previous pointer of first node points to NULL to indicate the end of the list in both ways.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyright @ 2009 Ananda Gunawardena The entry point into a DLL is always the first or head of the list. It should be noted that head is NOT a separate node, but a reference to the first Node in the list. If the list is empty, then the head has the value NULL. Unlike Arrays, nodes cannot be accessed by
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2009 for the course CS 123 taught by Professor Bajkzek during the Fall '08 term at Carnegie Mellon.

Page1 / 6

Lecture%2012%20-%20%20Doubly%20Linked%20Lists%20and%20Recursion

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online