Linked Lists.

Slides:



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

Lists CS 3358.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
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.
Linked Lists.
Linked Lists.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
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)
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
Data Structures Using C++ 2E
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,
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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)
Data Structures Using C++1 Chapter 5 Linked Lists.
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.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Linked List, Stacks Queues
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
Lecture 6 of Computer Science II
Unit – I Lists.
Linked List.
C++ Programming:. Program Design Including
Linked Lists.
Data Structure By Amee Trivedi.
Anatomy of a linked list
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Lectures linked lists Chapter 6 of textbook
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Big-O notation Linked lists
Lists CS 3358.
UNIT-3 LINKED LIST.
Problems with Linked List (as we’ve seen so far…)
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCI 3333 Data Structures Linked Lists.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Stacks, Queues, and Deques
Arrays and Linked Lists
Stacks, Queues, and Deques
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Doubly Linked Lists or Two-way Linked Lists
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Lists.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Stacks, Queues, and Deques
Linked Lists.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Linked Lists Chapter 5 (continued)
CMPT 225 Lecture 5 – linked list.
Problem Understanding
Presentation transcript:

Linked Lists

Anatomy of a linked list A linked list consists of: A sequence of nodes myList a b c d 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 (or may not) have a header

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)

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

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

Creating links in Java 44 97 23 17 myList: 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);

Singly-linked lists Here is a singly-linked list (SLL): b c d myList 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)

Creating a simple list To create the list ("one", "two", "three"): Cell numerals = new Cell(); numerals = new Cell("one", new Cell("two", new Cell("three", null))); numerals one two three

Traversing a SLL The following method traverses a list (and prints its elements): public void printFirstToLast(List here) { while (here != null) { System.out.print(here.value + " "); here = here.next; } } You would write this as an instance method of the Cell class

Traversing a SLL (animation) here three two one numerals

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

Inserting as a new first element This is probably the easiest method to implement In class Cell: Cell insertAtFront(Cell oldFront, Object value) { Cell newNode = new Cell(value, oldFront); return newNode; } Use this as: myList = insertAtFront(myList, value); Why can’t we just make this an instance method of Cell?

Using a header node A header node is just an initial node that exists at the front of every list, even when the list is empty The purpose is to keep the list from being null, and to point at the first element two one head numerals void insertAtFront(Object value) { Cell front = new Cell(value, this); this.next = front; }

Inserting a node after a given value void insertAfter(Object target, Object value) { for (Cell here = this; here != null; here = here.next) { if (here.value.equals(target)) { Cell node = new Cell(value, here.next); here.next = node; return; } } // Couldn't insert--do something reasonable here! }

Inserting after (animation) 2.5 node three two one numerals 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

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

Deleting an element from a SLL • To delete the first element, change the link in the header three two one numerals • To delete some other element, change the link in its predecessor three two one numerals (predecessor) • Deleted nodes will eventually be garbage collected

Doubly-linked lists Here is a doubly-linked list (DLL): myDLL a b c 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)

DLLs compared to SLLs Advantages: Disadvantages: 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)

Deleting a node from a DLL Node deletion from a DLL involves changing two links In this example,we will delete node b myDLL a b c We don’t have to do anything about the links in node b Garbage collection will take care of deleted nodes Deletion of the first node or the last node is a special case

Other operations on linked lists Most “algorithms” on linked lists—such as insertion, deletion, and searching—are pretty obvious; you just need to be careful Sorting a linked list is just messy, since you can’t directly access the nth element—you have to count your way through a lot of other elements

The End