Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Unordered List.

Similar presentations


Presentation on theme: "Chapter 4 Unordered List."— 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 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 Unordered List Properties

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

8 Unordered List Properties

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

10 Sequential Search

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

12 ArrayList Class

13 ArrayList Class

14 ArrayList Class

15 NoSuchElementException thrown back.

16

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

18 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);

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

20 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");

21 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);

22 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);

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

24 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

25 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);

26 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");

27 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

28 4.5 Linked List

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

30 4.5 Linked List

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

32 4.5.1 Node

33 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

34 4.5.2 Insertion Adding to the beginning of the list.

35 4.5.2 Insertion Adding in between two nodes.

36 4.5.2 Insertion Adding to the end of the list

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

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

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

40 4.5.4 Access

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

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

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

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

45

46 Inserting into a Doubly Linked List

47 Deleting from a Doubly Linked List


Download ppt "Chapter 4 Unordered List."

Similar presentations


Ads by Google