12LinkedStack - ITI 1121. Introduction to Computing II *...

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: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 14, 2011 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. 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. 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. 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. 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! 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 Let’s assign the reference of the newly created 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 ....
View Full Document

This note was uploaded on 03/02/2012 for the course ITI 1121 taught by Professor Samaan during the Winter '10 term at University of Ottawa.

Page1 / 73

12LinkedStack - ITI 1121. Introduction to Computing II *...

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