Chapter 4 Unordered List.

Slides:



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

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Stacks, Queues, and Linked Lists
Data Structures: A Pseudocode Approach with C
DATA STRUCTURES USING C++ Chapter 5
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
M180: Data Structures & Algorithms in Java
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Data Structures: A Pseudocode Approach with C
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
Chapter 7 – Arrays.
Chapter 3: Arrays, Linked Lists, and Recursion
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.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Data Structures Using Java1 Chapter 4 Linked Lists.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
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
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh 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.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Introduction toData structures and Algorithms
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 4 Linked Structures.
Chapter 12 Hash Table.
CSC111 Quick Revision.
Chapter 8 – Arrays and Array Lists
Lectures linked lists Chapter 6 of textbook
Chapter 4 Unordered List.
Big-O notation Linked lists
Chapter 5 Ordered List.
Chapter 6 Queue.
CSC 222: Object-Oriented Programming
John Hurley Cal State LA
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Chapter 5: Control Structures II
Multiway search trees and the (2,4)-tree
Repetition-Counter control Loop
Binary Tree and General Tree
Binary Search Trees.
Chapter 4 Unordered List.
Topic 11 Linked Lists -Joel Spolsky
Chapter 5 Ordered List.
Describing algorithms in pseudo code
Data Structures ADT List
Data Structures ADT List
Chapter 12 Hash Table.
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 6 Queue.
Chapter 5 Ordered List.
Binary Search Trees A special case of a Binary Tree
Chapter 6 Queue.
Data Structures ADT List
Chapter 4 Unordered List.
Introduction to Data Structure
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Topic 11 Linked Lists -Joel Spolsky
Lecture 3 – Data collection List ADT
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.

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.

Unordered List Properties

Unordered List Properties

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.

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?

A List Class

A List Class

A List Class Example that enumerates: NoSuchElementException thrown back.

Expense Class Interface Use Expense objects to represents the items.

Expense Class

Keeping track of daily expense problem Code for Keeping track of daily expense problem

Adding new item by user import java.util.Scanner; public static void main(String[] args) { List<Expense> expenselist = new List<Expense>(); Scanner s = new Scanner(System.in); float itemPrice; String itemName; System.out.println("Enter item name:"); itemName=s.next(); System.out.println("Enter item price:"); itemPrice=s.nextFloat(); Expense e = new Expense(itemPrice, itemName); expenselist.add(e);

Search for an item entered by user System.out.println("Enter item name:"); itemName=s.next(); System.out.println("Enter item price:"); itemPrice=s.nextFloat(); Expense e1 = expenselist.first(); while(e1!=null) { if(e1.item.equals(itemName)&& e1.amount==itemPrice) System.out.println("item is in the list"); break; } else e1=expenselist.next(); if(e1==null) System.out.println("item is not in the list");

Finding minimum price Expense e2 = expenselist.first(); float min = e2.amount; while(e2!=null) { if(e2.amount<min) min=e2.amount; } e2=expenselist.next(); System.out.println(min);

Remove item from the list Homework

The Whole program import java.util.Scanner; public class ExpenseApplication { public static void main(String[] args) { boolean run = true; List<Expense> expenselist = new List<Expense>(); while(run){ System.out.println("To add new item press 1"); System.out.println("To remove item press 2"); System.out.println("To search for item press 3"); System.out.println("To minimum amount press 4"); System.out.println("To exit press 5"); Scanner s = new Scanner(System.in); int choice = s.nextInt(); float itemPrice; String itemName; switch(choice) { case 1:// add new item code break; case 2:// remove item code case 3:// Search for an item code case 4:// Find minimum price code case 5: run=false; break; }} }

Improving our solution 1) Use a class to implement Expense list which has list object and methods that implement the operation needed in Keeping track of daily expense problem. 2) Add new methods to enhance the class

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

4.5.4 Access Deleting the first occurrence of the string “Carrot”. We can't delete nextNode unless we have a reference to the node prior to it.

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.

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