Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
2. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
2 / 79
3. Images/cinvestav-
Iterator Method
Scenario
We often want to access every item in a data structure or collection in
turn...
Example with the array representation
1 int listSize = SomeList.size();
2 for (int i = 0; i < listSize; i++)
3 System.out.println(SomeList.get(i));
3 / 79
4. Images/cinvestav-
Iterator Method
Scenario
We often want to access every item in a data structure or collection in
turn...
Example with the array representation
1 int listSize = SomeList.size();
2 for (int i = 0; i < listSize; i++)
3 System.out.println(SomeList.get(i));
3 / 79
5. Images/cinvestav-
Motivation
Question
What if we have a get that is really complex?
For example the get in a Chain List
We will see that it has complexity O(n)!!!
Previous Code
What a waste of time!!! O n2 !!!!
4 / 79
6. Images/cinvestav-
Motivation
Question
What if we have a get that is really complex?
For example the get in a Chain List
We will see that it has complexity O(n)!!!
Previous Code
What a waste of time!!! O n2 !!!!
4 / 79
7. Images/cinvestav-
Motivation
Question
What if we have a get that is really complex?
For example the get in a Chain List
We will see that it has complexity O(n)!!!
Previous Code
What a waste of time!!! O n2 !!!!
4 / 79
8. Images/cinvestav-
Then
Something Notable
Iteration is such a common operation that we could include it as part of
the ADT list.
However
We do not want to add another operation to the ADT each time we think
of another way to use an iteration.
5 / 79
9. Images/cinvestav-
Then
Something Notable
Iteration is such a common operation that we could include it as part of
the ADT list.
However
We do not want to add another operation to the ADT each time we think
of another way to use an iteration.
5 / 79
10. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
6 / 79
11. Images/cinvestav-
In Java at java.util we have the interface Iterator
Interface
package java . u t i l ;
p u b l i c i n t e r f a c e I t e r a t o r <Item> {
p u b l i c boolean hasNext ( ) ;
p u b l i c Item next ( ) ;
// Optional method
p u b l i c void remove ( ) ;
} // end I t e r a t o r
7 / 79
12. Images/cinvestav-
Explanation
public boolean hasNext()
It detects whether this iterator has completed its traversal and gone
beyond the last entry in the collection of data.
It returns true if the iterator has another entry to return.
public T next()
It retrieves the next entry in the collection and advances this iterator
by one position.
It returns a reference to the next entry in the iteration, if one exists
It throws NoSuchElementException if the iterator had reached the
end already, that is, if hasNext() is false.
8 / 79
13. Images/cinvestav-
Explanation
public boolean hasNext()
It detects whether this iterator has completed its traversal and gone
beyond the last entry in the collection of data.
It returns true if the iterator has another entry to return.
public T next()
It retrieves the next entry in the collection and advances this iterator
by one position.
It returns a reference to the next entry in the iteration, if one exists
It throws NoSuchElementException if the iterator had reached the
end already, that is, if hasNext() is false.
8 / 79
14. Images/cinvestav-
Explanation
public boolean hasNext()
It detects whether this iterator has completed its traversal and gone
beyond the last entry in the collection of data.
It returns true if the iterator has another entry to return.
public T next()
It retrieves the next entry in the collection and advances this iterator
by one position.
It returns a reference to the next entry in the iteration, if one exists
It throws NoSuchElementException if the iterator had reached the
end already, that is, if hasNext() is false.
8 / 79
15. Images/cinvestav-
Explanation
public boolean hasNext()
It detects whether this iterator has completed its traversal and gone
beyond the last entry in the collection of data.
It returns true if the iterator has another entry to return.
public T next()
It retrieves the next entry in the collection and advances this iterator
by one position.
It returns a reference to the next entry in the iteration, if one exists
It throws NoSuchElementException if the iterator had reached the
end already, that is, if hasNext() is false.
8 / 79
16. Images/cinvestav-
Explanation
public boolean hasNext()
It detects whether this iterator has completed its traversal and gone
beyond the last entry in the collection of data.
It returns true if the iterator has another entry to return.
public T next()
It retrieves the next entry in the collection and advances this iterator
by one position.
It returns a reference to the next entry in the iteration, if one exists
It throws NoSuchElementException if the iterator had reached the
end already, that is, if hasNext() is false.
8 / 79
17. Images/cinvestav-
Explanation
public void remove()
It removes from the collection of data the last entry that next()
returned.
Precondition: next() has been called, and remove() has not been
called since then.
It throws IllegalStateException if next() has not been
called, or if remove() was called already after the last
call to next().
9 / 79
18. Images/cinvestav-
Explanation
public void remove()
It removes from the collection of data the last entry that next()
returned.
Precondition: next() has been called, and remove() has not been
called since then.
It throws IllegalStateException if next() has not been
called, or if remove() was called already after the last
call to next().
9 / 79
19. Images/cinvestav-
Explanation
public void remove()
It removes from the collection of data the last entry that next()
returned.
Precondition: next() has been called, and remove() has not been
called since then.
It throws IllegalStateException if next() has not been
called, or if remove() was called already after the last
call to next().
9 / 79
20. Images/cinvestav-
Eļ¬ect in a list
Before next()
Joes Piotor Iterator cursor Jen Jess Marius
After next()
Joes Piotor Jen Iterator cursor Jess Marius
Eļ¬ect of remove()
Joes Piotor Iterator cursor Jess Marius
10 / 79
21. Images/cinvestav-
Eļ¬ect in a list
Before next()
Joes Piotor Iterator cursor Jen Jess Marius
After next()
Joes Piotor Jen Iterator cursor Jess Marius
Eļ¬ect of remove()
Joes Piotor Iterator cursor Jess Marius
10 / 79
22. Images/cinvestav-
Eļ¬ect in a list
Before next()
Joes Piotor Iterator cursor Jen Jess Marius
After next()
Joes Piotor Jen Iterator cursor Jess Marius
Eļ¬ect of remove()
Joes Piotor Iterator cursor Jess Marius
10 / 79
23. Images/cinvestav-
NO FREE LUNCH!!!
Remark
Some details of using an iterator depend on the approach used to
implement the iterator methods.
We can do the following
A possible, but not optimal, way to provide an ADT with traversal
operations is to deļ¬ne them as ADT operations.
For example, if ListInterface extends Iterator, a list object would
have iterator methods as well as list methods.
PROBLEM!!! What if you want to have two or more iterators over the
object list making diļ¬erent things at the same time!!!
11 / 79
24. Images/cinvestav-
NO FREE LUNCH!!!
Remark
Some details of using an iterator depend on the approach used to
implement the iterator methods.
We can do the following
A possible, but not optimal, way to provide an ADT with traversal
operations is to deļ¬ne them as ADT operations.
For example, if ListInterface extends Iterator, a list object would
have iterator methods as well as list methods.
PROBLEM!!! What if you want to have two or more iterators over the
object list making diļ¬erent things at the same time!!!
11 / 79
25. Images/cinvestav-
NO FREE LUNCH!!!
Remark
Some details of using an iterator depend on the approach used to
implement the iterator methods.
We can do the following
A possible, but not optimal, way to provide an ADT with traversal
operations is to deļ¬ne them as ADT operations.
For example, if ListInterface extends Iterator, a list object would
have iterator methods as well as list methods.
PROBLEM!!! What if you want to have two or more iterators over the
object list making diļ¬erent things at the same time!!!
11 / 79
26. Images/cinvestav-
NO FREE LUNCH!!!
Remark
Some details of using an iterator depend on the approach used to
implement the iterator methods.
We can do the following
A possible, but not optimal, way to provide an ADT with traversal
operations is to deļ¬ne them as ADT operations.
For example, if ListInterface extends Iterator, a list object would
have iterator methods as well as list methods.
PROBLEM!!! What if you want to have two or more iterators over the
object list making diļ¬erent things at the same time!!!
11 / 79
27. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
12 / 79
28. Images/cinvestav-
Solution
We can have
Separate class iterator
Inner Class Iterator
For separate class iterator, we have stuļ¬ like this
Iterator<String> nameIterator = new
SeparateIterator<String>(nameList);
13 / 79
29. Images/cinvestav-
Solution
We can have
Separate class iterator
Inner Class Iterator
For separate class iterator, we have stuļ¬ like this
Iterator<String> nameIterator = new
SeparateIterator<String>(nameList);
13 / 79
30. Images/cinvestav-
Solution
We can have
Separate class iterator
Inner Class Iterator
For separate class iterator, we have stuļ¬ like this
Iterator<String> nameIterator = new
SeparateIterator<String>(nameList);
13 / 79
33. Images/cinvestav-
Multiple Iterators
Thus
External Iterators provides a solution to multiple iterators
However
Object Iterator from
separate class
iterators
Object from class list
list
Lou
Peg
Meg
ME
Iterator
Cursor
Next
Position2
Huge Problem
We can only access the list through the public methods:
What if they are not enough?
15 / 79
34. Images/cinvestav-
Separate Iterator Code
Code
import java . u t i l . I t e r a t o r ;
import java . u t i l . NoSuchElementException ;
p u b l i c c l a s s S e p a r a t e I t e r a t o r <Item>
implements I t e r a t o r <Item> {
p r i v a t e L i n e a r L i s t <Item> l i s t ;
// p o s i t i o n of entry l a s t r et urn ed by next ()
p r i v a t e i n t n e x t P o s i t i o n ;
// needed by remove
p r i v a t e boolean wasNextCalled ;
p u b l i c S e p a r a t e I t e r a t o r ( L i s t L i s t <Item> a L i s t ) {
// A l l that i t i m p l i e s
} // end c o n s t r u c t o r
< Implementations of the methods hasNext , . . .
next , and remove go here > . . .
} // end S e p a r a t e I t e r a t o r
16 / 79
35. Images/cinvestav-
We need a better solution!!!
Declare a inner class
So you can have direct access to the protected or private elements in the
object!!!
Actually you have something like this
17 / 79
36. Images/cinvestav-
We need a better solution!!!
Declare a inner class
So you can have direct access to the protected or private elements in the
object!!!
Actually you have something like this
Object Iterator from
Inner class
iterators
Object from class
array list
Lou Peg Meg ME
nextPosition2
17 / 79
37. Images/cinvestav-
Inner Class Code
Code
import j a v a . u t i l . I t e r a t o r ;
import j a v a . u t i l . NoSuchElementException ;
p u b l i c c l a s s A r r a y L i s t W i t h I t e r a t o r <T>
implements L i s t W i t h I t e r a t o r I n t e r f a c e <T> {
// ALL the code f o r a r r a y l i n e a r l i s t
p r i v a t e c l a s s I t e r a t o r F o r A r r a y L i s t
implements I t e r a t o r <T> {
p r i v a t e i n t nextIndex ;
p r i v a t e boolean wasNextCalled ;
p r i v a t e I t e r a t o r F o r A r r a y L i s t () {
nextIndex = 0;
wasNextCalled = f a l s e ;
} // end d e f a u l t c o n s t r u c t o r
< Implementations of the methods i n the
i n t e r f a c e I t e r a t o r go here > . . . }
// end I t e r a t o r F o r A r r a y L i s t
} // end A r r a y L i s t W i t h I t e r a t o r
18 / 79
38. Images/cinvestav-
So, we have...
Advantages
It is a way of logically grouping classes that are only used in one place.
It increases encapsulation
It can lead to more readable and maintainable code.
Example
ArrayListWithIterator<Integers> SOME = new
ArrayListWithIterator<Integers>();
ArrayListWithIterator.IteratorForArrayList ļ¬ = SOME. new
IteratorForArrayList();
19 / 79
39. Images/cinvestav-
So, we have...
Advantages
It is a way of logically grouping classes that are only used in one place.
It increases encapsulation
It can lead to more readable and maintainable code.
Example
ArrayListWithIterator<Integers> SOME = new
ArrayListWithIterator<Integers>();
ArrayListWithIterator.IteratorForArrayList ļ¬ = SOME. new
IteratorForArrayList();
19 / 79
40. Images/cinvestav-
So, we have...
Advantages
It is a way of logically grouping classes that are only used in one place.
It increases encapsulation
It can lead to more readable and maintainable code.
Example
ArrayListWithIterator<Integers> SOME = new
ArrayListWithIterator<Integers>();
ArrayListWithIterator.IteratorForArrayList ļ¬ = SOME. new
IteratorForArrayList();
19 / 79
41. Images/cinvestav-
So, we have...
Advantages
It is a way of logically grouping classes that are only used in one place.
It increases encapsulation
It can lead to more readable and maintainable code.
Example
ArrayListWithIterator<Integers> SOME = new
ArrayListWithIterator<Integers>();
ArrayListWithIterator.IteratorForArrayList ļ¬ = SOME. new
IteratorForArrayList();
19 / 79
42. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
20 / 79
45. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
22 / 79
46. Images/cinvestav-
Memory Layout
We have the following
C A E D B
ļ¬rstNode
First
Last pointer (or link) from E is null
Thus
You can use a variable ļ¬rstNode to get to the ļ¬rst element in the Linked
List.
23 / 79
47. Images/cinvestav-
Memory Layout
We have the following
C A E D B
ļ¬rstNode
First
Last pointer (or link) from E is null
Thus
You can use a variable ļ¬rstNode to get to the ļ¬rst element in the Linked
List.
23 / 79
48. Images/cinvestav-
Memory Layout
We have the following
C A E D B
ļ¬rstNode
First
Last pointer (or link) from E is null
Thus
You can use a variable ļ¬rstNode to get to the ļ¬rst element in the Linked
List.
23 / 79
49. Images/cinvestav-
Normal Way To Draw A Linked List
Example
CA EDB
ļ¬rstNode
Link or pointer ļ¬eld of node
Data ļ¬eld of node
NULL
24 / 79
50. Images/cinvestav-
Actually, we know this as a Chain
First
A chain is a linked list in which each node represents one element.
Second
There is a link or pointer from one element to the next.
Third
The last node has a null pointer.
25 / 79
51. Images/cinvestav-
Actually, we know this as a Chain
First
A chain is a linked list in which each node represents one element.
Second
There is a link or pointer from one element to the next.
Third
The last node has a null pointer.
25 / 79
52. Images/cinvestav-
Actually, we know this as a Chain
First
A chain is a linked list in which each node represents one element.
Second
There is a link or pointer from one element to the next.
Third
The last node has a null pointer.
25 / 79
53. Images/cinvestav-
Code for ChainNode
Code
package L i n e a r L i s t ;
// Using Generic in Java
c l a s s ChainNode<Item>{
// package data members
protected Item element ;
protected ChainNode next ;
// c o n s t r u c t o r s and methods come here
}
26 / 79
54. Images/cinvestav-
The Constructors for ChainNode
A classic one
package L i n e a r L i s t ;
// Using Generic in Java
p u b l i c ChainNode (){
// Set the next node
t h i s . element = n u l l ;
t h i s . next = n u l l ;
}
p u b l i c ChainNode ( Item elem ){
// Set element
t h i s . element = elem ;
t h i s . next = n u l l ;
}
27 / 79
55. Images/cinvestav-
What about the Class Chain?
Code
/āā l i n k e d implementation of L i n e a r L i s t ā/
package i n f r a s t r u c t u r e s ;
import java . u t i l . ā ; // has I t e r a t o r
p u b l i c c l a s s Chain<Item> implements L i n e a r L i s t <Item>
{
// data members
protected ChainNode<Item> f i r s t N o d e ;
protected i n t s i z e ;
// methods of Chain come here
}
28 / 79
56. Images/cinvestav-
In addition!!! Constructors!!!
Code
p u b l i c Chain<Item> ( i n t i n i t i a l C a p a c i t y )
{
// the d e f a u l t i n i t i a l v a l u e s of f i r s t N o d e and s i z e
// are n u l l and 0 , r e s p e c t i v e l y
}
A simple example
public Chain<Item> ()
{
this.ļ¬rstNode = new ChainNode<Item>();
this.size = 0;
}
29 / 79
57. Images/cinvestav-
In addition!!! Constructors!!!
Code
p u b l i c Chain<Item> ( i n t i n i t i a l C a p a c i t y )
{
// the d e f a u l t i n i t i a l v a l u e s of f i r s t N o d e and s i z e
// are n u l l and 0 , r e s p e c t i v e l y
}
A simple example
public Chain<Item> ()
{
this.ļ¬rstNode = new ChainNode<Item>();
this.size = 0;
}
29 / 79
58. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
30 / 79
60. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
32 / 79
62. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
34 / 79
63. Images/cinvestav-
Operations: Get
Example
CA EDB
ļ¬rstNode
Link or pointer ļ¬eld of node
Data ļ¬eld of node
NULL
What to do to implement Get
1 Check Index
2 Move to the correct position
For example: TempNode = ļ¬rstNode.next.next.next.
You actually use a loop.
3 return TempNode.element
35 / 79
64. Images/cinvestav-
Operations: Get
Example
CA EDB
ļ¬rstNode
Link or pointer ļ¬eld of node
Data ļ¬eld of node
NULL
What to do to implement Get
1 Check Index
2 Move to the correct position
For example: TempNode = ļ¬rstNode.next.next.next.
You actually use a loop.
3 return TempNode.element
35 / 79
69. Images/cinvestav-
Move to the correct place
Index == 2
CA EDB
ļ¬rstNode NULL
TempNode=TempNode.next
index==2
39 / 79
70. Images/cinvestav-
Move to the correct place
Index == 2
CA EDB
ļ¬rstNode NULL
TempNode=TempNode.next
index==2
40 / 79
71. Images/cinvestav-
Move to the correct place
Index == 2
CA EDB
ļ¬rstNode NULL
return TempNode.element
index==2
41 / 79
72. Images/cinvestav-
Code
Here is the method
p u b l i c <Item> get ( i n t index ){
ChainNode<Item> TempNode ;
//Check always
i f ( t h i s . s i z e == 0) r e t u r n n u l l ;
i f ( index <0 | | index >t h i s . s i z e ā1){
System . out . p r i n t l n ( " Index ā£ out ā£ of ā£bound " ) ;
System . e x i t ( 0 ) ;
}
//Move to the c o r r e c t p o s i t i o n
TempNode = t h i s . f i r s t N o d e ;
f o r ( i n t i =0 ; i <index ; i ++){
TempNode = TempNode . next ;
}
r e t u r n TempNode . element ;
}
42 / 79
73. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
43 / 79
101. Images/cinvestav-
Outline
1 Iterators
Interface
Separate and Inner Class Iterator
2 Introduction
Linked Representation
Memory Layout
3 Operations
IsEmpty()
size()
Get(Index)
Remove
Add
4 Can we simplify the code?
5 Other data structures based in the Linked List
57 / 79
109. Images/cinvestav-
Add at an empty list
Steps
1 Create a node to store the element, NewNode
2 Then assign ļ¬rstNode = NewNode
61 / 79
110. Images/cinvestav-
Add at an empty list
Steps
1 Create a node to store the element, NewNode
2 Then assign ļ¬rstNode = NewNode
61 / 79
111. Images/cinvestav-
Add at an empty list
Steps
1 Create a node to store the element, NewNode
2 Then assign ļ¬rstNode = NewNode
ļ¬rstNode
K
NULL
61 / 79
112. Images/cinvestav-
Add At the Beginning
Steps
1 Create a node to store the element, NewNode
2 Do NewNode.next = ļ¬rstNode
3 Then reassign ļ¬rstNode = NewNode
4 Then, size = size +1
62 / 79
113. Images/cinvestav-
Add At the Beginning
Steps
1 Create a node to store the element, NewNode
2 Do NewNode.next = ļ¬rstNode
3 Then reassign ļ¬rstNode = NewNode
4 Then, size = size +1
62 / 79
114. Images/cinvestav-
Add At the Beginning
Steps
1 Create a node to store the element, NewNode
2 Do NewNode.next = ļ¬rstNode
3 Then reassign ļ¬rstNode = NewNode
4 Then, size = size +1
62 / 79
115. Images/cinvestav-
Add At the Beginning
Steps
1 Create a node to store the element, NewNode
2 Do NewNode.next = ļ¬rstNode
3 Then reassign ļ¬rstNode = NewNode
4 Then, size = size +1
62 / 79
116. Images/cinvestav-
Add At the Beginning
Steps
1 Create a node to store the element, NewNode
2 Do NewNode.next = ļ¬rstNode
3 Then reassign ļ¬rstNode = NewNode
4 Then, size = size +1
CA EDB
ļ¬rstNode
NULL
K
62 / 79
118. Images/cinvestav-
What about the other methods?
Linear List
AbstractDataType LinearList
{
instances
ordered ļ¬nite collections of zero or more elements
operations
isEmpty(): return true iļ¬ the list is empty, false otherwise
size(): return the list size (i.e., number of elements in the list)
get(index): return the element with the āindexā index
indexOf(x): return the index of the ļ¬rst occurrence of x in the list, return -1
if x is not in the list
remove(index): remove and return the indexth element, elements with higher
index have their index reduced by 1
add(theIndex, x): insert x as the index of th element, elements with
theIndex ā„ index have their index increased by 1
output(): output the list elements from left to right
}
64 / 79
119. Images/cinvestav-
Complexity of Linked List
We have
Dynamic Array Linked List
Amortized Analysis
Indexing O (1) O (n)
Search O (n) O (n)
Add/Remove at the beginning O (n) O (1)
Add/Remove at the middle O (n) Search Time
+O (1)
Space Complexity O (n) O (n)
65 / 79
120. Images/cinvestav-
Average Performance with Each Implementation on a Slow
Machine!!!
40,000 Operations each type on a 350Mhz PC
Operation FastArrayLinearList Chain
average get 5.6 ms 157 sec
average adds 5.8 sec 115 sec
average removes 5.8 sec 157 sec
66 / 79
122. Images/cinvestav-
Can we simplify the code?
How?
We would like to simplify the code.... How?
What about
CA EDB
headerNode NULL
67 / 79
123. Images/cinvestav-
Can we simplify the code?
Then, the empty list looks like
headerNode
Thus
This simplify a lot the code because everything is a middle node
68 / 79
124. Images/cinvestav-
Can we simplify the code?
Then, the empty list looks like
headerNode
Thus
This simplify a lot the code because everything is a middle node
68 / 79
126. Images/cinvestav-
Here, we need to have the following changes
We need to add the following to the class
/āā l i n k e d implementation of L i n e a r L i s t ā/
package i n f r a s t r u c t u r e s ;
import java . u t i l . ā ; // has I t e r a t o r
p u b l i c c l a s s Chain<Item> implements L i n e a r L i s t <Item>
{
// data members
protected ChainNode<Item> f i r s t N o d e ;
protected ChainNode<Item> lastNode ;
protected i n t s i z e ;
// methods of Chain come here
}
70 / 79
127. Images/cinvestav-
Example of add(index) at the end
Process
Check if (index==size-1)
Next
Make the lastNode.next = TempNode
Then
TempNode.next = ļ¬rstNode
71 / 79
128. Images/cinvestav-
Example of add(index) at the end
Process
Check if (index==size-1)
Next
Make the lastNode.next = TempNode
Then
TempNode.next = ļ¬rstNode
71 / 79
129. Images/cinvestav-
Example of add(index) at the end
Process
Check if (index==size-1)
Next
Make the lastNode.next = TempNode
Then
TempNode.next = ļ¬rstNode
71 / 79
134. Images/cinvestav-
We have other representations for the lists
Doubly Linked List
You have two chain list going through the nodes.
It has a ļ¬rstNode
It has a lastNode
Doubly Linked Circular List
You have two chain list going through the nodes.
It has a ļ¬rstNode
76 / 79
135. Images/cinvestav-
We have other representations for the lists
Doubly Linked List
You have two chain list going through the nodes.
It has a ļ¬rstNode
It has a lastNode
Doubly Linked Circular List
You have two chain list going through the nodes.
It has a ļ¬rstNode
76 / 79
138. Images/cinvestav-
Where does Java has all these things?
Package
java.util.LinkedList
There you have
It has the Linked implementation of a linear list.
It has the doubly linked circular list with header node.
It Has all methods of LinearList plus many more.
79 / 79
139. Images/cinvestav-
Where does Java has all these things?
Package
java.util.LinkedList
There you have
It has the Linked implementation of a linear list.
It has the doubly linked circular list with header node.
It Has all methods of LinearList plus many more.
79 / 79
140. Images/cinvestav-
Where does Java has all these things?
Package
java.util.LinkedList
There you have
It has the Linked implementation of a linear list.
It has the doubly linked circular list with header node.
It Has all methods of LinearList plus many more.
79 / 79