1 Recitation 9. Practice with linked lists Introduction. This recitation will go over the linked list implementation given on the back of this sheet and.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Data Structures ADT List
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.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Data Structure Lecture-5
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 17 Linked List.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Linked Data Structures
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Tirgul 12 Data Structures (2) 1.
Linked Lists Chapter 5 (continued)
Linked List Stacks, Linked List Queues, Dequeues
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Doubly Linked List Review - We are writing this code
Java collections library
Priority Queue.
Chapter 4 Linked Lists
Doubly linked lists.
Programmazione I a.a. 2017/2018.
Linked Lists, Stacks and Queues Textbook Sections
Queues.
Chapter 4 Linked Lists.
A Data Structure Bestiary
ADT list.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 4 Linked Lists.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Header and Trailer Sentinels
Presentation transcript:

1 Recitation 9. Practice with linked lists Introduction. This recitation will go over the linked list implementation given on the back of this sheet and show you some uses of it. The left column below gives an insertion sort algorithm. This recitation does not consider list iterators, which are a better structuring technique but in this case make the algorithms harder to write and perhaps to understand. // Perform an insertion sort on b public static void insertionsort(DoublyLinkedList b){ ListNode p= b.first(); // inv: p is the name of a node in b (or is the tail). // The nodes before p are sorted. while (b.isValid(p)) { insert(b,p); p= p.next; } // Nodes preceding node n in list b are sorted. Push n // down to its sorted place in b. // Precondition: b.isValid(p) public static void insert(DoublyLinkedList b, ListNode n) { ListNode m= n; int p= ((Integer)m.element).intValue(); // invariant: value p belongs in node m. // Nodes following m up to n are sorted and >= p // Nodes preceding m are sorted while (b.hasPrec(m) ) { ListNode mprev= b.prev(m); int beforeP= ((Integer)mprev.element).intValue(); if (beforeP <= p) { m.element= new Integer(p); return; } m.element= new Integer(beforeP); m= mprev; } m.element= new Integer(p); } a0akama.an... a0 head am m an n these are sortedthese are sorted and > p p p belongs in this node invariant of insert’s loop

2 /** An instance: doubly linked list with head, tail */ public class DoublyLinkedList { ListNode head; // the header node of this list. // head.prev = null, always ListNode tail; // the tail node of this list // tail.next = null, always int size; // number of nodes in this list /** Constructor: an empty linked list */ public DoublyLinkedList() { head= new ListNode(null, null, null); tail= new ListNode(head, null, null); head.next= tail; size= 0; } /** = the linked list is empty */ public boolean isEmpty() { return size == 0; } /** Remove all items from this linked list */ public void makeEmpty() { head.next= tail; tail.prev= head; size= 0; } /** = size of this linked list */ public int size() { return size; } /** = the first node in the list (tail if none) */ public ListNode first() { return head.next; } /** = the last node in the list (head if none) */ public ListNode last() { return tail.prev; } /** = "p is valid --is not one of head and last" */ public boolean isValid(ListNode p) { return p != head && p != tail; } /** = "a node of the list follows p" */ public boolean hasNext(ListNode p) { return p != tail && p.next != tail; } /** = "a node of the list precedes p" */ public boolean hasPrec(ListNode p) { return p != head && p.prev != head; } /** = node following node p (null if p is the tail) Precondition: p is not null */ public ListNode next(ListNode p) { return p.next; } /** = node preceding node p (null if p is head) Precondition: p is not null*/ public ListNode prev(ListNode p) { return p.prev; } /** if p is not tail, insert object x after node p. */ public void insert(Object x, ListNode p) { if (p == tail) return; ListNode n= new ListNode(p, x, p.next); p.next.prev= n; p.next= n; size= size+1; } /** if p is not head or tail, delete p from list. */ public void delete(ListNode p) { if (!isValid(p)) return; p.prev.next= p.next; p.next.prev= p.prev; size= size-1; } /** = a string that contains the elements of the list separated by commas and enclosed in "(" ")". */ public String toString() { String s= "("; ListNode c= head.next; while (c != tail) { s= s + c.element; c= c.next; if (c != tail) s= s + ", "; } return s + ")"; } /** An instance is a node of a doubly linked list */ public class ListNode { /** name of previous node (null if none) */ public ListNode prev; /** value in the node */ public Object element; /** name of next node (null if none) */ public ListNode next; // name of next node // (null if none) /** Constructor: a ListNode with element e, prev field p, and next field n */ public ListNode(ListNode p, Object e, ListNode n) { prev= p; element= e; next= n; } }