Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

15
Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Page 1: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Two-Dimensional Arrays

Introduction to Linked Lists

COMP53Sept 12 2007

Page 2: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

• Many games and simulations have a two-dimensional “domain”

• Such domains can be represented by a two-dimensional array or matrix

• 2D arrays use two indices [i][j] to represent [row][column] or [x][y] position

Two Dimensional Arrays

Page 3: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Two Dimensional Arrays

0 1 2 3

0 22 18 70 5

1 33 9 99 21

2 56 69 12 41

3 29 1 47 78

4 12 3 18 6

□ Arrays in Java are 1D□ 2D arrays are defined as an “array of

arrays”□ int[][]M = new int[5][4];□ This creates an array of ints with 5

rows and 4 columns□ What’s the value of M[3][1]?□ What’s M[3].length?

Page 4: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Tic-Tac-Toe

• How can we use a 2D array to represent a TTT board?

0 1 2

0 0 0 0

1 0 0 0

2 0 0 0

X 1

O -1X 1O -1

Page 5: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Nesting Loops Over a 2D Array

double[][] array = new double[3][4];int row, col, k;k = 1;for (row=0; row<array.length; row++)

for (col=0; col<array[0].length; col++)array[row][col] = k++;

Since columns were the inner loop, we walked across before down.

1.0 2.0 3.0 4.05.0 6.0 7.0 8.09.0 10.0 11.0 12.0

Page 6: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Nesting Loops Over a 2D Array

double[][] array = new double[3][4];int row, col, k;k = 1;for (col=0; col<array[0].length; col++)

for (row=0; row<array.length; row++)array[row][col] = k++;

Since rows were the inner loop, we walked down before across.

1.0 4.0 7.0 10.02.0 5.0 8.0 11.03.0 6.0 9.0 12.0

Page 7: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Printing a 2D Arraypublic static void printArray2D(double[][] _a){

int r, c;for (r=0; r<_a.length; r++){

for (c=0; c<_a[0].length; c++){

System.out.print(_a[r][c]+"\t");

}System.out.println();

}}

columns needs to be inner loop, since an entire row is printed on one line.

Page 8: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Simulating a 2D Array

• We can use 1D arrays for 2D data, with a bit of cleaver indexing.

• i = row*sizeOfRow + col

[0,0]

0

[0,1]

1

[0,2]

2

[1,0]

3

[1,1]

4

[1,2]

5

[2,0]

6

[2,1]

7

[2,2]

8

row 0 row 1 row 2

Page 9: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Singly Linked List

• A singly linked list is a concrete data structure consisting of a sequence of nodes

• Each node stores– an element– a link to the next node

next

elem node

A B C D

Page 10: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Node Class for SLLspublic class Node {

private Object element;private Node next;

public Node() { this(null, null); }public Node(Object e, Node n) { element=e; next=n; }

public Object getElement() { return element; }public Node getNext() { return next; }

public void setElement(Object newElem) { element = newElem; }public void setNext(Node newNext) { next = newNext; }

}

Page 11: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Singly Linked List: head and tail• The list object must keep track of the first

node in the list – usually called the head• The list object may also keep track of the last

node in the list – usually called the tail

A B C D

head tail

Page 12: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Inserting at the Head

1. Allocate a new node2. Insert new element3. Have new node point

to old head4. Update head to point

to new node

Page 13: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Removing at the Head

1. Update head to point to next node in the list

2. Allow garbage collector to reclaim the former first node

Page 14: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Inserting at the Tail

1. Allocate a new node

2. Insert new element

3. Have new node point to null

4. Have old last node point to new node

5. Update tail to point to new node

Page 15: Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept 12 2007.

Removing at the Tail

• Removing at the tail of a singly linked list is not efficient!

• We have to look through the list to find the new tail.