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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Multidimensional Arrays in Java Vidhu S. Kapadia.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Building Java Programs Chapter 7.5
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
CSC 212 – Data Structures Lecture 13: Linked Lists.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CS212D: Data Structures Week 5-6 Linked List.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Data Structures Linked list.
CS212D: Data Structures Week 5-6 Linked List.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 7 Part 2 Edited by JJ Shepherd
Problem Understanding
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
CS210- Lecture 6 Jun 13, 2005 Announcements
Problem Understanding
Presentation transcript:

Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept

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

□ 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?

Tic-Tac-Toe How can we use a 2D array to represent a TTT board? X 1 O X 1 O

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

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

Printing a 2D Array public 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.

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 0row 1row 2

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 ABCD 

Node Class for SLLs public 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; } }

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 ABCD  headtail

Inserting at the Head 1.Allocate a new node 2.Insert new element 3.Have new node point to old head 4.Update head to point to new node

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

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

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.