19List - ITI 1121. Introduction to Computing II * Marcel...

Info iconThis preview shows pages 1–7. 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
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 March 14, 2011 Abstract Linked List (Part 2) Tail pointer Doubly linked list Dummy node * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Time efficiency Compare the time efficiency of the dynamic array ( ArrayList ) and linked list ( LinkedList ) implementations of the interface List (both allow to store an unlimited number of objects). Let say that execution time of a method is variable (slow) if the number of operations depends on the number of elements currently stored in the data structure, and constant (fast) otherwise. Time efficiency Can you predict an overall winner beforehand? ArrayList LinkedList void addFirst( E o ) slow fast void addLast( E o ) slow slow void add( E o, int pos ) slow slow E get( int pos ) fast slow void removeFirst() slow fast void removeLast() fast slow Based on the above table, when would you use an array? Applications that need a direct (random) access to the elements. Based on the above table, when would you use a singly-linked list? Applications that add or remove elements at the start of the list only. Which implementation is more memory efficient? Speeding up addLast for SinglyLinkedList There is a simple implementation technique that makes adding an element at the end of a list fast. The problem with the singly linked list implementation is that one needs to traverse the data structure to access the last element. What if we could always access the last element efficiently as we do for the first element. Got the idea? Yes, adding an instance variable pointing to the tail element will solve our problem. Representing an empty list: head tail General case: head next value next value next value next value 10:10:00 11:00:00 12:12:00 9:59:45 tail public class SinglyLinkedList<E> implements List<E> { private static class Node<T> { private T value; private Node<T> next; private Node( T value, Node<T> next ) { this.value = value; this.next = next; } } private Node<E> head; private Node<E> tail; // ......
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 / 46

19List - ITI 1121. Introduction to Computing II * Marcel...

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

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