CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Slides:



Advertisements
Similar presentations
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Advertisements

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)
Double-Linked Lists and Circular Lists
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 17: – Linked Lists Copyright © 2008 Xiaoyan Li.
CS 206 Introduction to Computer Science II 02 / 06 / 2009 Instructor: Michael Eckmann.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Summary of lectures (1 to 11)
List Interface CS java.util.List Interface and its Implementers CS340 2.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
A Introduction to Computing II Lecture 6: Lists, Stacks, and Queues Fall Session 2000.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Chapter 5 Linked Lists II
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
CS2006- Data Structures I Chapter 5 Linked Lists III.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
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.
CSCI 62 Data Structures Dr. Joshua Stough September 25, 2008.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSCS-200 Data Structure and Algorithms Lecture
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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,
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Single-Linked Lists.
4. Linked Lists.
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Chapter 18: Linked Lists.
LINKED LISTS.
Programming II (CS300) Chapter 07: Linked Lists
Presentation transcript:

CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Linked Lists Introduction – Singly-Linked List – Doubly-Linked List

CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Consists of “nodes”. Each node is linked to the next node in the list via the “next”, or “link”, etc. reference. Each node contains data “payload”.

CS-2852 Data Structures, Andrew J. Wozniewicz Singly Linked List – Class Exercise repeat until name of 1 person obtained – Ask someone for a name If the name given… – If there is no way to traverse the chain of people back to you… » Remember the name and the person (point your finger at them at all times) – If asked for a name… If you have given your name to someone already: refuse Else, give your name!

CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List: Operations Insert element at the head Add element at the end Insert element in the middle Delete element Find an element Get an element at n-th position Clear the list

CS-2852 Data Structures, Andrew J. Wozniewicz Doubly-Linked List Consists of “nodes”. Each node is linked to the next node in the list via the “next”, or “link”, or … reference. Each node is linked to th previous one via the “prev” reference.

CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Variation 1 “head”, as before “tail”, for ease of insertion

CS-2852 Data Structures, Andrew J. Wozniewicz Singly L.L. Variation 1A – Valid? Something must hold on to the head of the list, or else there is no way to access most of the elements! Must have a “head”.

CS-2852 Data Structures, Andrew J. Wozniewicz Tiger-By-The-Tail… Licensed under the Creative Commons license.

CS-2852 Data Structures, Andrew J. Wozniewicz Dbl.L.L. Variation 1 “Tail”variable - in addition to the “head”. Why? To make additions at the end easier. “Tail”variable - in addition to the “head”. Why?

CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Variation 2 “head”, as before “tail”, for ease of insertion

CS-2852 Data Structures, Andrew J. Wozniewicz SLL Variation 3 - Circular “head” – “tail” does not make much sense here… Could have an arbitrary number of variables pointing at different elements in the list, for ease of access.

CS-2852 Data Structures, Andrew J. Wozniewicz List Node A node is a data structure that consists of a data item (“payload”) and one or more links, where a link is a reference to a node. DEFINITION

CS-2852 Data Structures, Andrew J. Wozniewicz Node Class private static class Node { private E data; private Node next; private Node(E data) { this.data = data; next = null; } private Node(E data, Node ref) { this.data = data; next = ref; } A nested (inner) class – does not reference its outer class.

CS-2852 Data Structures, Andrew J. Wozniewicz Singly-Linked List Class “head”, “tail”, and “size” are fields of this SinglyLinkedList class.

CS-2852 Data Structures, Andrew J. Wozniewicz SLL – Insert at the Head public void addFirst(E value) { Node newNode = new Node (value); newNode.next = head; head = newNode; } You Try It First!

CS-2852 Data Structures, Andrew J. Wozniewicz SLL – Insert at the Tail public void addLast(E value) { Node newNode = new Node (value); Node nodeRef = head; while (nodeRef != null) if (nodeRef.next != null) nodeRef = nodeRef.next; else break; if (nodeRef == null) head = newNode; else nodeRef.next = newNode; } You Try It First!

CS-2852 Data Structures, Andrew J. Wozniewicz Summary Linked Lists Nodes – Payload – Links Singly-Linked List – Adding elements – Removing elements – Traversing Think about time-efficiency of operations! Doubly-Linked List

Questions? Image copyright © 2010 andyjphoto.com