Introduction to Data Structures and Algorithms

Slides:



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

Linked Lists Geletaw S..
Stacks, Queues, and Linked Lists
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
Data Structures ADT List
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures: A Pseudocode Approach with C
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of.
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.
Lists Lists as an abstract data type (ADT)
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)
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1/26 COP 3540 Data Structures with OOP Chapter 5 - Part 2 Linked Lists Abstract Data Types and Sorted Lists.
Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.
Linked Lists Tonga Institute of Higher Education.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Stacks and Queues Phil Tayco Slide version 1.0 Feb. 16, 2015.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 5 Linked Lists II
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
1 COP 3540 Data Structures with OOP Chapter 5 - Part 3 Linked Lists Doubly-Linked Lists and Iterators.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked List First next Data Linked List Link next Data Link next Data Link next Data Link Null.
Linked Data Structures
Chapter 12 – Data Structures
Stack and Queue APURBO DATTA.
COP 3538 Data Structures with OOP
8-1.
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
8-1.
Arrays and Linked Lists
Data Structures ADT List
ADT list.
A List Implementation That Links Data
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
A List Implementation That Links Data
Presentation transcript:

Introduction to Data Structures and Algorithms Chapter 5 Linked List

Lower-Level Data Structures Linked lists are: Used to implement higher-level data structures (e.g., stacks, queues, etc) Dynamic in structure

Links Each data item in a linked list is embedded in an object called link or node. Each link object has a reference to the next link object in the list

Simple Linked List The operations allowed in simple linked list are: Inserting an item at the beginning of the list Deleting the item at the beginning of a list Traversing the list to display the data items in the list

Linked list a list of items connected together as a chain Each item contains data part and reference part Data part: individual or compound data Reference part: point to the next item on the linked list (location information of next item) Operations: Insertion Find Delete (element with a target value or the head element)

Comparison to array: Similarity: linear data structure Operations: insertion, deletion, find Difference: dynamic size vs. static size in array items are linked by references (based on many isolated but related memory spaces; array: a big piece of memory space) referenced by relationship not by position (array: index) To find an element, always start from the first item (no quick access) !!!

class Link //represents one item on the list { public int iData; // data item public double dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(int id, double dd) // constructor { iData = id; // initialize data dData = dd; // ('next' is automatically } // set to null) // ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link

What is the meaning in the following Java codes? Link someLink = new Link(); Link aLink = someLink; Figure 5.2 Two Link reference variables refer to the same object of type Link

class LinkList //represents the LinkedList { private Link first; // ref to first item on list // ------------------------------------------------------------- public LinkList() // constructor { first = null; // no links on list yet } // ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return (first==null); } // ------------------------------------------------------------- public void insertFirst(int id, double dd) // insert at start of list { // make new link } // ------------------------------------------------------------- public Link deleteFirst() // delete first item { // (assumes list not empty) } // ------------------------------------------------------------- public void displayList() { } } // end class LinkList

Insert a new item at the beginning of the linked list The new item links to the original first item The first reference points to the new item See Figure 5.5 public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }

Delete the item at the beginning of the linked list store a copy the first item The first reference points to the next item Figure 5.6 public Link deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link }

Walk through all the elements on the linked list Keep updating the current reference See Figure 5.7 public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }

class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list while( !theList.isEmpty() ) // until it's empty, { Link aLink = theList.deleteFirst(); // delete link System.out.print("Deleted "); // display it aLink.displayLink(); System.out.println(""); } theList.displayList(); // display list } // end main() } // end class LinkListApp

Find the first item with the target key on the list public Link find(int key) // find link with given key { // (assumes non-empty list) Link current = first; // start at 'first' while(current.iData != key) // while no match, { if(current.next == null) // if end of list, return null; // didn't find it else // not end of list, current = current.next; // go to next link } return current; // found it }

Delete the first item with the target key in the linked list See Figure 5.8 Shift all the elements down after deletion?

public Link delete(int key) // delete link with given key { // (assumes non-empty list) Link current = first; // search for link Link previous = first; while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; }

class LinkList2App { public static void main(String[] args) { LinkList theList = new LinkList(); // make list theList.insertFirst(22, 2.99); // insert 4 items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list Link f = theList.find(44); // find item if( f != null) System.out.println("Found link with key " + f.iData); else System.out.println("Can't find link"); Link d = theList.delete(66); // delete item if( d != null ) System.out.println("Deleted link with key " + d.iData); else System.out.println("Can't delete link"); theList.displayList(); // display list } // end main() } // end class LinkList2App

How to insert an item at the end of the linked list? Double-Ended Linked list Figure 5.9 Maintain two references One reference to the first item Another reference to the last item Perform insertion and deletion at both ends of the list Improve insertion and deletion operation efficiency

class FirstLastList { private Link first; // ref to first link private Link last; // ref to last link // ------------------------------------------------------------- public FirstLastList() // constructor { first = null; // no links on list yet last = null; } // ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; } // ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { } // ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { } // ------------------------------------------------------------- public long deleteFirst() // delete first link { // (assumes non-empty list) } // ------------------------------------------------------------- public void displayList() { } // ------------------------------------------------------------- } // end class FirstLastList

// ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, last = newLink; // newLink <-- last newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } // ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink <-- last } // ------------------------------------------------------------- Figure 5.10

public long deleteFirst() // delete first link { // (assumes non-empty list) long temp = first.dData; if(first.next == null) // if only one item last = null; // null <-- last first = first.next; // first --> old next return temp; } // ------------------------------------------------------------- public long deleteLast() // delete last link { ? }

public void displayList() { System. out public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }

Running time analysis Insertion, deletion O(N) or O(1)? Compare to operations in the array

Stack Linked List Implementation The push operation on a stack, inserts the new node (with data) to the top (front) of the stack myStack.insertFirst(data) The pop operation deletes the first node of the stack myStack.deleteFirst() See Listing 5.4

Queue Linked List Implementation A node with data is inserted at the tail of the queue (last node) myQueue.insertLast(data) A node is removed from the front of the queue (the first node) myQueue.deleteFirst() See Listing 5.5