Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.

Slides:



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

Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
Linked Lists.
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Linked Lists Dr. Tim Margush University of Akron © 2009.
Topic 11 Linked Lists -Joel Spolsky
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
More on Linked List Algorithms. Linked List and Template Remember the implementation of a double linked list we learned before. Some of methods are shown.
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,
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 7 Last time: –(A midterm!) –Invariants –Started Linked Lists.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 6 Last time: –Encapsulation –Javadoc documentation –Testing and Debugging.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Week 4 - Friday CS221.
Linked Lists Chapter 5 (continued)
COP3530- Data Structures Advanced Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Sparse Tables A sparse table refers to a table that is populated sparsely by data and most of its cells are empty With a sparse table, the table can be.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
CSC 143 Java Linked Lists.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Stacks, Queues, and Deques
TCSS 143, Autumn 2004 Lecture Notes
Linked Lists Chapter 5 (continued)
Presentation transcript:

Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed. You will need to download a new copy. Solution to assn 2 is posted (you can use this with assn 3). Error in code shown last time for deleting the tail node in a singly linked list. No separate lab exercise this week.

Winter 2006CISC121 - Prof. McLeod2 Singly Linked Lists Singly linked list, so far: –Use of an inner class for the node. –Deleting the head node. –Deleting all nodes. –Searching for, and deleting an inner node. –Deleting the tail node in a singly linked list. Deleting the tail node on a singly linked list is a lot of work – can we have a link to the previous node built in?

Winter 2006CISC121 - Prof. McLeod3 Today Doubly linked lists. Circular lists. Skip lists. Self – organizing lists. Replacing sparse tables with linked lists. LinkList in java.util midterm

Winter 2006CISC121 - Prof. McLeod4 Deleting a Tail Node So how is this going to work? How can the tail pointer be moved up to the preceding node? 15 head 105 null 20 tail

Winter 2006CISC121 - Prof. McLeod5 public int removeTail () { int i = -1; if (!isEmpty()) { i = tail.info; if (head == tail) { head = null; tail = null;} else { IntNode pred = head; IntNode temp = head.next; while (temp.next != null) { pred = pred.next; temp = temp.next; } // end while tail = pred; tail.next = null; } // end else } // end if return i; } // end removeTail method was: temp != null

Winter 2006CISC121 - Prof. McLeod6 Deleting a Tail Node - Cont. Of course, it is unlikely that you will every use that method! Deleting the tail node this way is more time consuming than deleting an inner node. Would it not be nice if the tail node already had a link pointing to the previous node? No problem! Create a doubly linked list.

Winter 2006CISC121 - Prof. McLeod7 Doubly Linked Lists public class IntDLList { private IntDLNode head; private IntDLNode tail; private class IntDLNode { private int info; private IntDLNode next; private IntDLNode prev; // new link! public IntDLNode (int aNum) { this(aNum, null, null); } public IntDLNode (int aNum, IntDLNode n, IntDLNode p) { info = aNum; next = n; prev = p; } } // end IntDLNode // IntDLList constructors and methods } // end IntDLList

Winter 2006CISC121 - Prof. McLeod8 Doubly Linked List – Cont. Structure: head 20 tail null 105 null next prev

Winter 2006CISC121 - Prof. McLeod9 Doubly Linked List – Cont. To add a node to the tail of the list: // better add a constructor too! public IntDLList () { head = null; tail = null; } public boolean isEmpty () { return head == null; } public void addToTail (int aNum) { if (!isEmpty()) { tail = new IntDLNode(aNum, null, tail); tail.prev.next = tail; } else { head = new IntDLNode(aNum); tail = head; } } // end addToTail

Winter 2006CISC121 - Prof. McLeod10 Doubly Linked List – Cont. dLList.addToTail(-10); head 20 tail null 105 null head 20 tail null 105 null -10 null head 20 tail null null -10 null After “ new …”: After tail.prev.next = tail; After tail=…;

Winter 2006CISC121 - Prof. McLeod11 Doubly Linked List – Cont. To remove the tail node: public int removeFromTail () { int i = -1; if (!isEmpty()) { i = tail.info; if (head == tail) { // one node in list head = null; tail = null; } else { tail = tail.prev; tail.next = null; } } // end if return i; } // end removeFromTail

Winter 2006CISC121 - Prof. McLeod12 Doubly Linked List – Cont. int temp = dLList.removeFromTail(); head 20 tail null null After tail = tail.prev; Before head 20 tail null null After tail.next = null; head 20 tail null 105 null -10 null temp is -10

Winter 2006CISC121 - Prof. McLeod13 Doubly Linked List – Cont. Now the removeFromTail method is much easier. So, adding or deleting head or tail nodes is “easy” - which operations will require iteration? Any operation that involves a node other than the head or tail!

Winter 2006CISC121 - Prof. McLeod14 Variations on Linked Lists Circular Lists Skip Lists Self-Organizing Lists

Winter 2006CISC121 - Prof. McLeod15 Circular Lists In a circular list, the last node’s “ next ” value is no longer null – it is linked to the head node. A variable like “ current ” is needed to point to one of the nodes: current

Winter 2006CISC121 - Prof. McLeod16 Circular Lists - Cont. For a doubly linked list: current

Winter 2006CISC121 - Prof. McLeod17 Circular Lists - Cont. While this design seems “elegant”, it is really no improvement over the singly and doubly linked lists, with head and tail, described above. You might use this to model a data structure that does not have a beginning or an end. The structure just grows and shrinks in size. (?) But the next two List variations, on the other hand…

Winter 2006CISC121 - Prof. McLeod18 Skip Lists The biggest problem with linked lists is that they require iteration to locate elements that are not at the head or tail of the list. Even if nodes are ordered, a sequential search is still required. Skip lists were suggested in 1990 as a way to speed up searching. Structured in such a way that every second node contains a link that points two positions ahead, every fourth node has a link to a node four positions ahead, and so on. Each node will contain an array of links:

Winter 2006CISC121 - Prof. McLeod19 Skip Lists – Cont. Node class definition: public class IntSkipListNode { public int info; public IntSkipListNode[] next; public IntSkipListNode (int aNum, int n) { info = aNum; next = new IntSkipListNode[n]; for (int i = 0; i < n; i++) next[i] = null; } } // end IntSkipListNode

Winter 2006CISC121 - Prof. McLeod20 Singly linked skip list: In order to search the skip list, the nodes must be in order by some attribute value. Searching starts by skipping along the highest order links, and then by moving down into the lower order links when the upper order link moves past the target value. Searching is now like the binary search. Skip Lists – Cont

Winter 2006CISC121 - Prof. McLeod21 Skip Lists – Cont. Consider what needs to happen when a node is added or deleted! If the order of links is maintained, then all nodes on one side of the skip list have to be changed. This is very time consuming. If the node is just inserted at the lowest level of the list, and all other nodes are kept the same then the link level order is not maintained. Eventually this will reduce the searching speed to be the same as a sequential search, and the list behaves just as a singly linked list.

Winter 2006CISC121 - Prof. McLeod22 Self-Organizing Lists The idea here is to impose some organizational scheme on the list, in order to speed up searching. Many different organizations can be used, depending on the nature of the data to be stored in the list.

Winter 2006CISC121 - Prof. McLeod23 Self-Organizing Lists - Cont. Examples of organizations: –“Move to front” – when the desired element is located, move it to the front of the list. –“Transpose” – when the element is located, swap it with its predecessor, unless it is already at the head of the list. –“Count” – Order the list by the number of times elements are being accessed. –“Ordering” – Order by some criteria from the information being stored in the list. Choice of organization depends on the how often new elements are added to the list and how often they are accessed.

Winter 2006CISC121 - Prof. McLeod24 Sparse Tables A sparse table is defined as a table where the available cells are only partly populated. For example, consider a table where the columns are student ID’s and the rows are courses taken, for the entire University: –A cell can contain a grade for a course taken by the student. –Of course, not all students take all courses, so only a small portion of each column is taken up with data. –Such a “sparse table” is probably not an efficient use of memory.

Winter 2006CISC121 - Prof. McLeod25 Sparse Tables – Cont. Replace the table by a system of linked lists. Here is one possible design: –Have an ArrayList of course Objects. Each course Object contains all the necessary info about each course and a link to the first student enrolled in the course. –Have an ArrayList of student Objects. Each student Object contains the necessary student information and has a link to the first course taken by that student. –However, make the node design in such a way that the nodes are shared by both lists!

Winter 2006CISC121 - Prof. McLeod26 Sparse Tables – Cont. –Each node contains: student number class number grade code (0 is “A”, 9 is “F”) link to next student link to next course –One node for each course the student has taken. No empty nodes. See the structure on the next slide: Data Links

Winter 2006CISC121 - Prof. McLeod27 1

Winter 2006CISC121 - Prof. McLeod28 Sparse Tables – Cont. (I don’t know why SN and course# have to be in each node in the diagram…) How to navigate through this structure? –Start with a student or start with a course. –But from any given node you can flip the direction of your navigation. At the moment, you have to follow links from oldest to newest in one direction – this would be a painful way of finding recent students in a course that has been around for a while! How would you fix this?

Winter 2006CISC121 - Prof. McLeod29 Sparse Tables – Cont. More efficient use of memory because there are no empty table elements. Uses 17% of the memory used by the sparse table for a typical University setting. Can easily grow as required.

Winter 2006CISC121 - Prof. McLeod30 Linked Lists in java.util java.util contains a class called “ LinkedList ”. ( E is the element type to be stored in the list.) As does the ArrayList class, LinkedList contains many useful pre-defined methods. LinkedList implements a linked list as a “generic doubly-linked list with references to the head and tail”. Many of the LinkedList methods throw Exceptions for illegal parameters. LinkedList only stores “ Objects ” of type E.

Winter 2006CISC121 - Prof. McLeod31 Linked Lists in java.util – Cont. Methods include (“ ob ” is an object of type E ): void add(ob) // adds ob to end of list. void add(pos, ob) // adds ob at pos. void addFirst(ob) // adds ob at beginning of list. void addLast(ob) // same as add(ob). void clear() // removes all objects from the list. boolean contains(ob) // returns true if the list contains ob.

Winter 2006CISC121 - Prof. McLeod32 Linked Lists in java.util – Cont. Object get(pos) // returns the object at pos. Object getFirst() // returns first object in list. Object getLast() // returns last object in list. int indexOf(ob) // returns position of first occurrence of ob, or –1 if ob is not found. boolean isEmpty() // returns true if list is empty, false otherwise.

Winter 2006CISC121 - Prof. McLeod33 Linked Lists in java.util – Cont. Iterator iterator() // generates and returns an iterator for the list. LinkedList() // creates an empty linked list. boolean remove(ob) // removes first occurrence of ob and returns true. Object removeFirst() // removes and returns first element in list.

Winter 2006CISC121 - Prof. McLeod34 Linked Lists in java.util – Cont. Object removeLast() // removes and returns last element in list. int size() // returns number of elements in list.

Winter 2006CISC121 - Prof. McLeod35 Linked Lists - Summary Linked lists really take advantage of Java’s use of Objects, by creating a structure based on pointers. A structure that can be easily tailored to suit the needs of a particular data structure. Only uses the space it needs, no empty data nodes, does not need contiguous memory. Remember to compare linked lists to arrays when choosing a data structure - advantages and disadvantages. (Hint: use diagrams to help write linked list code!)