Lists Lists as an abstract data type (ADT)

Slides:



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

Stacks, Queues, and Linked Lists
Linked Lists.
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
M180: Data Structures & Algorithms in Java
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.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures: A Pseudocode Approach with C
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.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
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 Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
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.
Doubly Linked Lists Deleting from the end of the list – Have to traverse the entire list to stop right in front of tail to delete it, so O(n) – With head.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
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.
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.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Linked Lists A dynamically resizable efficient list implementation.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
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.
Introduction to Data Structures and Algorithms
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CSE 1342 Programming Concepts
Introduction What Is Linked List? (2002 , 2007,2011)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Linked Lists.
Data Structures ADT List
Data Structures ADT List
Data Structures ADT List
Lists.
Presentation transcript:

Lists Lists as an abstract data type (ADT) Different list implementations and the tradeoffs of each approach

What Is A List? A method of organizing data From “Data Structures and Abstractions with Java” by Carrano and Savitch

Common List Operations Adding new elements Ordered by time Ascending/descending order Ordered by frequency Removing an element/elements Replace an element with a new value Searching the list for an element Counting the elements in the list Checking if the list is full or empty Display all elements

List Implementations List Array Linked list ADT (general concept) Data structure (specific) Array Linked list

Lists Implemented As Arrays Advantages Simple to use (often a built-in type) Retrievals are quick if the index is known (O(n1)) Disadvantages Adding/removing elements may be awkward Fixed size arrays either limits the size of the list or wastes space Dynamic sized arrays requires copying

Arrays: Adding Elements In The Middle 123 125 135 155 161 165 166 167 167 169 177 178

Arrays: Deleting Elements From The Middle 123 125 135 155 161 166 167 167 169 177 178

Arrays: Dynamic Sized Arrays int [] arr = new int[4]; : : : int [] temp = arr; int [] arr = new int[8]; // Copy from temp to arr is needed

Lists Implemented As Linked Lists Types Of Linked Lists Singly linked Circular Doubly linked

Singly Linked List Example: The full example can be found in the directory: /home/331/tamj/examples/lists/singlyLinked class ListManager { private Node head; private int length; private int currentDataValue = 10; private static final int MAX_DATA = 100; : : }

List Operations: Arrays Vs. Singly Linked Lists Initialization O (n) O (1)

Examples Of List Initializations Array for (i = 0; i < list.length; i++) list[i] = -1; Linked list public ListManager () { head = null; length = 0; } public ListManager (Node newHead) head = newHead; length = 1;

List Operations: Arrays Vs. Singly Linked Lists Search O (n) Sequential O (log2n) Binary O (n)

Example Of A Linked List Search public int search (int key) { Node temp = head; boolean isFound = false; int index = 1;

Example Of A Linked List Search (2) while ((temp != null) && (isFound == false)) { if (temp.data == key) isFound = true; } else temp = temp.next; index++;

Example Of A Linked List Search (3) if (isFound == true) return index; else return -1; }

List Operations: Arrays Vs. Singly Linked Lists Insertion O (1) No shifting O (n) Shifting O (n)

Example Of A Linked List Insertion public void addToEnd () { Node anotherNode = new Node (currentDataValue); currentDataValue += 10; Node temp; if (isEmpty() == true) head = anotherNode; length++; }

Example Of A Linked List Insertion (2) else { temp = head; while (temp.next != null) temp = temp.next; } temp.next = anotherNode; length++;

Another Example Of A Linked List Insertion public void addToPosition (int position) { Node anotherNode = new Node (currentDataValue); Node temp; Node current; int index; if ((position < 1) || (position > (length+1))) System.out.println("Position must be a value between 1-" + (length+1)); }

Another Example Of A Linked List Insertion (2) else { if (isEmpty() == true) if (position == 1) length++; head = anotherNode; } System.out.println("List empty"); else if (position == 1) anotherNode.next = head;

Another Example Of A Linked List Insertion (3) else { current = head; index = 1; while (index < (position-1)) current = current.next; index++; } anotherNode.next = current.next; current.next = anotherNode; length++;

List Operations: Arrays Vs. Singly Linked Lists Deletion O (1) No shifting O (n) Shifting O (n)

An Example Of A Linked List Deletion public void delete (int key) { int indexToDelete; int indexTemp; Node previous; Node toBeDeleted; indexToDelete = search(key); if (indexToDelete == -1) System.out.println("Cannot delete element because it was not found in the list."); }

An Example Of A Linked List Deletion (2) else { if (indexToDelete == 1) head = head.next; length--; }

An Example Of A Linked List Deletion (3) else { previous = null; toBeDeleted = head; indexTemp = 1; while (indexTemp < indexToDelete) previous = toBeDeleted; toBeDeleted = toBeDeleted.next; indexTemp++; } previous.next = toBeDeleted.next; length--;

Recursively Processing A List public void displayReverse () { Node temp = head; System.out.println("Displaying list in reverse order"); if (isEmpty() == false) reverse(temp); else System.out.println("Nothing to display, list is empty"); } private void reverse (Node temp) if (temp.next != null) reverse(temp.next); System.out.println(temp.data);

Circular Linked Lists An extra link from the end of the list to the front forms the list into a ring List Data Ptr Data Ptr Data Ptr

Uses Of A Circular List e.g., Memory management by operating systems RAM A = B + C

Searches With A Circular Linked Lists Cannot use a null reference as the signal that the end of the list has been reached. Must use the list reference as a point reference (stopping point) instead List Data Ptr Data Ptr Data Ptr temp temp temp

Traversing A Circular Linked List Cannot use a null reference as the signal that the end of the list has been reached. Must use the list reference as a point reference (stopping point) instead List Data Ptr Data Ptr Data Ptr temp

An Example Of Traversing A Circular Linked List public void display () { Node temp = list; System.out.println("Displaying list"); if (isEmpty() == true) System.out.println("Nothing to display, list is empty."); } do System.out.println(temp.data); temp = temp.next; } while (temp != list); System.out.println();

Worse Case Times For Circular Linked Lists Operation Time Search O(n) Addition Deletion

Doubly Linked Lists Each node has a reference or pointer back to the previous nodes Head Data Ptr null

Pros Of Doubly Linked Lists Traversing the list in reverse order is now possible. You can traverse a list without a trailing reference (or by scanning ahead) It’s more efficient for lists that require frequent additions and deletions near the front and back From “Data Structures and Abstractions with Java” by Carrano and Savitch

Cons Of Doubly Linked Lists An extra reference is needed Additions and deletions are more complex (especially near the front and end of the list)

Doubly Linked List Example: The full example can be found in the directory: /home/331/tamj/examples/lists/doublyLinked class ListManager { private Node head; private int length; private int currentDataValue = 10; private static final int MAX_DATA = 100; : : : : }

Doubly Linked List: Adding To The End public void addToEnd () { Node anotherNode = new Node (currentDataValue); Node temp; if (isEmpty() == true) head = anotherNode;

Doubly Linked List: Adding To The End (2) else { temp = head; while (temp.next != null) temp = temp.next; } temp.next = anotherNode; anotherNode.previous = temp; currentDataValue += 10; length++;

Doubly Linked List: Adding Anywhere public void addToPosition (int position) { Node anotherNode = new Node (currentDataValue); Node temp; Node prior; Node after; int index; if ((position < 1) || (position > (length+1))) System.out.println("Position must be a value between 1-" + (length+1)); }

Doubly Linked List: Adding Anywhere (2) else { // List is empty if (head == null) if (position == 1) currentDataValue += 10; length++; head = anotherNode; } System.out.println("List empty, unable to add node to " + "position " + position);

Doubly Linked List: Adding Anywhere (3) // List is not empty, inserting into first position. else if (position == 1) { head.previous = anotherNode; anotherNode.next = head; head = anotherNode; currentDataValue += 10; length++; }

Doubly Linked List: Adding Anywhere (4) // List is not empty inserting into a position other than the first else { prior = head; index = 1; // Traverse list until current is referring to the node in front // of the position that we wish to insert the new node into. while (index < (position-1)) prior = prior.next; index++; } after = prior.next;

Doubly Linked List: Adding Anywhere (5) // Set the references to the node before the node to be // inserted. prior.next = anotherNode; anotherNode.previous = prior; // Set the references to the node after the node to be if (after != null) after.previous = anotherNode; anotherNode.next = after; currentDataValue += 10; length++; }

Doubly Linked List: Deleting A Node public void delete (int key) { int indexToDelete; int indexTemp; Node previous; Node toBeDeleted; Node after;

Doubly Linked List: Deleting A Node (2) indexToDelete = search(key); // No match, nothing to delete. if (indexToDelete == -1) { System.out.println("Cannot delete element with a data value of " + key + " because it was not found."); } else // Deleting first element. if (indexToDelete == 1) head = head.next; length--;

Doubly Linked List: Deleting A Node (3) else { previous = null; toBeDeleted = head; indexTemp = 1; while (indexTemp < indexToDelete) previous = toBeDeleted; toBeDeleted = toBeDeleted.next; indexTemp++; } previous.next = toBeDeleted.next; after = toBeDeleted.next; after.previous = previous; length--; : : :

Tracking Two-Dimensional Information Example: Student grades1 Students [0] [1] [2] … [30000] [0] [1] [2] : [300] Courses 1 Example based on the described in “Data Structures and Algorithms in Java” by Adam Drozdek

Tracking Two-Dimensional Information Example: Student grades1 Problem: Wasted space Students [0] [1] [2] … [30000] [0] [1] [2] : [300] A F W B- D Courses 1 Example based on the described in “Data Structures and Algorithms in Java” by Adam Drozdek

Sparse Matrices/ Sparse Table Memory is allocated only as needed (compile arrays and linked lists) Example based on the described in “Data Structures and Algorithms in Java” by Adam Drozdek

You Should Now Know The advantages and disadvantages of implementing a list as an array and as a linked list. The amount of time taken to perform different list operations on an array vs. a linked list. How different types of linked lists are implemented, issues associated with each implementation and the speed of different list operations. What is a sparse table and what is the advantage and disadvantage of implementing it as an array vs. as a linked list.

Sources Of Lecture Material Data Structures and Abstractions with Java by Frank M. Carrano and Walter Savitch Data Abstraction and Problem Solving With Java: Walls and Mirrors by Frank M. Carrano and Janet J. Prichard “Data Structures and Algorithms in Java” by Adam Drozdek CPSC 331 course notes by Marina L. Gavrilova http://pages.cpsc.ucalgary.ca/~marina/331/