M180: Data Structures & Algorithms in Java

Slides:



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

Linked Lists Mohammed Almashat CS /03/2006.
Lists CS 3358.
Linked Lists.
JAVA & Linked List Implementation
Linear Lists – Linked List Representation
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Linked Lists Linked Lists Representation Traversing a Linked List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Linked Lists Course Lecture Slides 23 July 2010 A list is only as strong.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures: A Pseudocode Approach with C
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
Summary of lectures (1 to 11)
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 17 Linked List.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Linked Lists Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 17 © 2002 Addison Wesley.
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.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
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.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
Data Structure & Algorithms
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
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,
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 6 of Computer Science II
Unit – I Lists.
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.
UNIT – I Linked Lists.
Big-O notation Linked lists
LINKED LISTS CSCD Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
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.
Problem Understanding
Linked List and Selection Sort
Chapter 17: Linked Lists.
Problem Understanding
Presentation transcript:

M180: Data Structures & Algorithms in Java Linked Lists – Part 1 Arab Open University

Outline Linked list nodes Linked list operations Insertion Append Deletion Other types of linked lists Doubly-linked Circular

Limitation of Arrays An array has a limited number of elements routines inserting a new value have to check that there is room Can partially solve this problem by reallocating the array as needed (how much memory to add?) adding one element at a time could be costly one approach - double the current size of the array A better approach: use a Linked List

Dynamically Allocating Elements Allocate elements one at a time as needed, have each element keep track of the next element Result is referred to as linked list of elements, track next element with a pointer

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 (reference) to some other node The last node contains a null link The list may 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)

Linked Lists Stores a collection of items non-contiguously. Each item in the list is stored with an indication of where the next item is. Must know where first item is. The list will be a chain of objects, called nodes, of type ListNode that contain the data and a reference to the next ListNode in the list. Allows addition or deletion of items in the middle of collection with only a constant amount of data movement. Contrast this with array. con•tig•u•ous adj. 1 in physical contact; touching along all or most of one side 2 near, next, or adjacent SYN. adjacent ListNode ListNode ListNode ListNode first A0 A1 A2 A3

ListNode: Definition public class ListNode <DataType> { DataType data; ListNode<DataType> next; // constructors ListNode(DataType d, ListNode<DataType> n) data = d; next = n; } ListNode(DataType d) { this (d, null); } ListNode() { this (null); }

Linked List: Insertion a b c d current Insert X immediately after current position a b c d x current

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); a b current tmp

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b current tmp

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b x current tmp

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current tmp

Implementing Insertion: Shorter Version A shorter version: // create a new node tmp = new ListNode<DataType>(x,current.next); // a’s next node is x current.next = tmp; a b x current tmp

Implementing Insertion: Shorter Version A shorter version: // create a new node tmp = new ListNode<DataType>(x,current.next); // a’s next node is x current.next = tmp; a b x current tmp

Implementing Append Insert X immediately at the end of the list // last refers to the last node in the linked list last.next = new ListNode<DataType>(); last = last.next; // adjust last last.data = x; // place x in the node last.next = null; // adjust next Most efficient approach last = last.next = new ListNode (x, null); a b c d last a b c d X last

Implementing Basic Deletion Delete an item immediately after current position Basic deletion is a bypass in the linked list. a x b current a b current

Implementing Basic Deletion Need a reference to node prior to the one to be deleted. current.next = current.next.next; a x b current a x b current a b current

Iterate Through The Linked List If items are stored in contiguous array: //step through array, outputting each item for (int index = 0; index < a.length; index++) System.out.println (a[index]); If items are stored in a linked list: // step through list, outputting each item for(ListNode p=l.first; p!=null; p=p.next) System.out.println (p.data); A0 A1 A2 A3 first