Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Linked Lists Chapter 4.
Queues and Linked Lists
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.
Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
The Ranked Sequence ADT Definition A ranked sequence is a collection of items arranged in a linear order, where each item has a rank defining the relative.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Sequences. The Sequence Abstract Data Type Implementing a Sequence.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Lists. The Position Abstract Data Type A positionp, which is associated with some elemente in a list S, does not change, even if the rank ofe changes.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
Josephus problem (J(N-1, K) + K) mod N = J(N, K) J(1, K) = 0 N=5, K=3 Ak K=2 a N.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Elementary Data Structures
CHAPTER 4: Linked Structures
Double-Ended Queues Chapter 5.
Linked List Stacks, Linked List Queues, Dequeues
COMP9024: Data Structures and Algorithms
Doubly Linked List Review - We are writing this code
CS212D: Data Structures Week 5-6 Linked List.
Doubly linked lists.
CS212D: Data Structures Week 5-6 Linked List.
Introduction to Data Structures
Data Structures and Algorithm Design (Review).
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Header and Trailer Sentinels
Vectors, Lists, and Sequences
CS210- Lecture 6 Jun 13, 2005 Announcements
General List.
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
The Ranked Sequence ADT
Presentation transcript:

Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3

Difference from singly linked lists: -each node contains two links. -two extra nodes: header and trailer, which contain no elements.

Class DLNode

Insertion of an Element at the Head

DLNode x = new DLNode(); x.setElement(new String(Toronto)); (x.element new String(Toronto)) Have a new node: header RomeSeattle trailer Baltimore Toronto

x.setPrev(header); x.setNext(header.getNext()); (header.getNext()).setPrev(x); header.setNext(x); x.prev header; x.next header.next; header.next.prev x; header.next x; Update the links: header RomeSeattle trailer Baltimore Toronto

Deleting an Element at the Tail

((trailer.getPrev()).getPrev).setNext(trailer); trailer.setPrev((trailer.getPrev()).getPrev()); trailer.prev.prev.next trailer; trailer.prev trailer.prev.prev; Update the links: header Rome Seattle trailer BaltimoreToronto

However, for inserting a node into the middle of a double linked list or deleting a node in the middle, link hopping is always needed.

public class NodeList implements List { protected int numElts; // Number of elements in the list protected DNode header, trailer;// Special sentinels /** Constructor that creates an empty list; O(1) time */ public NodeList() { numElts = 0; header = new DNode(null, null, null);// create header trailer = new DNode(header, null, null);// create trailer header.setNext(trailer);// make header and trailer point to each other } /** Checks if position is valid for this list and converts it to * DNode if it is valid; O(1) time */ public DNode checkPosition(Position p) throws InvalidPositionException { if (p == null) throw new InvalidPositionException ("Null position passed to NodeList");

if (p == header) throw new InvalidPositionException ("The header node is not a valid position"); if (p == trailer) throw new InvalidPositionException ("The trailer node is not a valid position"); try { DNode temp = (DNode)p; if ((temp.getPrev() == null) || (temp.getNext() == null)) throw new InvalidPositionException ("Position does not belong to a valid NodeList"); return temp; } catch (ClassCastException e) { throw new InvalidPositionException ("Position is of wrong type for this list"); }

/** Returns the number of elements in the list; O(1) time */ public int size() { return numElts; } /** Returns whether the list is empty; O(1) time */ public boolean isEmpty() { return (numElts == 0); } /** Returns the first position in the list; O(1) time */ public Position first() throws EmptyListException { if (isEmpty()) throw new EmptyListException("List is empty"); return header.getNext(); } public Position last() throws EmptyContainerException { if (isEmpty()) throw new EmptyContainerException("List is empty"); return trailer.getPrev(); }

/** Returns the position before the given one; O(1) time */ public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException { DNode v = checkPosition(p); DNode prev = v.getPrev(); if (prev == header) throw new BoundaryViolationException ("Cannot advance past the beginning of the list"); return prev; } /** Insert the given element before the given position, returning * the new position; O(1) time */ public Position insertBefore(Position p, Object element) throws InvalidPositionException {// DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v.getPrev(), v, element); v.getPrev().setNext(newNode); v.setPrev(newNode); return newNode; }

public Position insertLast(Object element) {numElts++; DNode newNode = new DNode(trailer.getPrev(), trailer, element); // System.out.println(((ONode)newNode.element()).inorderNum()); trailer.getPrev().setNext(newNode); trailer.setPrev(newNode); //trailer.getPrev().setNext(newNode); return newNode; } public Position insertAfter(Position p, Object element) { DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v, v.getNext(), element); v.getNext().setPrev(newNode); v.setNext(newNode); return newNode;}

/** Insert the given element at the beginning of the list, returning * the new position; O(1) time */ public Position insertFirst(Object element) { numElts++; DNode newNode = new DNode(header, header.getNext(), element); header.getNext().setPrev(newNode); header.setNext(newNode); return newNode; }

/**Remove the given position from the list; O(1) time */ public Object remove(Position p) throws InvalidPositionException { DNode v = checkPosition(p); numElts--; DNode vPrev = v.getPrev(); DNode vNext = v.getNext(); vPrev.setNext(vNext); vNext.setPrev(vPrev); Object vElem = v.element(); // unlink the position from the list and make it invalid v.setNext(null); v.setPrev(null); return vElem; }

/** Replace the element at the given position with the new element * and return the old element; O(1) time */ public Object replace(Position p, Object element) throws InvalidPositionException { DNode v = checkPosition(p); Object oldElt = v.element(); v.setElement(element); return oldElt; } public void swapElements(Position a, Position b) throws InvalidPositionException { //System.out.println("swapElement is executed!!!"); DNode pA = checkPosition(a); DNode pB = checkPosition(b); Object temp = pA.element(); pA.setElement(pB.element()); pB.setElement(temp); }

public Position next(Position p) throws InvalidPositionException, BoundaryViolationException { DNode v = checkPosition(p); DNode next = v.getNext(); if (next == trailer) throw new BoundaryViolationException ("Cannot advance past the beginning of the list"); return next; } public Iterator positions() { return new PositionIterator(this); } public Iterator elements(){return new PositionIterator(this);} }

Data Structure Exercises 6.1

Double-Ended Queues

The Deque Abstract Data Type

public interface Deque { void insertFirst(Object e); void insertLast(Object e); Object removeFirst(); Object removeLast(); Object first(); Object last(); int size(); boolean isEmpty(); }

Implementing a Deque with a Doubly Linked List

Class MyDeque public class MyDeque implements Deque { DLNode header, trailer; int size; public MyDeque() { header = new DLNode(); trailer = new DLNode(); header.setNext( trailer ); trailer.setPrev( header ); size = 0; } headertrailer

public Object first() throws DequeEmptyException { if( isEmpty() ) throw new DequeEmptyException( "Deque is empty." ); returnheader.getNext().getElement(); } header Baltimore link hopping

public void insertFirst( Object o ) { DLNode second = header.getNext(); DLNode first = new DLNode( o, header, second ); second.setPrev( first ); header.setNext( first ); size++; } header second first Object o … header secondfirst Object o …

… public Object removeLast() { if( isEmpty() ) throw new DequeEmptyException( "Deque is empty." ); DLNode last = trailer.getPrev(); Object o = last.getElement(); DLNodesecondtolast = last.getPrev(); trailer.setPrev( secondtolast ); secondtolast.setNext( trailer ); size--; return o; } trailer secondtolastlast Object o … trailer secondtolast last Object o

public Object last() throws DequeEmptyException { if( isEmpty() ) throw new DequeEmptyException( "Deque is empty." ); return trailer.getPrev().getElement(); } public void insertLast( Object o ) { DLNode secondLast = trailer.getPrev(); DLNode last = new DLNode( o, secondLast, trailer ); secondLast.setNext( last ); trailer.setPrev( last ); size++; } public int size( ) {return size;} public boolean isEmpty( ) { return header.getNext() == trailer;} … trailer secondtolast last Object o

public Object removeFirst() { if( isEmpty() ) throw new DequeEmptyException( "Deque is empty." ); DLNode first = header.getNext(); Object o = first.getElement(); DLNode second = first.getNext(); header.setNext( second ); second.setPrev( header ); size--; return o; } public class GenerateDeque { public static void main(String args[]) { MyDeque D = new MyDeque(); int i; for (i = 0; i < 10; i++) { D.insertFirst(new Integer(i));} for (i = 0; i < 10; i++) System.out.print(((Integer) D.removeFirst()).intValue()); } header secondfirst …

Implementing Stacks and Queues with Deques

Class DequeStack

Data Structure Exercises 6.2