Download CH4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 4
Unordered List
1
Learning Objectives
●
●
●
●
Describe the properties of an unordered list.
Study sequential search and analyze its worstcase 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.
2
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.
3
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
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.
5
Unordered List Properties
6
Unordered List Properties
7
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.
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

●
Worst case

●
1
n
Unsuccessful search?

n
12
A List Class
13
A List Class
14
A List Class
●
Example that enumerates:
NoSuchElementException thrown
back.
15
Expense Class Interface
Use Expense objects to represents the
items.
16
Expense Class
17
Code for
Keeping track of daily
expense problem
18
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);
19
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");
20
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);
21
Remove item from the list
Homework
22
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
break;
case 3:// Search for an item code
break;
case 4:// Find minimum price code
break;
case 5:
run=false;
break; }} }
23
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
24
4.5 Linked List
25
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.
26
4.5 Linked List
27
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.
28
4.5.1 Node
29
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
30
4.5.2 Insertion
●
Adding to the
beginning of the list.
31
4.5.2 Insertion
●
Adding in between
two nodes.
32
4.5.2 Insertion
●
Adding to the end of the list
33
4.5.3 Deletion
●
Deleting the last node, or in-between node.
●
Deleting the first node
●
L = L.next
34
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.
35
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.
36
4.5.4 Access
37
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.
38
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.
39
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.
40
Doubly Linked List
●
6-41
A linked list in which each node is linked to both
its successor and its predecessor
6-42
Inserting into a Doubly Linked List
6-43
Deleting from a Doubly Linked List
6-44