public void print() { - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

public void print() {

Description:

public void print() {Iterator iter = iterator(); while(iter.hasNext()) System.out.print(iter.next()+ – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 22
Provided by: Chen297
Category:

less

Transcript and Presenter's Notes

Title: public void print() {


1
public void print() Iterator iter
iterator() while(iter.hasNext())
System.out.print(iter.next()" ")
System.out.println(" ") public class
Linked_list_iterator implements Iterator
private Linked_list running public
Linked_list_iterator() running new
Linked_list(new Link("off left",first_link()))
public boolean hasNext() return (!
running.isEmpty()) public Object next()
Object head running.removeHead()
return head
2
public class List_driver public static void
main(String args) List lst new
Linked_list() StringTokenizer tokens new
StringTokenizer("0 1 2 3 4 5 6 7 8 9 10")
while (tokens.hasMoreTokens())
lst.addAt(0,tokens.nextToken())
((Linked_list) lst).print()
System.out.println(lst.addAt(6,"6"))
((Linked_list) lst).print()
System.out.println(lst.removeAll("6"))
((Linked_list) lst).print()
System.out.println(lst.set(6,"x"))
((Linked_list) lst).print()
System.out.println(lst.contains("10")
" "
lst.contains("uuuu"))
???
10 9 8 7 6 5 4 3 2 1 0 true 10 9 8 7 6 5 6 4 3 2
1 0 true 10 9 8 7 5 4 3 2 1 0 x 10 9 8 7 5 4 x 2
1 0 true false
3
public class SortableList extends Linked_list
public SortableList() super() private
SortableList(Link first) super(first)
private SortableList tail() return new
SortableList(first_link()) public boolean
comparableWith(Object data) if (data
null) return false else if (isEmpty())
return data instanceof Comparable
else return data.getClass().isInsta
nce(head())
4
public boolean addAtHead(Object data)
if (comparableWith(data)) return
super.addAtHead(data) else
return false public boolean
insert(Comparable data) // add data before
the first element which is bigger than it
if (! comparableWith(data)) return false
if (isEmpty()) return addAtHead(data) if
(data.compareTo(head()) lt 0) return
addAtHead(data) return ((SortableList)tail(
)).insert(data)
5
public void sort() if (!isEmpty())
Object head removeHead()
sort() insert((Comparable) head)

6
import java.util.StringTokenizer public class
SortableList_driver public static void
main(String args) SortableList lst new
SortableList() StringTokenizer tokens
new StringTokenizer("0 1 2 3 4 5 6 7 8 9
10") while (tokens.hasMoreTokens())
lst.addAt(0,tokens.nextToken())
((Linked_list) lst).print() lst.sort()
((Linked_list) lst).print()
???
10 9 8 7 6 5 4 3 2 1 0 0 1 10 2 3 4 5 6 7 8 9
7
class SortedList extends SortableList public
SortedList() super() public
SortedList(SortableList list) super()
while ( !list.isEmpty() )
insert((Comparable) list.removeHead())
private SortedList(Link ) super(original.firs
t_link()) private SortedList tail()
return new SortedList(this)
8
public boolean addAtHead(Object data)
Comparable d (Comparable) data if ((!
comparableWith(d)) ((! isEmpty())
d.compareTo(head())gt 0)) return false
return super.addAtHead(data) public
boolean addAt(int index, Object data)
Comparable d (Comparable) data if (index
0) return addAtHead(data) if (isEmpty()
d.compareTo(head())lt 0) return false
return tail().addAt(index-1,data) public
Object setAt(int index, Object data) return
null
9
public class SortedList_driver1 public
static void main(String args)
SortableList list new SortableList()
list.addAtHead(new Integer(4))
list.addAtHead(new Integer(5))
list.addAtHead(new Integer(2))
list.addAtHead(new Integer(3))
list.addAtHead(new Integer(1))
list.addAtHead(new Integer(6))
list.print() SortedList s new
SortedList(list) s.print()
s.insert(new Integer(4)) s.print()
s.insert(new Integer(12)) s.print()

???
6 1 3 2 5 4 1 2 3 4 5 6 1 2 3 4 4 5 6 1 2 3 4 4 5
6 12
10
public class SortedList_driver2 public
static void main(String args) SortedList
l new SortedList() System.out.println(l.a
ddAtHead(new Integer(3)))
System.out.println(l.addAtHead(new Integer(1)))
System.out.println(l.addAtHead(new
Integer(3))) l.print()
System.out.println(l.addAt(3,new Integer(2)))
l.print() System.out.println(l.addAt(2,new
Integer(2))) l.print()
System.out.println(l.addAt(1,new Integer(2)))
l.print()
true true false 1 3 false 1 3 false 1 3 true 1 2
3
11
????? ??? ????? ?????? ?? ???????? ???? ????
mergesort
????????? ??? ????? ???? ?????? ???? ?????? ???,
?? ???? ?????? ?????? ?????? ??? ???? ???
?????. mergesort ?????? ????? ?? ?????? ???
?????? - ?? ?????? ???? ???? ????. ???? ?. ???
?? ?????? ???? ?????? ???? ?????? (? 1). ?. ????
?? ??????? ?????. ?. ??? ?? ??????? ????????
?????? ?????? ???, ????? ????.
12
?????
????? ????? ?? ?????? ??? ??????

5 3 1 2 9 6 8 7 4 ?. ????
?? ?????? ???? ?????? ???? ?????? (? 1).

5 1 9 8 4 3 2 6
7 ?. ???? ?? ??????? ?????.

9 8 5 4 1 7 6 3 2 ?. ??? ??
??????? ???????? ?????? ?????? ???.

9 8 7 6 5 4 3 2 1
????? ????? ???????
13
???? ?? ?? ?? ??? ? ???? ?????? ???? ? n , ????
?????? ???????? ?????, ??? ???? ?? 2 (??????
????? ????? ?? ?? ??? ?? ????). ?????? ??????
(split) ? ?????? (merge) ?????? ??? ???? ?????
??????? ??? ????? ?????? (???? ?????? ???????
????? ?? ????? ??????? ?? ???? ??????). ???
?????? ???? ???? ?????? ?? ????? ????? n ????
???? ?????? ?? ??? ?????? ??????? ????? n/2
. ???? ???? ??? t(n) .
14
??? ?????\?????
????? ????? N
t(n)
????? ????? N/2
????? ????? N/2
2t(n/2)
4t(n/4)
. . . . .
N ?????? ????? 1
. . . ..
????? ????? N/4
????? ????? N/4
????? ????? N/4
????? ????? N/4
4t(n/4)
????? ????? N/2
????? ????? N/2
2t(n/2)
????? ????? N
15
?? ???? (?? ??? ??? ?????) Log2(t(n)) ???? t(n)
????? ??? n ???? ????. ???? ?? ??? ??? ?????
??? ????? ??? nlog(n) . ???? ?? ????? ?????
??????? ???? ?? ???? 2 ?
16
public class Sort public static
SortableList split(SortableList list)
SortableList pair new SortableList(),
new SortableList() int i 0
while ( !list.isEmpty() )
pairi2.addAtHead(list.removeHead()) i
i1 return pair
17
public class Split_driver public static void
main(String args) SortableList list
new SortableList() list.addAtHead(new
Integer(4)) list.addAtHead(new
Integer(5)) list.addAtHead(new
Integer(2)) list.addAtHead(new
Integer(3)) list.addAtHead(new
Integer(1)) list.addAtHead(new
Integer(6)) list.addAtHead(new
Integer(4)) list.print()
SortableList pair Sort.split(list)
pair0.print() pair1.print()
???
4 6 1 3 2 5 4 4 2 1 4 5 3 6
18
public static SortedList merge(SortedList list1,
SortedList list2) SortedList list
Object head if (list1.isEmpty()) return
list2 if (list2.isEmpty()) return list1
Comparable data1 (Comparable) list1.head()
Comparable data2 (Comparable) list2.head()
if (data1.compareTo(data2) lt 0) head
list1.removeHead() else head
list2.removeHead() list merge(list1,list2)
list.addAtHead(head) return list
19
public class Merge_driver public static void
main(String args) SortedList list1
new SortedList() SortedList list2 new
SortedList() list1.insert(new
Integer(4)) list1.insert(new
Integer(5)) list1.insert(new
Integer(2)) list1.insert(new
Integer(3)) list2.insert(new
Integer(1)) list2.insert(new
Integer(6)) list2.insert(new
Integer(4)) list1.print()
list2.print() (Sort.merge(list1,list2)).p
rint()
???
2 3 4 5 1 4 6 1 2 3 4 4 5 6
20
public static SortedList mergesort(SortableList
list) if (list.isEmpty()
(list.first_link().next() null)) return new
SortedList(list) SortableList pair
split(list) return merge(mergesort(pair0),
mergesort(pair1)) // class Sort
21
public class SortedList_driver1 public
static void main(String args)
SortableList list new SortableList()
list.addAtHead(new Integer(4))
list.addAtHead(new Integer(5))
list.addAtHead(new Integer(2))
list.addAtHead(new Integer(3))
list.addAtHead(new Integer(1))
list.addAtHead(new Integer(6))
list.addAtHead(new Integer(4))
list.print() SortedList s
Sort.mergesort(list) s.print()
???
4 6 1 3 2 5 4 1 2 3 4 4 5 6
Write a Comment
User Comments (0)
About PowerShow.com