Download presentation
Presentation is loading. Please wait.
1
Matrix and Linked List ADTs
B.Ramamurthy (Still in Chapter 4)
2
Topics for Discussion Matrix interface And an implementation
Singly-linked List Linked List from Java API Summary
3
Matrix 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); }
4
An 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.
5
Data 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 double[numberOfRows][numberOfColumns]; } //others
6
times method public Matrix times (Matrix mat)
{ DenseMatrix arg = (DenseMatrix) mat; if (numberOfColumns != arg.numberOfRows) // throw exception DenseMatrix result = new DenseMatrix(numberOfRows, arg.numberOfColumns);
7
times method (contd.) for (int j = 0; j< numberOfRows; j++)
for (int k= 0; k< arg.numberOfColumns;k++) {double sum = 0; for (int p =0; p< numberOfColumns; p++) sum = sum + array[j][p] *arg[p][k]; result.array[j][k] = sum; } return result; }
8
Linked 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.
9
A node of linked list public class ListNode { ItemType item;
ListNode link; } some times you have explicit representation for a head and or tail. 5/30/2019 B.Ramamurthy
10
Linked 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()
11
JDK1.2 Linked List Object AbstractCollection Collection AbstractList
AbstractSequentialList List, java.io.Serializable, java.lang.Cloneable LinkedList
12
LinkedList’s 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.
13
LinkedList 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.
14
ListIterator 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()); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.