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.
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)
John Hurley Cal State LA
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
Chapter 7 – Arrays.
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.
F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
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.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Java linked list.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
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.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Chapter 4 Unordered List.
Chapter 12 Hash Table.
CSC111 Quick Revision.
EECE 310: Software Engineering
Lectures linked lists Chapter 6 of textbook
Chapter 4 Unordered List.
Chapter 5 Ordered List.
Chapter 6 Queue.
John Hurley Cal State LA
Chapter 5: Control Structures II
Mid Term Review Advanced Programming Ananda Gunawardena
Repetition-Counter control Loop
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Binary Search Trees.
Something about Java Introduction to Problem Solving and Programming 1.
Topic 11 Linked Lists -Joel Spolsky
Conditional Loops.
Chapter 6 More Conditionals and Loops
Chapter 5 Ordered List.
Data Structures ADT List
Outline Boolean Expressions The if Statement Comparing Data
Chapter 12 Hash Table.
Building Java Programs
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 6 Queue.
Chapter 6 Queue.
Building Java Programs
Chapter 4 Unordered List.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Building Java Programs
Chapter 2 : List ADT Part I – Sequential List
Building Java Programs
Outline Software Development Activities
Topic 11 Linked Lists -Joel Spolsky
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 Answer the following questions: What is the maximum (or minimum) expense? What is the average expense? What is the total amount spent? 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?

ArrayList Class

ArrayList Class

ArrayList Class

NoSuchElementException thrown back.

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

Adding new price by user import java.util.Scanner; import java.util. ArrayList; public static void main(String[] args) { ArrayList<Float> expenses = new ArrayList<Float>(); Scanner s = new Scanner(System.in); System.out.println("Enter price"); num = s.nextFloat(); expenses.add(num);

Deleting price entered by user System.out.println("Enter price"); num = s.nextFloat(); expenses.remove(num);

Search for an price entered by user System.out.println("Enter price"); num = s.nextFloat(); if(expenses.contains(num)) System.out.println("price found"); else System.out.println("price not found");

Finding minimum price Float min = expenses.get(0); for(int i=1;i<expenses.size();i++) { if(min>expenses.get(i)) min=expenses.get(i); } System.out.println("minimum price is: "+min);

Finding the total Float sum=0f; for(int i=0;i<expenses.size();i++) { sum+=expenses.get(i); } System.out.println("Total price is: "+sum);

Print All Prices System.out.println("All prices are: "+expenses);

The Whole program package expensbyaeeaylist; import java.util.*; public class ExpensByAeeayList { public static void main(String[] args) { ArrayList<Float> expenses = new ArrayList<Float>(); Scanner s = new Scanner(System.in); Float num; Float min; Float sum=0f; int choice; boolean run = true; while(run) { System.out.println("***********************************"); System.out.println("To insert new price press 1."); System.out.println("To delete a price press 2."); System.out.println("To search for a price press 3."); System.out.println("To find minimum price press 4."); System.out.println("To find total price press 5."); System.out.println("To print all prices press 6."); System.out.println("To exit press 7."); } choice = s.nextInt(); The Whole program

switch(choice) { case 1: System.out.println("Enter price"); num = s.nextFloat(); expenses.add(num); break; case 2: expenses.remove(num); case 3: if(expenses.contains(num)) System.out.println("price found"); else System.out.println("price not found"); case 4: min = expenses.get(0); for(int i=1;i<expenses.size();i++) if(min>expenses.get(i)) min=expenses.get(i); } System.out.println("minimum price is: "+min);

case 5: for(int i=0;i<expenses.size();i++) { sum+=expenses.get(i); } System.out.println("Total price is: "+sum); break; case 6: System.out.println("All prices are: "+expenses); case 7: run = false; default: System.out.println("not valid choise, press number from 1 to 7");

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