Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.

Slides:



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

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Lists CS 3358.
COMP171 Fall 2005 Lists.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
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.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Review Learn about linked lists
Stacks, Queues, and Deques. 2 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.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Stacks.
Binary Search Introduction to Trees. Binary searching & introduction to trees 2 CMPS 12B, UC Santa Cruz Last time: recursion In the last lecture, we learned.
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
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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)
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Data Structure & Algorithms
CSCS-200 Data Structure and Algorithms Lecture
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Lecture 6 of Computer Science II
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Stacks, Queues, and Deques
Arrays and Linked Lists
Stacks, Queues, and Deques
CS2013 Lecture 4 John Hurley Cal State LA.
Dynamic Data Structures and Generics
Problem Understanding
Stacks, Queues, and Deques
Presentation transcript:

Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists

Lecture 6: Linked Lists 2 CMPS 12B, UC Santa Cruz Common operation: keep a list of items It’s common in programs to want to store a collection of items Example: array Collection of items of the same type Easy to access elements of the array, but… Difficult to make the array larger Making the array smaller wastes space Alternate solution: linked lists

Lecture 6: Linked Lists 3 CMPS 12B, UC Santa Cruz Abstract data type: list Common abstract data type: list (of objects) List supports several operations Insert Delete Lookup Index Length Implementation of a list may vary Array can be used to implement a list Index & length are fast Insert can be very slow (and waste memory) Alternative: linked list

Lecture 6: Linked Lists 4 CMPS 12B, UC Santa Cruz Linked lists versus arrays Array: objects occupy contiguous memory Linked list: objects need not be contiguous Each object refers to others in the list Singly-linked list: object has reference to next one in list Doubly-linked list: object has reference to previous and next one in list Start

Lecture 6: Linked Lists 5 CMPS 12B, UC Santa Cruz So how is this done in Java? Java: reference is a name for an object One object can have multiple names Changes in the object seen through all names Two types make up a list Header type: used for the list itself Node type: used for elements in the list Object being stored: may store reference or new object Reference to other nodes public class SLList { SLLNode start; int count; } class SLLNode { SLLNode next; Object obj; }

Lecture 6: Linked Lists 6 CMPS 12B, UC Santa Cruz Definitions for SLList and SLLNode public class SLList { private SLLNode start; private int count; public void add (int index, Object item) throws ArrayIndexOutOfBoundsException; public void remove (int index) throws ArrayIndexOutOfBoundsException; public Object get (int index) throws ArrayIndexOutOfBoundsException; private SLLNode getIndex (int index) throws ArrayIndexOutOfBoundsException; public void removeAll(); private class SLLNode { SLLNode next; Object obj; } }

Lecture 6: Linked Lists 7 CMPS 12B, UC Santa Cruz Why use Object in the list? Lists contain values Strings Numbers More complex data structures We want to build a list that’ll work with anything! Write the code once and reuse it! All types in Java except builtins are descended from Object Builtins like int must use provided classes like Integer List can now be used for anything!

Lecture 6: Linked Lists 8 CMPS 12B, UC Santa Cruz Inserting into a singly linked list Inserting into a link list has two cases First in the list Not first in the list If going at head, modify head reference (only) If going elsewhere, need reference to node before insertion point New node.next = cur node.next Cur node.next = ref to new node Must be done in this order! head next

Lecture 6: Linked Lists 9 CMPS 12B, UC Santa Cruz Deleting from a singly linked list Deleting a link list has two cases First in the list Not first in the list If deleting from head, modify head reference (only) If deleting elsewhere, simply “point around” the deleted node Space for deleted nodes is automatically reclaimed (garbage collection) head next

Lecture 6: Linked Lists 10 CMPS 12B, UC Santa Cruz Traversing a singly linked list Start at the head Use a “current” reference for the current node Use the “next” reference to find the next node in the list Repeat this to find the desired node N times to find the nth node Until the object matches if looking for a particular object Caution: objects can “match” even if the references aren’t the same… Don’t forget to check to see if this is the last node head next Node 2

Lecture 6: Linked Lists 11 CMPS 12B, UC Santa Cruz More on traversing singly linked lists Check for “end of list” before moving on to the next element! Trying to dereference a null reference generates an exception Java (and C) ignore statements in logical operations if they’re not “needed” Example while (n != null && n.obj != whatWeWant) { n = n.next } If n is null, the second half of the expression is not evaluated Be careful with object comparisons! String s = “hello”; String t = “hello”; At this point, s is not equal to t: (s == t) results in false Reason: s and t refer to different objects with the same content Solution: use String.equals or similar method to compare object contents rather than references

Lecture 6: Linked Lists 12 CMPS 12B, UC Santa Cruz Doubly-linked lists Each node in the list refers to its predecessor as well as successor Twice as many references Easier to insert / delete nodes List can be traversed in either direction List typically has both head and tail references Insertion only needs a reference to an adjacent node Deletion only needs a reference to the node being deleted prev 2 next head tail prev 5 next

Lecture 6: Linked Lists 13 CMPS 12B, UC Santa Cruz Inserting into a doubly-linked list As with singly linked lists, special case for head Also special case for tail Need to update two nodes Node before new node Node after new node Hook up new node before modifying other nodes Don’t overwrite necessary information before relocating it! Head & tail: if a link is null, update the head or tail as appropriate prev 2 next head tail prev 5 next prev 4 next

Lecture 6: Linked Lists 14 CMPS 12B, UC Santa Cruz Deleting from a doubly-linked list As with singly linked lists, special case for head Also special case for tail Need to update two nodes Node before new node Node after new node Hook up new node before modifying other nodes Don’t overwrite necessary information before relocating it! Head & tail: if a link is null, update the head or tail as appropriate prev 2 next head tail prev 5 next prev 4 next

Lecture 6: Linked Lists 15 CMPS 12B, UC Santa Cruz Summary of list operations Insert objects May insert at any position Special methods to insert at head or tail? Delete objects Delete by index Delete by content Find objects Find by index number Find by (incomplete?) content Find the length of the list Next time: stacks and queues (debugging next week)