Lect10_ADT_Linkedlists_

Lect10_ADT_Linkedlists_ - ITI 1121. Introduction to...

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

View Full Document Right Arrow Icon
ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 23, 2010 Abstract Implementing a stack using linked elements * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates a constant number of operations. For some applications, particularly if the number of elements to be stored is not known in advance or varies during the execution of the program, arrays are not suitable. A technique frequently used consists of copying the elements of the array to a new one that’s larger and substituting the new array for the old one. The consequences are that 1) insertions into the array are now slower (because of the need to copy the elements to the new array) and 2) the amount of physical memory used is larger than the actual logical size required by the application; i.e. memory is reserved but not necessarily used.
Background image of page 2
Linked structures We now consider a data structure that always uses the exact amount of memory required by the application. This data structure grows one element at a time without copying any elements! We will need to create “containers” such that each “container” will hold one element. p 13:00 14:30 The price to pay will be that certain elements will be more accessible than others, as the above figure suggests.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Linked structures Access to the first element will be fast, but the access to the subsequent elements will necessitate “traversing” the data structure up to the element of interest. Just like arrays, each element has a single predecessor and successor (except for the first and last elements). We say that the structure is linear . Unlike the arrays, this data structure will not be implemented with contiguous memory cells.
Background image of page 4
How to implement such structure in Java? Study the following class definition (as usual, we start with the visibility of the instance variables being public, we will fix that later): public class Elem { public Object value; public Elem next; } Do you notice anything peculiar? Yes, the type of the reference next is the name of the class that we are currently defining. Hum, is this legal? Let’s try compiling this definition: > javac Elem.java it works!
Background image of page 5

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

View Full DocumentRight Arrow Icon
What does it do? Let’s see what we can do with it, let’s declare a reference of type Elem : Elem p; p Creating an instance of the class Elem . new Elem(); p
Background image of page 6
Elem to p : p Convention. I will be using circles to represent the memory diagrams of all the objects of the class Elem . The top part represents the instance variable value . The bottom par represents the instance variable
Background image of page 7

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

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

This note was uploaded on 01/06/2011 for the course ITI 1121 taught by Professor Samaan during the Spring '10 term at University of Ottawa.

Page1 / 73

Lect10_ADT_Linkedlists_ - ITI 1121. Introduction to...

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