Title: Matrix and Linked List ADTs
1Matrix and Linked List ADTs
- B.Ramamurthy
- (Still in Chapter 4)
2Topics for Discussion
- Matrix interface
- And an implementation
- Singly-linked List
- Linked List from Java API
- Summary
3Matrix Interface
- public interface Matrix
-
- double get (int i, int j)
- void put (int i, int j, double d)
- Matrix transpose()
- Matrix times (Matrix matrix)
- Matrix plus (Matrix matrix)
4An Implementation Dense Matrix
- From an interface definition there can be many
implementations. - For example, dense matrix, sparse matrix,
triangular matrix, may have different semantics
for the methods specified in the interface.
5Data Fields and Constructor
- public class DenseMatrix implements Matrix
- protected int numberOfRows
- protected int numberOfColumns
- protected double array
- public DenseMatrix (int numberOfRows, int
numberOfColumns) -
- this.numberOfRows numberOfRows
- this.numberOfColumns numberOfColumns
- array new doublenumberOfRowsnumberOfColumn
s -
- //others
6times method
- public Matrix times (Matrix mat)
- DenseMatrix arg (DenseMatrix) mat
- if (numberOfColumns ! arg.numberOfRows)
- // throw exception
- DenseMatrix result new DenseMatrix(numberOfRow
s, arg.numberOfColumns)
7times method (contd.)
- for (int j 0 jlt numberOfRows j)
- for (int k 0 klt arg.numberOfColumnsk)
- double sum 0
- for (int p 0 plt numberOfColumns p)
- sum sum arrayjp argpk
- result.arrayjk sum
- return result
8Linked List
- Linked data representation and algorithms that
manipulate them are an important component of
study in CS. - Linked representations are used when it is
difficult to predict the size and shape of the
data structures needed.
9A node of linked list
public class ListNode ItemType item ListNode
link some times you have explicit
representation for a head and or tail.
10Linked List class
LinkedList
// data int length ListNode firstNode //
methods public int size() public bool
insertNode(ItemType it) public bool
insertLast(ItemType it) public bool
deleteLast() public ListNode search(ItemType
it) public bool equals(LinkedList ll) public
String toString()
11JDK1.2 Linked List
Object
AbstractCollection
Collection
AbstractList
List
AbstractSequentialList
List, java.io.Serializable, java.lang.Cloneable
LinkedList
12LinkedLists inheritance
- Most of the direct linked list functionality is
defined in the interface List. - Serializable defines the disk archiving
functionality. - Cloenable defined the cloning ability.
- LinkedList class defines the variables needed for
storing the header and size. - A ListIterator class offers the Iterator facility.
13LinkedList methods
- List interface and how LinkedList implements
them. - Look at /util/lang/jdk1.2/docs/index.html for
more details. - Among the methods a special method called
iterator is important many of the Collection
classes. This is proper way carry out an
operation on on all elements of a collection.
14ListIterator
- Usage
- LinkedList myList new LinkedList()
- // fill it up with data
- ListIterator k myList.listIterator()
- while(k.hasNext())
- // process the elements
- myProcess(k.next())
- System.out.println(k.next())