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

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

9 Unordered List Properties

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

11 Sequential Search

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

13 A List Class

14 A List Class

15 A List Class Example that enumerates:
NoSuchElementException thrown back.

16 Expense Class Interface
Use Expense objects to represents the items.

17 Expense Class

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

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

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

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

22 Remove item from the list
Homework

23 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; }} }

24 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

25 4.5 Linked List

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

27 4.5 Linked List

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

29 4.5.1 Node

30 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

31 4.5.2 Insertion Adding to the beginning of the list.

32 4.5.2 Insertion Adding in between two nodes.

33 4.5.2 Insertion Adding to the end of the list

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

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

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

37 4.5.4 Access

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

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

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

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

42

43 Inserting into a Doubly Linked List

44 Deleting from a Doubly Linked List


Download ppt "Chapter 4 Unordered List."

Similar presentations


Ads by Google