week-13 - C O M P 2 2 I n t r o d u c t i o n t o C o m p u...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 1 COMP 202 Week 13 Dynamic/Recursive data structures Learning outcome: Self-referencing/Recursive objects Dynamic data structures C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 2 Dynamic data structure C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 3 Fixed-size vs. dynamic-size List Remember, we used arrays to represent list of objects in memory One problem of array was that Once you create the array with a particular size, you cannot resize it easily Inserting/deleting items from an array is also tedious ArrayList hides these tedious jobs, but actually does them internally There is another way of creating dynamic Lists, where insertion/deletion is actually very easy, in terms of computers work C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 4 Linked List Say 3 Student objects are created in memory student1, student2, student3 are object reference variables. Remember that references point to the actual object in memory by storing memory addresses student1 name: Sarah ID: 110023459 gpa: 3.34 name: Jeremy ID: 110233459 gpa: 3.45 name: Jason ID: 110233459 gpa: 3.35 student2 student3 C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 5 Linked List An ordered list of these 3 objects could be formed, if the first one stored the reference of second one, second one stores the reference of third one, etc. and thus creating a chain StartOfList name: Sarah ID: 110023459 gpa: 3.34 next: null /*nothing*/ name: Jeremy ID: 110233459 gpa: 3.45 next: student3 name: Jason ID: 110263476 gpa: 3.35 next: student2 C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 6 Linked List The whole list can be referred by the StartOfList reference, which basically the reference to the first element of the list The next links are also references to Student objects, storing the reference of the next object in the list Now, what attributes (member variables) should each of the Student objects contain? class Student { private String name; private int Id; private double gpa; private Student next ; /* referring to an object of same class, Hence, recursive */ /*method definitions follow */ } C O M P 2 2 I n t r o d u c t i o n t o C o m p u t in g 1 COMP 202 - Week 12 7 Inserting is easy It is very easy to insert a new object in the middle of a list....
View Full Document

This note was uploaded on 06/28/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 27

week-13 - C O M P 2 2 I n t r o d u c t i o n t o C o m p u...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online