Chapter 4 Unordered List.

Slides:



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

Linked Lists.
Lists CS 3358.
DATA STRUCTURES USING C++ Chapter 5
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
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.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Data Structures Using Java1 Chapter 4 Linked Lists.
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
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.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
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.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
LINKED LISTS.
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
Unit – I Lists.
C++ Programming:. Program Design Including
Chapter 4 Linked Structures.
Chapter 5 Ordered List.
Pointers and Linked Lists
Data Structures and Design in Java © Rick Mercer
Pointers and Linked Lists
Multiway Search Trees Data may not fit into main memory
Lectures linked lists Chapter 6 of textbook
Chapter 4 Unordered List.
Design & Analysis of Algorithm Priority Queue
Big-O notation Linked lists
Chapter 5 Ordered List.
Lists CS 3358.
Data Structures Interview / VIVA Questions and Answers
Lecture 22 Binary Search Trees Chapter 10 of textbook
Data Structures: A Pseudocode Approach with C
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Chapter Trees and B-Trees
Chapter Trees and B-Trees
Binary Tree and General Tree
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:
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Linked Lists.
Chapter 4 Unordered List.
Topic 11 Linked Lists -Joel Spolsky
Chapter 5 Ordered List.
List Data Structure.
Multi-Way Search Trees
CIS16 Application Development and Programming using Visual Basic.net
Linked Lists.
Chapter 5 Ordered List.
Linked List.
Linked Lists.
Lists.
Data Structures & Algorithms
B-Trees.
Topic 11 Linked Lists -Joel Spolsky
Problem Understanding
Presentation transcript:

Chapter 4 Unordered List

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.

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.

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.

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.

4.1 Unordered List Properties

4.1 Unordered List Properties

4.1 Unordered List Properties 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.

4.1 Unordered List Properties

4.2 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.

4.2 Sequential Search

4.2 Sequential Search Best case 1 Worst case n Unsuccessful search?

UnorderedList implementation using array List: array of element

Insert method insert(“Rami”)

Delete method 1- define loc =0 2- search for item in array list using loop. If item in the list 3- assign item index to loc. 4- store last element of array “list” in index = loc 5- numItems -- Else Print element not in the list

Delete method

delete(“Judy”)

UnorderedList class

4.3 A List Class NoSuchElementException thrown back.

4.3 A List Class

4.3 A List Class

4.3 A List Class Example that enumerates:

4.3 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. Use a cursor to enumerate a list, so that each of the enumeration methods first and next may be implemented in O(1) time.

4.4 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.

4.4.1 Expense Class Interface

4.4.1 Expense Class Interface

4.4.2 Expense Class

4.4.2 Expense Class

4.4.3 ExpenseList Class Interface

4.4.3 ExpenseList Class Interface

4.4.4 ExpenseList Class Implementation Wrong Wrong

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

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

4.4.5 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.

4.4.5 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.

4.4.5 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.

4.5 Linked List

4.5 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.

4.5 Linked List

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

4.5.1 Node

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

4.5.2 Insertion Adding to the beginning of the list.

4.5.2 Insertion Adding in between two nodes.

4.5.2 Insertion Adding to the end of the list

4.5.3 Deletion Deleting the last node, or in-between node. Deleting the first node L = L.next

4.5.3 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.

4.5.4 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.

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

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

UnorderedList implementation using Linked List Insert list = null  empty list Insert(“john”)

Insert insert(“Becca”)

Delete delete(“Lila”)

delete(“Kate”)

delete(“John”)

delete last node in the list delete(“Becca”) list = null

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

4.5.5 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.

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

4.5.5 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.

4.5.5 Circular Linked List Assumes the list is not empty.

Doubly Linked List A linked list in which each node is linked to both its successor and its predecessor

Inserting into a Doubly Linked List

Deleting from a Doubly Linked List