Lecture 15 Doubly Linked Lists CSE 1322 4/26/2018.

Slides:



Advertisements
Similar presentations
Lecture 15 Linked Lists part 2
Advertisements

Chapter 24 Lists, Stacks, and Queues
Data Structure Lecture-5
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
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.
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
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.
Data Structures Using Java1 Chapter 4 Linked Lists.
Chapter 14 Introduction to Data Structures. Topics Linked Lists Concepts and Structure Linked Lists of Objects Implementing a Stack Using a Linked List.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Data Structures Using C++1 Chapter 5 Linked Lists.
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,
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Chapter 4 Linked Structures.
Data Structure By Amee Trivedi.
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.
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Sit in Lab 3 ( Circular Linked List )
Review Deleting an Element from a Linked List Deletion involves:
John Hurley Cal State LA
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Linked lists.
Data Structures and Database Applications Linked Lists
Linked Lists with Tail Pointers
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Topic 11 Linked Lists -Joel Spolsky
Announcements A3 will available on Piazza tomorrow. Refer often to the Piazza FAQ Note for A3. Please read the assignment A3 FAQ Notes on the Piazza.
Practice Test – Linked Lists
Arrays and Linked Lists
List Data Structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Lecture 20 Linked Lists Richard Gesick.
CSE 143 Lecture 27: Advanced List Implementation
Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
LINKED LISTS.
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
CS148 Introduction to Programming II
List Iterator Implementation
General List.
Linked lists.
Chapter 9 Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Problem Understanding
Presentation transcript:

Lecture 15 Doubly Linked Lists CSE 1322 4/26/2018

Challenge Write a recursive method that adds 10 to the data in each node of a linked list. 4/26/2018

Challenge Solution Write a recursive method that adds 10 to the data in each node of a linked list. void AddTen(Node n) { if (n != null) n.Data += 10; AddTen(n.Next); } 4/26/2018

Doubly Linked List Each node has two links: one forward (as before) and one backward. The Node class now has an additional instance variable: previous, the previous Node. Thus, we can traverse a doubly linked list either forward or backwards. Every time we insert or delete, both links must be updated. 4/26/2018

Doubly Linked Node The right arrow represents next. A doubly linked node looks like this The right arrow represents next. The left arrow represents previous. 4/26/2018

Inserting in a Doubly Linked List There are three cases: insert at the beginning. insert in the middle. insert at the end. Updating the links will differ in the three cases above. Furthermore, when a node is inserted at the beginning, head needs to be updated. 4/26/2018

Inserting in the Middle In order to insert a node before a node named current, the following steps need to be performed: Instantiate a new node. Set two forward links: new node to current, node before current to new node. Set two backward links: current to new node, new node to node before current. Update the number of items. Updating the links will differ in the three cases above. 4/26/2018

Inserting into a Doubly Linked List 1. Instantiate the new node 2. Set next in new node to current. 4/26/2018

Inserting into a Doubly Linked List 3. Set next in node before current to the new node. 4. Set previous in the new node to the node before current. 4/26/2018

Inserting into a Doubly Linked List 5. Set previous in current to the new node. 4/26/2018

Deleting from a Doubly Linked List There are four cases: delete at the beginning. delete in the middle. delete at the end. cannot delete. Updating the links will differ in the first three cases above. Furthermore, when a node is deleted at the beginning, head needs to be updated. 4/26/2018

Deleting in the Middle of a Doubly Linked List We will delete the Player with id 7 Set next in the node before current to the node after current. 2. Set previous in the node after current to the node before current. 4/26/2018

C# doubly linked list Nodes The nodes of the linked list are called LinkedListNodes and are also generic. LinkedListNode<string> myNode = LL1.First; First and Last are properties of the LinkedList class that allow direct access to the first and last nodes respectively. 4/26/2018

C# doubly linked list LL1.AddFirst("a"); LL1.AddLast("c"); The linked list class in C# is a generic and doubly linked which means that we have direct access to the front and the end of the linked list. LinkedList<string> LL1 = new LinkedList<string>(); LL1.AddFirst("a"); LL1.AddLast("c"); 4/26/2018

Traversing by Nodes { LinkedListNode<string> myNode = L.First; public static void PrintList (LinkedList<string> L) { LinkedListNode<string> myNode = L.First; while (myNode != null) { Console.Write(myNode.Value + " "); myNode = myNode.Next; } Console.WriteLine(); 4/26/2018

Java Doubly linked list LinkedList<String> LL = new LinkedList<String>(); System.out.println(LL); LL.addFirst("bill"); System.out.println("display1 "+ LL); LL.addFirst("tom"); System.out.println("display2 "+ LL); LL.addLast("joe"); System.out.println("display3 "+ LL); LL.addFirst("art"); System.out.println("display4 "+ LL); LL.removeFirst(); System.out.println("display5 "+ LL); PrintList(LL); System.out.println("display6 "+ LL); 4/26/2018

Printing the hard way public static void PrintList (LinkedList<String> L) { LinkedList<String> temp=new LinkedList<String>(); for(String s : L) temp.addLast(s); while (temp.size()>0) { String myData = temp.removeFirst(); System.out.println(myData + " "); } System.out.println(); } 4/26/2018