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.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Linked Lists Geletaw S..
Introduction to Computation and Problem
0 - 0.
Addition Facts
Lecture 15 Linked Lists part 2
CS16: Introduction to Data Structures & Algorithms
Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Linked Lists Linked Lists Representation Traversing a Linked List
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
Data Structures Using C++
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
Double-Linked Lists and Circular Lists
John Hurley Cal State LA
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSE Lecture 12 – Linked Lists …
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Linked Lists.
Linked Lists.
The List ADT Textbook Sections
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-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Addition 1’s to 20.
Week 1.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
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.
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.
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)
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
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.
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.
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.
LINKED LISTS.
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Linked Lists.
Anatomy of a linked list
Linked Lists.
Stacks, Queues, and Deques
Linked Lists.
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

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 (pointer or reference) to some other node The last node contains a null link The list must have a header myList

3 More terminology A nodes successor is the next node in the sequence –The last node has no successor A nodes predecessor is the previous node in the sequence –The first node has no predecessor A lists length is the number of elements in it –A list may be empty (contain no elements)

4 Pointers and 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 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

7 Traversing a SLL 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

8 Traversing a SLL (animation) threetwo one numerals curr

9 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

10 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

11 Inserting a node after a given value 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; } // if } // for // Couldn't insert--do something reasonable! }

12 Inserting after (animation) 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

13 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 cant follow a pointer backwards Deleting the first node in a list is a special case, because the nodes predecessor is the list header

14 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

15 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; } }

16 Doubly-linked lists Here is a doubly-linked list (DLL): Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any) The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty) myDLL ab c

17 DLLs compared to SLLs Advantages: –Can be traversed in either direction (may be essential for some programs) –Some operations, such as deletion and inserting before a node, become easier Disadvantages: –Requires more space –List manipulations are slower (because more links must be changed) –Greater chance of having bugs (because more links must be manipulated)

18 Constructing SLLs and DLLs public class SLL { private SLLNode first; public SLL() { this.first = null; } // methods... } public class DLL { private DLLNode first; private DLLNode last; public DLL() { this.first = null; this.last = null; } // methods... }

19 DLL nodes in Java public class DLLNode { protected Object element; protected DLLNode pred, succ; protected DLLNode(Object elem, DLLNode pred, DLLNode succ) { this.element = elem; this.pred = pred; this.succ = succ; } }

20 Deleting a node from a DLL Node deletion from a DLL involves changing two links Deletion of the first node or the last node is a special case Garbage collection will take care of deleted nodes myDLL ab c

21 Other operations on linked lists Most algorithms on linked listssuch as insertion, deletion, and searchingare pretty obvious; you just need to be careful Sorting a linked list is just messy, since you cant directly access the n th elementyou have to count your way through a lot of other elements