The Singly-Linked Structure

Slides:



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

1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
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.
Linked Lists Chapter 4.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.
Data Structures: A Pseudocode Approach with C
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
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.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Chapter 5 Linked Lists II
CS2006- Data Structures I Chapter 5 Linked Lists III.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Link-Based Implementations
Linked List, Stacks Queues
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Stack: a Linked Implementation
Stacks II David Lillis School of Computer Science and Informatics
Lecture 6 of Computer Science II
Computer Engineering Department Islamic University of Gaza
Chapter 4 Linked Structures.
Linked Lists.
Data Structures and Design in Java © Rick Mercer
Anatomy of a linked list
Data Structures and Design in Java © Rick Mercer
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Section 2.5 Introduction to Linked Lists
CS2006- Data Structures I Chapter 5 Linked Lists I.
Storage Strategies: Dynamic Linking
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 10: An Array Instance Variable
A Bag Implementation that Links Data
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Chapter 4 Link Based Implementations
Arrays and Linked Lists
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 16 Linked Structures
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.
Linked Lists & Iterators
Linked Lists.
Linked Lists Chapter 5 (continued)
Data Structures & Algorithms
Recursive Objects Singly Linked Lists.
CS410 – Software Engineering Lecture #5: C++ Basics III
Linked Lists.
Linked Lists Chapter 5 (continued)
Topic 11 Linked Lists -Joel Spolsky
Stacks – Cont’d Nour El-Kadri CSI 1101.
Presentation transcript:

The Singly-Linked Structure © Rick Mercer

Store elements stored in a linked structure A linked structure provides an alternative implementation of collection classes "Linked" structures are a collection of nodes Each node stores a reference to an object and a reference to another node first references the linked structure The last element references nothing (we’ll use null) first Ryan Ali Alex

Sequential processing, not direct access like arrays Linked structures store collections sequentially Start at the beginning to find anything Each element has a successor and predecessor except the first and last element The collection of nodes has each storing a reference to some value it will be called data a reference to another node it will be called next Maintains memory on an as needed basis

Store one element that can be referenced from another // Allow elements to be stored in a linked structure private class Node { private E data; // Reference to the element private Node next; // Reference to next node public Node(){ data = null; next = null; } } // end inner class You will be able to access the instance variables data and next from any place inside a class that contains private class Node

Hard code a linked structure One node can be reached from another via references Hard code a linked structure of three nodes: // Build the first node Node first = new Node(); first.data = "Bob"; // Construct a second Node referenced // by the first Node's next first.next = new Node(); first.next.data = "Chris"; // Link a third and fourth node first.next.next = new Node("Yean"); first.next.next.next = new Node("Zorro");

A linked structure with 4 nodes You end up with four linked nodes What are the values of first.data ___ first.next.data _____ first.next.next.data ____ first.next.next.next.next ____

Traverse the linked structure Using the same linked structure, what is the output generated by this code? for(Node ref = first; ref != null; ref = ref.next) { System.out.print(ref.data.toString()); }

Node as an Inner class Other authors use the old-fashioned way to represent a node: a public class with private instance variables public getter and setter methods such as getNext, setNext, setItem, getItem Sun Microsystems added inner classes to Java The enclosing class can access the private instance variables data and next We will access these instance variable directly from the enclosing class

Construct an empty list and isEmpty public class LinkedBag<E> implements Bag<E> { private class Node { private E data; // Reference to the element private Node next; // null, or reference to next node public Node(E element) { // Different constructor data = element; next = null; } // An external reference // to the first element private Node first; // Create an empty Bag public LinkedBag() { first = null; // Explicitly assign null first Empty list reference is null

add Implement add in code to match these pictures of computer memory Consider that adding to an empty list is a special case. Do this only if empty LinkedBag<String> list = new LinkedBag<String>(); list.add("First"); first

// If the List is not empty list.add("Second"); first first first

Is there a better way? Would this work? Simpler with a 2 argument constructor public void add(E element) { first = new Node(element, first); } first