Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.

Slides:



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

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Lists CS 3358.
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Linked Lists Linked Lists Representation Traversing a Linked List
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
CSE Lecture 12 – Linked Lists …
Linked Lists.
Linked Lists.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Linked Lists. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
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.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Data Structures and Algorithms Lecture 7,8 and 9 (Linked List) Instructor: Quratulain Date: 25, 29 September and 2 October, 2009 Faculty of Computer Science,
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
Data Structures Using Java1 Chapter 4 Linked Lists.
Linked Lists. 2 Anatomy of a linked list A linked list consists of: A sequence of nodes abcd  Each node contains a value  and a link (pointer or reference)
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Chapter 5 Linked Lists II
Data Structures Using C++1 Chapter 5 Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Recitation 7 Collections. Array List and Linked List Array List and Linked List are implementations of the same interface: List. As a result, they have.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
List Interface and Linked List Mrs. Furman March 25, 2010.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CSCS-200 Data Structure and Algorithms Lecture
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,
CSCE , SPRING 2016 INSTRUCTOR: DR. NANCY M. AMATO 1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Linked Lists.
Anatomy of a linked list
Lectures linked lists Chapter 6 of textbook
Big-O notation Linked lists
UNIT-3 LINKED LIST.
Linked Lists.
Stacks, Queues, and Deques
Linked Lists.
8 List ADTs List concepts. List applications.
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Linked Lists [AJ 15] 1

2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer or reference) to some other node the last node contains a null link the list may have a header myList These slides are based on Dr. David Matuszek’s lecturesDr. David Matuszek

3 More Terminology  a node’s successor is the next node in the sequence  the last node has no successor  a node’s predecessor is the previous node in the sequence  the first node has no predecessor  a list’s length is the number of elements in it  a list may be empty (contain no elements)

4 Pointers vs. References  in C and C++ we have “pointers,” while in Java we have “references”  these are essentially the same thing  the difference is that C and C++ allow you to modify pointers in arbitrary ways, and to point to anything  in Java, a reference is more of a “black box,” or ADT  Available operations are:  dereference (“follow”)  copy  compare for equality  there are constraints on what kind of thing is referenced: for example, a reference to an array of int can only refer to an array of int

5 Creating References  the keyword new creates a new object, but also returns a reference to that object  for example, Person p = new Person("John")  new Person("John") creates the object and returns a reference to it  we can assign this reference to p, or use it in other ways

6 Creating Links in Java class Cell { int value; Cell next; Cell (int v, Cell n) { // constructor value = v; next = n; } } Cell temp = new Cell(17, null); temp = new Cell(23, temp); temp = new Cell(97, temp); Cell myList = new Cell(44, temp); myList :

7 Singly-Linked Lists  here is a singly-linked list (SLL):  each node contains a value and a link to its successor (the last node has no successor)  the header points to the first node in the list (or contains the null link if the list is empty) abcd myList

8 Singly-Linked Lists in Java public class SLL { private SLLNode first; public SLL() { this.first = null; } // methods }  this class actually describes the header of a singly-linked list  however, the entire list is accessible from this header  users can think of the SLL as being the list  users shouldn’t have to worry about the actual implementation From, Java Collections: An Introduction to Abstract Data Types, Data Structures and Algorithms by David A. Watt, Deryck F. Brown, Dave Watt

9 SLL node s in Java public class SLLNode { protected Object element; protected SLLNode succ; protected SLLNode(Object elem, SLLNode succ) { this.element = elem; this.succ = succ; } From, Java Collections: An Introduction to Abstract Data Types, Data Structures and Algorithms by David A. Watt, Deryck F. Brown, Dave Watt

10 Creating a Simple List  to create the list ("one", "two", "three"):  SLL numerals = new SLL();  numerals.first = new SLLNode("one", new SLLNode("two", new SLLNode("three", null))); threetwoone numerals

11  the following method traverses a list (and prints its elements): public void printFirstToLast() { for (SLLNode curr = first; curr != null; curr = curr.succ) { System.out.print(curr.element + " "); } }  you would write this as an instance method of the SLL class From, Java Collections: An Introduction to Abstract Data Types, Data Structures and Algorithms by David A. Watt, Deryck F. Brown, Dave Watt Traversing a SLL

12 Traversing a SLL threetwo one numerals curr

13 Inserting a node into a SLL  there are many ways you might want to insert a new node into a list:  as the new first element  as the new last element  before a given node (specified by a reference)  after a given node  before a given value  after a given value  all are possible, but differ in difficulty

14 Inserting as a New First Element  this is probably the easiest method to implement  in class SLL (not SLLNode ): void insertAtFront(SLLNode node) { node.succ = this.first; this.first = node; }  notice that this method works correctly when inserting into a previously empty list

15 void insertAfter(Object obj, SLLNode node) { for (SLLNode here = this.first; here != null; here = here.succ) { if (here.element.equals(obj)) { node.succ = here.succ; here.succ = node; return; } // Couldn't insert--do something reasonable! } From, Java Collections: An Introduction to Abstract Data Types, Data Structures and Algorithms by David A. Watt, Deryck F. Brown, Dave Watt Inserting a node After a Given Value

16 Inserting a node After a Given Value threetwo one numerals 2.5 node find the node you want to insert after first, copy the link from the node that's already in the list then, change the link in the node that's already in the list

17 Deleting a node from a SLL  in order to delete a node from a SLL, you have to change the link in its predecessor  this is slightly tricky, because you can’t follow a pointer backwards  deleting the first node in a list is a special case, because the node’s predecessor is the list header

18 Deleting an element from a SLL threetwo one numerals threetwo one numerals to delete the first element, change the link in the header to delete some other element, change the link in its predecessor deleted nodes will eventually be garbage collected

19 Deleting from a SLL public void delete(SLLNode del) { SLLNode succ = del.succ; // If del is first node, change link in header if (del == first) { first = succ; } else { // find predecessor and change its link SLLNode pred = first; while (pred.succ != del) pred = pred.succ; pred.succ = succ; } From, Java Collections: An Introduction to Abstract Data Types, Data Structures and Algorithms by David A. Watt, Deryck F. Brown, Dave Watt

Circular Lists  in open or linear list the last node the link field contains a null reference  but, in circular list, it is made to point to the first node of the list  the list is circular or circularly linked 20 abcd myList

Doubly Linked List  each node contains a second link field pointing to the previous node in the sequence, along with, the next- node link  the two links may be called forward(s) and backwards, or next and prev(ious) 21 From Wikipedia