Matrix and Linked List ADTs - PowerPoint PPT Presentation

About This Presentation
Title:

Matrix and Linked List ADTs

Description:

Matrix and Linked List ADTs. B.Ramamurthy (Still in Chapter 4) Topics for Discussion ... double get (int i, int j); void put (int i, int j, double d); Matrix transpose ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 15
Provided by: bina1
Learn more at: https://cse.buffalo.edu
Category:
Tags: adts | double | linked | list | matrix

less

Transcript and Presenter's Notes

Title: Matrix and Linked List ADTs


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 doublenumberOfRowsnumberOfColumn
    s
  • //others

6
times method
  • public Matrix times (Matrix mat)
  • DenseMatrix arg (DenseMatrix) mat
  • if (numberOfColumns ! arg.numberOfRows)
  • // throw exception
  • DenseMatrix result new DenseMatrix(numberOfRow
    s, arg.numberOfColumns)

7
times 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

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.
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
List
AbstractSequentialList
List, java.io.Serializable, java.lang.Cloneable
LinkedList
12
LinkedLists 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())
Write a Comment
User Comments (0)
About PowerShow.com