1 / 13

Lists and Structures

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 ?

von
Télécharger la présentation

Lists and Structures

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lists and Structures Peter Newman

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

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

  4. What does a 2D array look like? 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 ...

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

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

  7. Arrays are all I need! 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

  8. Pause! 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’

  9. 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 Element The pages overleaf will show a working example.

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

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

  12. Lets add another element... Example (3) “start” element “last” element Element 2 Element 1 null

  13. Question Time! That’s all I haveTime to ask questions....Followed by either sleep or Caffeine...

More Related