CSC 205 Programming II Lecture 15 Linked List – Other Variations.

Slides:



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

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked Lists.
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Lists and the Collections Framework
Data Structures & Algorithms
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.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
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.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linking Objects Together References are particularly important in computer science because they make it possible to represent the relationship among objects.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
COMPSCI 105 SS 2015 Principles of Computer Science
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
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,
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Linked List, Stacks Queues
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Linked Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
public class StrangeObject { String name; StrangeObject other; }
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Doubly Linked Lists or Two-way Linked Lists
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Ch. 5 Linked List When a list is implemented by an array Linked list
Linked List Insert After
Linked Lists Chapter 4.
Problem Understanding
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists Chapter 5 (continued)
CS148 Introduction to Programming II
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Programming II (CS300) Chapter 07: Linked Lists
Linked List Insert After
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

CSC 205 Programming II Lecture 15 Linked List – Other Variations

The First Node – a special case Add to or delete from index = 1 is special You don’t have to find first! head obj1 nObj inserted item obj1obj2 deleted item head Node newNode = new Node(nObj, head); head = newNode; head = head.getNext();

Write a Linked List Backward Two recursive strategies writeListBackward: Write the last node Write the list minus its last node backward writeListBackward2: Write the list minus its first node backward Write the first node obj1 next obj2 null obj2 null obj1 nextheaditem writeListBackwardwriteListBackward2 head

Which One Is More Efficient? writeListBackward2! There is no efficient ways to get the last node There is no efficient ways to get the list minus the last node void writeListBackward2(Node nextNode) { if (nextNode != null) { writeListBackward2(nextNode.getNext()); //write the first node System.out.println(nextNode.getItem()); }

Variations The normal version (singly) linked list (with a head reference) Other variations Circular linked list Doubly linked list with both head and tail references Circular doubly linked list

Circular Linked List The last node has its next referencing the first node ( head ) in the list … obj1obj2obj3objx head //display contents in a circular linked list if (head != null) { Node curr = head; do { System.out.println(curr.getItem()); curr = curr.getNext(); } while (curr != head) }

Doubly Linked List A node has a reference to its successor and one to the node precedes it Node(Object item, Node next, Node precede) … obj1obj2objx head

Circular Doubly Linked List … obj1obj2objx head The one implemented in the java.util package