Singly linked lists (continued...)



Similar documents
Universidad Carlos III de Madrid

Algorithms and Data Structures

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Analysis of a Search Algorithm

Sequential Data Structures

Lecture 12 Doubly Linked Lists (with Recursion)

STACKS,QUEUES, AND LINKED LISTS

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

D06 PROGRAMMING with JAVA

Data Structures and Algorithms Lists

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

ADTs,, Arrays, Linked Lists

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

CmpSci 187: Programming with Data Structures Spring 2015

What Is Recursion? Recursion. Binary search example postponed to end of lecture

NPV Versus IRR. W.L. Silber We know that if the cost of capital is 18 percent we reject the project because the NPV

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

recursion, O(n), linked lists 6/14

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Introduction to Data Structures and Algorithms

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Practice with Proofs

Module 2 Stacks and Queues: Abstract Data Types

Section IV.1: Recursive Algorithms and Recursion Trees

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Algorithms and Data Structures

Node-Based Structures Linked Lists: Implementation

Unordered Linked Lists

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Chapter 3: Restricted Structures Page 1

Abstract Data Types. Chapter 2

The ADT Binary Search Tree

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

TREE BASIC TERMINOLOGIES

Data Structures and Algorithms

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Common Data Structures

COMP 110 Prasun Dewan 1

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Data Structures UNIT III. Model Question Answer

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Chapter 8 Hypothesis Testing Chapter 8 Hypothesis Testing 8-1 Overview 8-2 Basics of Hypothesis Testing

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Introduction to Object-Oriented Programming

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Lecture Notes on Binary Search Trees

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures and Algorithms Written Examination

A Labeling Algorithm for the Maximum-Flow Network Problem

Project 4 DB A Simple database program

Lab Experience 17. Programming Language Translation

CSE 211: Data Structures Lecture Notes VII

Dynamic Programming Problem Set Partial Solution CMPSC 465

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Programming Languages

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Token Sequencing Approach to Prevent SQL Injection Attacks

Linked List as an ADT (cont d.)

Ordered Lists and Binary Trees

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

NUMBER SYSTEMS. William Stallings

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Material Requirements Planning MRP

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Data Structures and Algorithms

Programming with Data Structures

Optimal Binary Search Trees Meet Object Oriented Programming

Lecture 11: Tail Recursion; Continuations

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Sequences in the C++ STL

Linux Process Scheduling Policy

Limitations of Data Encapsulation and Abstract Data Types

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Linked Lists: Implementation Sequences in the C++ STL

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

DATA STRUCTURES USING C

3.4 Statistical inference for 2 populations based on two samples

Normal Approximation. Contents. 1 Normal Approximation. 1.1 Introduction. Anthony Tanbakuchi Department of Mathematics Pima Community College

Class 32: The Java Collections Framework

AP Computer Science A 2004 Free-Response Questions

Chapter 2 Probability Topics SPSS T tests

Lecture 11 Array of Linked Lists

Linear Programming. March 14, 2014

Lecture Notes on Binary Search Trees

1.2 Solving a System of Linear Equations

>

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Cpt S 223. School of EECS, WSU

Transcription:

Singly linked lists (continued...) I began the lecture by discussing two more methods that are commonly defined for linked lists, namely adding or removing an element at the back of the list. This requires manipulating the tail reference. Adding a node at the tail can be done in a small number of steps. addlast( newnode ){ tail.next = newnode tail = tail.next Removing the last node from a list, however, requires many steps. The reason is that you need to modify the next reference of the the node that comes before the tail node which you want to remove. But you have no way to directly access the node that comes before tail, and so you have to find this node by searching from the front of the list. The algorithm begins by checking if the list has just one element. If it does, then the last node is the first node and this element is removed. Otherwise, it scans the list for the next-to-last element. removelast(){ if (head == tail) head = null tail = null else{ tmp = head while (tmp.next!= tail){ tmp = tmp.next tmp.next = null tail = tmp This method requires about size steps. This is significantly more expensive than what we had with an array implementation, where we had a constant cost in removing the last element from a list. We will come back to this comparison at the end of the lecture, when we compare arrays with singly and doubly linked lists. Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list. The fact that each node held a primitive type versus a reference type, or whether the reference type was a Shape object or Dog object (or whatever) was not important to that discussion. Rather, we were mainly concerned with how to manipulate the nodes in the list. 1

When you implement a linked list in some programming language such as Java, you do have to consider the types of elements at each node. However, you don t want to have to reimplement your linked list class for each new type (Shape, Dog, etc. Java allows you to write classes with a generic type, which addresses this issue. Rather than declaring classes for a particular type for example, being an or Shape as we saw last lecture we would like to define our linked list in a more general way. class SNode{ SNode element next class SLinkedList{ SNode head; SNode tail; Java allows us to do so. We can declare these classes to have a generic type. class SNode<>{ element SNode<> next class SLinkedList<>{ SNode<> head; SNode<> tail; This way, we can write all the methods of these classes such that they can be used for any type of reference type. For example, we could have a linked list of Shape objects, or a linked list of Student objects, etc. SLinkedList<Shape> SLinkedList<Student> shapelist = new SLinkedList<Shape>(); studentlist = new SLinkedList<Student>(); You should think of the class name as a parameter that is passed to the constructor SLinkedList<>(). See the examples that are given in the online code. 2

Doubly linked lists The S in the SLinkedList class did not stand for Shape, but rather it stood for singly, namely there was only one link from a node to another node. We next look at doubly linked lists. ach node of a doubly linked list has two links rather than one, namely each node of a doubly linked list has a reference to the previous node in the list and to the next node in the list. These reference variables are typically called prev and next. class DNode<>{ element; DNode<> next; DNode<> prev; class DLinkedList<>{ DNode<> head; DNode<> tail; One advantage of doubly linked lists is that they allow us to access elements near the back of the list without having to step all the way from the front of the list. Recall the removelast() method from earlier this lecture. To remove the last node of a singly linked list, we needed to follow the next references from the head all the way to the node whose next node was the last/tail node. This required about size steps which is inefficient. If you have a doubly linked list, then you can remove the last element in the list in a constant number of steps, e.g. tail = tail.prev tail.next = null size = size-1 More generally, with a doubly linked list, we can remove an arbitrary element in the list in constant time. remove( node ){ node.prev.next = node.next node.next.prev = node.prev size-- (Here I am not concerning myself with the next and prev references in the removed node, i.e. if we don t set them to null then they will continue to po to nodes in the list.) Another advantage of doubly linked lists is that, if we want to access an element, then we don t necessarily need to start at the beginning of the list. If the node containing the element is near the 3

back of the list, then we can access the node by following the links from the back of the list. For example, suppose we wished to remove the i th node. getnode(i){ if (i < size/2){ tmp = head index = 0 while (index < i){ tmp = tmp.next index++ else{ tmp = tail index = size - 1 while (index > i){ tmp = tmp.prev index-- return tmp This algorithm takes at most size/2 steps, instead of size steps. Dummy nodes The above method for removing a node assumes that node.prev and node.next are well defined. However, this sometimes will not be the case, for example, if the node is the first or the last in the list, respectively. Thus, to write correct code for removing an element, you need to take these special end cases o account. (Recall the removelast() method on page 1.) It is easy to forget to do so. To avoid this problem, it is common to define linked lists by by using a dummy head node and a dummy tail node, instead of head and tail reference variables. The dummy nodes 1 are nodes just like the other nodes in the list, except that they do not contain an element (e.g. a shape object). Rather the element field pos to null. These nodes do not contribute to the size count. class DLinkedList<>{ DNode<> dummyhead; DNode<> dummytail; 1 Dummy nodes are somewhat controversial. Some programmers regard them as an ugly hack. Others find them elegant and useful. I personally don t have a strong opinion either way. I am teaching you about dummy nodes so that you are aware of them. 4

DLinkedList<>(){ dummyhead = new DNode<>(); dummytail = new DNode<>(); size = 0; //... lots of list methods Comparison of arrays and linked lists Let s compare the time required to execute several methods, using arrays versus singly versus doubly linked lists. Let N = size, namely the number of elements in a list. array singly doubly linked list linked list ------ ---------- ---------- addfirst N 1 1 removefirst N 1 1 addlast 1 1 1 removelast 1 N 1 getnode(i) 1 i min( i, N/2 - i) TODO To understand how linked lists work, you should go beyond reading these notes. You should do the xercises 2a, and you should also have a look and run the code that I have made available to you (next to these lecture notes). 5