lists and structures
Skip this Video
Download Presentation
Lists and Structures

Loading in 2 Seconds...

play fullscreen
1 / 13

Lists and Structures - PowerPoint PPT Presentation

  • Uploaded on

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.
Download Presentation

PowerPoint Slideshow about ' Lists and Structures' - von

An Image/Link below is provided (as is) to download presentation

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
quick recap

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


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.

2d array

What does a 2D array look like?

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

2D Array


















lets use 2d arrays

Are they the same to use as normal arrays?

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

lets talk about 1d arrays again

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!

Arrays seem pretty darn 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




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’

linked lists

What is a linked list?

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.

Linked Lists

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


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.


“start” element

“last” element


example 2

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


example 3

Lets add another element...

Example (3)

“start” element

“last” element

Element 2

Element 1