1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.

Slides:



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

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Singly linked lists Doubly linked lists
DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
CHP-5 LinkedList.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
M180: Data Structures & Algorithms in Java
Skip List & Hashing CSE, POSTECH.
Review Learn about linked lists
Chapter 6 Queue. Learning Objectives ● Describe the behavior of a queue. ● Enumerate the primary operations supported by a queue. ● Learn about the UNIX.
Data Structures: A Pseudocode Approach with C
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
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.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Summary of lectures (1 to 11)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
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.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSC 211 Data Structures Lecture 13
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked List by Chapter 5 Linked List by
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.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Chapter 16: Linked Lists.
Chapter 4 Unordered List.
Lecture 6 of Computer Science II
Chapter 5 Ordered List.
Chapter 6 Queue.
Chapter 4 Unordered List.
Chapter 5 Ordered List.
Chapter 4 Unordered List.
Chapter 5 Ordered List.
Linked Lists.
Chapter 5 Ordered List.
Chapter 4 Unordered List.
Introduction to Data Structure
Presentation transcript:

1 Chapter 4 Unordered List

2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case and average running times. ● Discover how the entries of a list may be dynamically rearranged at achieve better search times. ● Understand the public interface of an unordered list class in Java and the running times of its methods.

3 Learning Objectives ● Develop a set of classes for an expense processing application based on an unordered list. ● Understand how object-oriented programming can be used to write a single piece of code in Java that can perform equality checking based on different criteria for different input objects. ● Learn what linked lists are, why they are useful, and how to build and manipulate them.

4 Learning Objectives ● Implement a linked lest class in Java and analyze the running times of its methods. ● Implement an unordered list class in Java using a linked list component.

5 4.1 Unordered List Properties ● Keeping track of daily expenses.  It would be useful to write a program that maintains an expense list of all recorded expenses, that can be used to find quick answers to simple budgeting type questions.

6 4.1 Unordered List Properties

7

8 ● Answer the following questions:  What is the maximum (or minimum) expense, and on what item?  What is the average expense?  What is the total amount spent on a given item? ● All these question may be answered by scanning such a list from the beginning and terminating when our question is answered.

9 4.1 Unordered List Properties

Sequential Search ● Operation contains searches for a specific itme in the list.  Since the list is unordered, the only way to conduct the search is to look at every element in the sequence.  If a match is found, the operation returns true, otherwise it returns false.

Sequential Search

Sequential Search ● Best case  1 ● Worst case  n ● Unsuccessful search?  n

A List Class ● NoSuchElementException thrown back.

A List Class

A List Class

A List Class ● Enumeration  The items of a List object may be enumerated with a simple device called a cursor. ● To start, a call is made to the first method. ● This sets the cursor at the first item of the list, and returns that item. ● Every subsequent call to the next method moves the cursor to the next item, and returns that item. ● When the cursor is at the end of the list, any subsequent call to next will return null.

A List Class ● Example that enumerates:

A List Class ● Running times  An implementation should be able to access the last item of the list in O(1) time, so that the add method may be implemented in O(1) time.  Maintain a count of the number of items in the list. ● The size method can then simply return this count.  Empty the list in O(1) time.  Use a cursor to enumerate a list, so that each of the enumeration methods first and next may be implemented in O(1) time.

An ExpenseList Class Using List ● An ExpenseList class would support operations for maintaining expenses.  Use the generic List class as a component, implementing all the ExpenseList class methods by reusing code from one or more of the appropriate List class methods.  Every expense will consists of the amount of expense and the item on which the expense was incurred.

Expense Class Interface

Expense Class Interface

Expense Class

Expense Class

ExpenseList Class Interface

ExpenseList Class Interface

ExpenseList Class Implementation

ExpenseList Class Implementation ● minExpense, and aveExpense scan every expense entry in the list.

ExpenseList Class Implementation ● Time requirement is O(n).  amountSpentOn involve sequential search.

ExpenseList Class Implementation ● Returns a matching Expense object from the expense list. ● If they are different, how come they match?

Equality of Objects and Searching ● Rewrite the method by implementing a search in the method.

Equality of Objects and Searching ● The notion of equality is defined by the equals method of the exp object.  Two expenses are equal if they have the same amount and item.  What if we wanted the equality based only on the item so if two expenses have the same item with different amount they are equal.  We would need to redefine the equality of expenses in terms of item only.

Equality of Objects and Searching ● Class Specialization for Special Equality  Define special classes that extend the Expense class, with the sole aim of implementing special, and different, kinds of expenses.

Equality of Objects and Searching ● About Keys  The get method is useful to extract an entire object from the list by matching its key part with a specified key.

Equality of Objects and Searching ● Only use the key part, (ex item )and get returns the entire matching entry (including amount), if any.  What data structure should be used to store the items in a list? ● Removing items from anywhere in the list.  Leaves holes in the array.  Uses more space than necessary.  Search times would be greater than O(n). ● If the holes are patched up by compacting the array, we would be doing a lot of data movement within the array.

Linked List

Linked List ● To access the entries of the linked list, a reference to its first entry is all we need.  One can access any entry by simply following the chain of links.  When an entry is removed from some place in a linked list, all that needs to be done is to have its predecessor's link refer to its successor.  Similarly, an entry may be inserted anywhere in the list without having to move other entries over to create space.

Linked List

Linked List ● The biggest drawback of the linked list is its inability to perform random accesses for any entry in a single step.

Node

Node ● A node is defined in terms of itself:  next field of the node class is a reference to another Node object.  Self-referential structure

Insertion ● Adding to the beginning of the list.

Insertion ● Adding in between two nodes.

Insertion ● Adding to the end of the list

Deletion ● Deleting the first node, the last node, or in- between node.

Deletion ● In both insertion and deletion we assumed the existence of P, a reference to the node just prior to the one to be inserted or deleted.

Access ● Stepping through, or traversing, all the entries of a linked list from beginning to end following the chain of references is a useful operation in practice.

Access ● Deleting the first occurrence of the string “Carrot”.

Access ● We can't delete nextNode unless we have a reference to the node prior to it.

Circular Linked List ● It is useful to have instant access to both the first and the last entries of a linked list.

Circular Linked List ● Given that L refers to the last entry, the first entry is simply L.next.  if L==L.next, that means there is exactly one entry in the CLL.

Circular Linked List ● Insertion  Inserting a new node as the first entry?  Will not work if the list is empty.

Circular Linked List ● Deletion  Deleting the last node.  Wee need to have access to the node preceding it.  Assume that P is this predecessor node, and that it has already been located.  Assumes there are at least two nodes in the list.  The termination condition is that the scanning pointer returns to the starting position.

Circular Linked List ● Assumes the list is not empty.

A LinkedList class

A LinkedList class

A LinkedList class ● Class methods may be classified according to functionality.

A LinkedList class ● Running times  Basic unit time operations that count toward the running: ● Retrieving a reference, as in Node x = p.next ● Updating a reference, as in p.next = x ● Retrieving or updating the size (number of items) of the linked list ● Comparison between a pair of data items stored in the linked list.

A LinkedList class

A LinkedList class

A LinkedList class ● Since the Node class is used as an inner class, the generic type parameter T for the LinkedList class is already known to Node, and must not be repeated in the Node class.  The fields tail makes it clear that this class is implemented as a circular linked list.

A LinkedList class

A LinkedList class ● After the insertion, the old entry is at i + 1.

A LinkedList class

A LinkedList class ● We need to be sure that the tail variable has a legal value before the method terminates.

A LinkedList class

A LinkedList class

A LinkedList class

A LinkedList class

A LinkedList class

A LinkedList class

A LinkedList class ● get is identical in logic to the indexOf method except that it returns the matching item if found.

List Class Implementation ● Review  Started with the List interface, using it to build an ExpenseList class.  To implement the List class use an array or the java.util.ArrayList class would not be a good choice.  The LinkedList class, on the other hand would be ideal.  The ExpenseList class implementation uses the List class as a component, the List class is built using the LinkedList class as a compenent.

List Class Implementation

List Class Implementation

Summary ● The unordered list is a linear collection of entries whose relative positions with respect to each other is irrelevant. ● The unordered list is characterized by four main operations:  Append  Remove  Enumerate  Search

Summary ● An entry is searched for in a unordered list using sequential search. ● In sequential search, a target is compared against every entry in the list in sequence, one after the other. ● The average number of comparisons for successful search in a data structure is the number of comparisons required.

Summary ● The average number of comparisons for successful search in a data structure is given by the formula: ● In the absence of specific information about probabilities, each element is assumed to e searched with the same probability as any other element, P i =1/n.

Summary ● The lowest number of comparisons on the average for successful search in a list is obtained if the entries of the list are arranged in decreasing order of search probabilities. ● The notion of the equality of objects is specific to a client, and is implemented in a client class by overriding the default implementation. ● The key part of an object is that which is used as the basis of comparing against another object for equality.

Summary ● A linked list is a linear structure consisting of nodes. ● The linked list's main advantage over an array is that space for nodes is allocated only when needed. ● Inserting a node into or deleting a node from a linked list follows a strict sequence. ● A circular linked list is one in which the last node refers back to the first.

Summary ● An unordered list class may be implemented using a linked list object as component. ● Code reuse by composition is also demonstrated in the implementation of the ExpenseList class, which uses an unordered list component.