1 / 13

# Lists and Structures - PowerPoint PPT Presentation

Lists and Structures. Peter Newman. Last week, we covered what single dimensional arrays and recursion were. . Quick Recap. Just to make sure... What is recursion, and what is a recursive construct known as? Give me the definition of an array ?

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Lists and Structures' - von

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Lists and Structures

Peter Newman

Last week, we covered what single dimensional arrays and recursion were.

### Quick Recap

• Just to make sure...

• What is recursion, and what is a recursive construct known as?

• Give me the definition of an array?

• Give me a drawback of arrays? (I never covered this but you might have figured it out)

This recursion were. week during lab practicals, we have to construct and populate 2D arrays. We will do this in more detail and discuss some other useful data structures you will use.

### Today

2D array?

A single dimension array (1D) array is where we have a list of contiguous memory locations containing related data. A 2D array is where we have an array of arrays. The structure when drawn out resembles a table.

What does a 2D array look like? recursion were.

Again, best to illustrate with one of my famous Powerpoint diagrams.

### 2D Array

0

1

2

3

4

5

6

7

0

1

2

3

4

5

6

7

...

Are they the same to use as normal arrays? recursion were.

Sort of...

Declare it...

int[][] myIntArray = new int[ 10][ 10 ]; //almost the same, you give it col + //row

Fill it...

myIntArray[0][0] = 10; //places an element in the ‘top left’ corner

myIntArray[0][1] = 7; //places an element in the 1st row, 2nd column

myIntArray[...][...] = 9; //etc

Use it...

System.out.println( myIntArray[0][0] ); //prints out ‘10’

### Lets use 2D Arrays...

Last week you mentions something about arrays being used for Queues and Stacks...?

Yes! 2 VERY important data structures that use arrays (or similar mechanisms) for data storage).

### Lets talk about 1D arrays again...

• Lets see what you know...

• What is a Queue? (Not a trick question)....

• What is a Stack? (Also not a trick question)...

• Can you think of an example of a stack in real life/computing.

• Can you think of an example of a queue in real life/computing.

Arrays are all I need! Queues and Stacks...

Arrays seem pretty darn awesome...is that all I need?

You wish! Arrays are limited in the way that they cannot be expanded. Once you set their size, that is the size they will be until they are ‘garbage-collected’. There are ways around this, but strictly speaking arrays don’t resize.

Well that sucks... Is there another way?

Yes there is... Through the power of linked-lists

Pause! Queues and Stacks...

Pause?

Before I continue, there is something you need to know. This is were I illustrate on the whiteboard what ‘pass-by-reference’ and ‘pass-by-value are’

What is a linked list? Queues and Stacks...

A linked list is a solution to our limited size problem. They are a chain of objects that point to each other – forming a daisy chain that we can traverse. All that you need to monitor is the start element of the list.

Each element is a class that you will make that will store the data that you wish it to contain, and a variable that will hold a reference to the next element (if there is any).

“Next” element

Element

The pages overleaf will show a working example.

Lets start with a list that is empty. As we will monitor the first element from our main program, we will just set it as null.

### Example

“start” element

“last” element

null

Lets add an element... Because Peter only did this 10 minutes before your tutorial, he will explain the intermediate steps...

### Example (2)

“start” element

“last” element

Element 1

null

Lets add another element... minutes before your tutorial, he will explain the intermediate steps...

### Example (3)

“start” element

“last” element

Element 2

Element 1

null

Question Time! minutes before your tutorial, he will explain the intermediate steps...