Linked Data Structures

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CHAPTER 7 Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
12-1 Priority Queues, Sets, and Bags Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 12 Monday, March 12 Handout #22.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
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.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
CSE 1342 Programming Concepts
Elementary Data Structures
Review Array Array Elements Accessing array elements
The List ADT.
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Pointers and Linked Lists
QueueStack CS1020.
Week 4 - Friday CS221.
Doubly Linked List Review - We are writing this code
Stacks and Queues CMSC 202.
Stacks and Queues.
Queues Queues Queues.
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Stack and Queue APURBO DATTA.
Java collections library
Chapter 17 Object-Oriented Data Structures
Top Ten Words that Almost Rhyme with “Peas”
Topic 16 Queues "FISH queue: n.
Topic 16 Queues Adapted from Mike Scott’s materials.
CMSC 341 Lecture 5 Stacks, Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Linked Lists, Stacks and Queues Textbook Sections
Topic 16 Queues "FISH queue: n.
Data Structures ADT List
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues.
Stacks, Queues, and Deques
Ordered Structures Wellesley College CS230 Lecture 10
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Presentation transcript:

Linked Data Structures

Linked lists Linked data structure = capsules of data (nodes) that are connected via links (Singly) linked list = linear structure of nodes connected via a single link to the next element First node is called the head node Last node is called the tail node

Linked lists

Linked lists A (link or next) value of null is typically used to represent the end of the list. An empty list is one that starts with a null value (the head is null).

Integer node definition public class Node { private int mData; Node mNext; //AKA link //next, we need a ctor(s) } What access is this?

Integer node definition public class Node { private int mData; private Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } Why don’t we have a no-arg ctor? What does an empty list look like?

Add a node to the beginning of the list public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public class Main { public static void main ( String args[] ) { Node head = null; Node n = new Node( 13 ); //add at beginning n.mNext = head; head = n; }

Add another node to the beginning of the list public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public class Main { public static void main ( String args[] ) { Node head = null; Node n = new Node( 13 ); //add at beginning n.mNext = head; head = n; n = new Node( 12 ); } Can you draw a picture of these data structures in memory?

Add a toString method public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; }

Add a toString method Why is this necessary? public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData; public class Main { public static void main ( String args[] ) { Node head = null; Node n = new Node( 13 ); //add at beginning n.mNext = head; head = n; n = new Node( 12 ); System.out.println( head ); } Why is this necessary?

Add a toString method Something interesting happens! What? public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class Main { public static void main ( String args[] ) { Node head = null; Node n = new Node( 13 ); //add at beginning n.mNext = head; head = n; n = new Node( 12 ); System.out.println( head ); } Something interesting happens! What?

How can we make this more OO? public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class Main { public static void main ( String args[] ) { Node head = null; Node n = new Node( 13 ); //add at beginning n.mNext = head; head = n; n = new Node( 12 ); System.out.println( head ); }

How can we make this more OO? public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class MyLinkedList { private Node mHead = null; } public class Main { public static void main ( String args[] ) { MyLinkedList mml = new MyLinkedList(); } //end class Main

Add a toString method public class MyLinkedList { public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class MyLinkedList { private Node mHead = null; public String toString ( ) { return “” + mHead; } public class Main { public static void main ( String args[] ) { MyLinkedList mml = new MyLinkedList(); } //end class Main

Next, let’s add an add method public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class MyLinkedList { private Node mHead = null; public void add ( int n ) { … } public void add ( Node n ) { public String toString ( ) { return “” + mHead;

Next, let’s add an add method public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class MyLinkedList { private Node mHead = null; public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void add ( Node n ) { … public String toString ( ) { return “” + mHead; Any potential privacy leak?

Next, let’s add an add method public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } public String toString ( ) { return "" + mData + “ ” + mNext; public class MyLinkedList { private Node mHead = null; public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void add ( Node n ) { n.mNext = mHead; mHead = n; public String toString ( ) { return “” + mHead; Any potential privacy leak?

Next, let’s add an add method public class Node { private int mData; Node mNext; //ctor public Node ( int value ) { mData = value; mNext = null; } //accessor public int getData ( ) { return mData; public String toString ( ) { return "" + mData + “ ” + mNext; public class MyLinkedList { private Node mHead = null; public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void add ( Node n ) { n = new Node( n.getData() ); n.mNext = mHead; mHead = n; public String toString ( ) { return “” + mHead; Corrected potential privacy leak.

Precondtion(s) What precondition is important here? public class MyLinkedList { private Node mHead = null; public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void add ( Node n ) { n = new Node( n.getData() ); n.mNext = mHead; mHead = n; public String toString ( ) { return “” + mHead; What precondition is important here?

Analysis of algorithm – computational complexity public class MyLinkedList { private Node mHead = null; public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void add ( Node n ) { n = new Node( n.getData() ); n.mNext = mHead; mHead = n; public String toString ( ) { return “” + mHead; Is either add method dependent on the current size of the list?

Add to the tail of the list public class MyLinkedList { private Node mHead = null; public void addTail ( Node n ) { if (mHead==null) { … } else { }

Add to the tail of the list public class MyLinkedList { private Node mHead = null; public void addTail ( Node n ) { if (mHead==null) { n.mNext = null; mHead = n; } else { … }

Add to the tail of the list public class MyLinkedList { private Node mHead = null; public void addTail ( Node n ) { if (mHead==null) { n.mNext = null; mHead = n; } else { Node temp = mHead; while (temp.mNext!=null) { temp = temp.mNext; } temp.mNext = n; … Is the addTail method dependent on the current size of the list? Convert this while-loop to a for-loop.

Linked list The linked list is so useful that Sun has provided us with one! See http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html There is also a List interface. See http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html

Example of creating a linked list Other useful functions: void clear ( ) Removes all of the elements from this list. boolean contains ( Object o ) Returns true if this list contains the specified element. E get ( int index ) Returns the element at the specified position in this list.

Example of creating a linked list Other useful functions: E remove ( int index ) Removes the element at the specified position in this list. boolean remove ( Object o ) Removes the first occurrence in this list of the specified element. int size ( ) Returns the number of elements in this list.

Example of creating a linked list Other useful functions: void add ( int index, E element ) Inserts the specified element at the specified position in this list. E set ( int index, E element ) Replaces the element at the specified position in this list with the specified element. boolean isEmpty ( ) Returns true if this list contains no elements.

Variations on a theme (different types of lists) Singly linked Circular

Variations on a theme (different types of lists) Doubly linked

Our linked list and an inner class Often the user doesn’t need to know about the Node class. Let’s modify MyLinkedList to use an inner class for the Node.

Our linked list and an inner class public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void addTail ( int value ) { Node n = new Node( value ); if (mHead==null) { n.mNext = null; mHead = n; } else { Node temp = mHead; while (temp.mNext!=null) { temp = temp.mNext; temp.mNext = n; public String toString ( ) { return "" + mHead; } } //end class MyLinkedList2 public class MyLinkedList2 { // - - - - - - - - - private class Node { private int mData; private Node mNext; public Node ( int value ) { //ctor mData = value; mNext = null; } public String toString ( ) { return "" + mData + " " + mNext; } //end class Node private Node mHead = null;

Our linked list and an inner class public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } … public int removeHead ( ) { ? public String toString ( ) { return "" + mHead; } //end class MyLinkedList2 public class MyLinkedList2 { // - - - - - - - - - private class Node { private int mData; private Node mNext; public Node ( int value ) { //ctor mData = value; mNext = null; } public String toString ( ) { return "" + mData + " " + mNext; } //end class Node private Node mHead = null;

Our linked list and an inner class public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } … public int removeHead ( ) { int temp = mHead.mData; mHead = mHead.mNext; return temp; public String toString ( ) { return "" + mHead; } //end class MyLinkedList2 public class MyLinkedList2 { // - - - - - - - - - private class Node { private int mData; private Node mNext; public Node ( int value ) { //ctor mData = value; mNext = null; } public String toString ( ) { return "" + mData + " " + mNext; } //end class Node private Node mHead = null; But wait! How can we access these private members?

Finally, how can we convert it to a generic? public void add ( int n ) { Node temp = new Node( n ); temp.mNext = mHead; mHead = temp; } public void addTail ( int value ) { Node n = new Node( value ); if (mHead==null) { n.mNext = null; mHead = n; } else { Node temp = mHead; while (temp.mNext!=null) { temp = temp.mNext; temp.mNext = n; public String toString ( ) { return "" + mHead; } } //end class MyLinkedList2 public class MyLinkedList2 { // - - - - - - - - - private class Node { private int mData; private Node mNext; public Node ( int value ) { //ctor mData = value; mNext = null; } public String toString ( ) { return "" + mData + " " + mNext; } //end class Node private Node mHead = null;

Finally, how can we convert it to a generic? public void addTail ( T value ) { Node<T> n = new Node<T>( value ); if (mHead==null) { n.mNext = null; mHead = n; } else { Node<T> temp = mHead; while (temp.mNext!=null) { temp = temp.mNext; } n.mNext = null; temp.mNext = n; } } public T removeHead ( ) { T temp = mHead.mData; mHead = mHead.mNext; return temp; } public String toString ( ) { return "" + mHead; } } //end class MyLinkedList3 public class MyLinkedList3<T> { private class Node<T> { private T mData; private Node<T> mNext; public Node ( T value ) { //ctor mData = value; mNext = null; } public String toString ( ) { return "" + mData + " " + mNext; } } //end class Node private Node<T> mHead = null; public void add ( T n ) { Node<T> temp = new Node<T>( n ); temp.mNext = mHead; mHead = temp; }

Using our generic, MyLinkedList3<T> MyLinkedList3<Integer> mml3 = new MyLinkedList3<Integer>(); mml3.add( 192 ); System.out.println( mml3 );

Stack A stack is a data structure with the major operations of: push – adds an item to the head pop – removes an item from the head isEmpty – true when the stack is empty; false otherwise A stack is also described as a LIFO (Last In, First Out)

Stack

Queue A queue is a data structure with the major operations of: enq (enqueue) – adds an item to the tail deq (dequeue) – removes an item from the head isEmpty – true when the queue is empty; false otherwise A queue is also described as a FIFO (First In, First Out)

Queue

Sets “A set is a collection of elements in which order and multiplicity are ignored.” (Note: Bags or multisets may have duplicate elements.) A simple implementation of sets is a linked list.

Set operations Add elements – add a new item (an item that does not already exist) into a set Contains – determine if a target item is a member of the set Union – return a set that is the union (a new set of items from either set) of two sets Intersection – return a set that is the intersection (a new set where each item is contained in both sets) of two sets Given a particular set implementation, can you determine O (big-oh) for each operation?

Additional set operations An iterator so that every item can be retrieved from a set Remove an item from the set Obtain the cardinality (the number of items in the set) of the set Given a particular set implementation, can you determine O (big-oh) for each operation?