Linked Lists and Abstract Data Structures A brief comparison

Similar documents
Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Standard ADTs. Lecture 19 CS2110 Summer 2009

Information Science 2

Abstract Data Types. Definitions, Implementations, and Uses

Data Structure. IBPS SO (IT- Officer) Exam 2017

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

+ Abstract Data Types

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

1 P age DS & OOPS / UNIT II

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

Ashish Gupta, Data JUET, Guna

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Dynamic Data Structures

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Introduction to Data Structures

cs Java: lecture #6

3. Fundamental Data Structures

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Summer Final Exam Review Session August 5, 2009

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CS350 - Exam 1 (100 Points)

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Data Structures Question Bank Multiple Choice

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

CIS 190: C/C++ Programming. Lecture 12 Student Choice

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Lecture Notes on Queues

CSE 230 Intermediate Programming in C and C++

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Linked Lists in C and C++

CS301 - Data Structures Glossary By

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

An Introduction to Trees

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

malloc(), calloc(), realloc(), and free()

Algorithm Design and Analysis

Basic Data Structures (Version 7) Name:

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

III Data Structures. Dynamic sets

Lecture 10 Notes Linked Lists

Data Structures I: Linked Lists

Lesson 3: Understanding General Software Development

DATA STRUCTURE AND ALGORITHM USING PYTHON

Linked Lists

Algorithms, Data Structures, and Problem Solving

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Cpt S 122 Data Structures. Data Structures

Section 05: Solutions

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

COMP 250 Midterm #2 March 11 th 2013

Lecture 10 Notes Linked Lists

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Linked List. April 2, 2007 Programming and Data Structure 1

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

CP2 Revision. theme: dynamic datatypes & data structures

COMP 401 Fall Recitation 7: Factories and Lists

G Programming Languages - Fall 2012

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Structure of Programming Languages Lecture 10

EECE.2160: ECE Application Programming

Lecture 4: Elementary Data Structures Steven Skiena

CA341 - Comparative Programming Languages

Dynamic Storage Allocation

About this exam review

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

CSE 341: Programming Languages

Programming. Lists, Stacks, Queues

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Lecture Notes on Memory Management

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Lecture Notes on Queues

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CS532: Design and Analysis of Algorithms. Overview. Linked Lists. Data Structures Intro. Classes, Objects, and Pointers.

Data Structure Series

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

The Dynamic Typing Interlude

CMSC 341 Lecture 14: Priority Queues, Heaps

CS 225. April 6 Disjoint Sets Implementation. Data Structures. Wade Fagen-Ulmschneider

Course Review. Cpt S 223 Fall 2009

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Data structure and algorithm in Python

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Datatypes. List of data structures. Datatypes ISC

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

Basic Data Structures and Heaps

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Week 6. Data structures

Transcription:

Linked Lists and Abstract Data A brief comparison 24 March 2011

Outline 1 2 3 4

Data Data structures are a key idea in programming It s just as important how you store the data as it is what you do to it A firm grasp on these structures aids in efficient coding Critical to more complex algorithms

Some more All data is stored as bits As you will see, you can impose some clever structures (patterns) as to how you interpret them to make things easier (or harder) The key is that these data structures are custom, and abstracted: define ways of extracting, adding and removing elements without worrying about implementation Abstract Data We will be looking at: Arrays, Linked Lists, Priority Queues, Hash Tables, and Sorted Sets Concept, Analysis, Application

Outline 1 2 3 4

Arrays: Concept Can be thought of as many individual variables in memory These are stored in some long list Each one has a unique index Computers count from 0 A key component in nearly all languages In general, there are no restrictions on what each element can be That is, nest arbitrarily Index 0 1 2 3... Element e 0 e 1 e 2 e 3...

Analysis We know the start of an array, and if we know the size of each element (eg: IEEE 32bit int), we can directly get to each memory location Thus, retrieving/setting at an index is O(1) What about resizing? If the array has more allocated space than it currently uses: Adding to the end is O(1) Anywhere else is O(n). Why is this the case? Determining if something is in the array is O(n)

Application Keep track of many, related variables Manipulate data that is not likely to grow or shrink too much Fast access for read and write is necessary (constant time, in essence) Allows iterative application of some operation to a whole range of variables without calling it on each one explicitly

Singly-linked List: Concept In essence, one long chain comprised of nodes Each node has some data stored, and points to the next node in the list You have a variable that points to the first node, and thus the entire list is accessed through it The last node does not point to anything of interest Head list Tail null

Retrieving Clearly, the only item we can get to directly is the first one Thus, to get to others we need to walk the list We create an iterator, and assign it to point to the first value. Then, if we want the next value, iterator iterator.next This can continue until iterator.next = null However, we can only do this in one direction be sure not to miss something the first time

Inserting Consider adding a new node, C into the singly linked list A B D null between B and D Seeing as there is only one link in each node, we only need to ensure that the thing before this new item points to it That means: C.next B.next, and B.next C (patch it in) Does order matter? Why? It doesn t matter where the node was in memory Adding to the end (tail) is easiest

Deleting Out of sight, out of memory. We depend on garbage collectors to delete items most often (can explicitly deallocate) Consider deleting the node B in A B C null Only one node points to it That means: A.next A.next.next ( skip ). Now, either our garbage collector or programmer will remove it from memory Clearly, deleting from the front (head) is the easiest

Analysis Deleting the first node and adding to the front or end: O(1) Otherwise its O(n) to insert and delete Retrieving the first and last nodes: O(n) Otherwise it is O(n) (need to traverse the list) Therefore, you can treat this as a queue or a stack and get read and write in O(1)! Determining if something is in the list is O(n) *

Applications Good way to simulate stacks and many other abstract data structures (stacks, queues, associative arrays... ) Thus can be used as a buffer Simulate sequential access devices Represents a hierarchical relationship well (with objects) Limited by one-way-ness

Implementation How would we implement this with what we already know (arrays)? Recall arrays can nest arbitrarily Recall that a node has some data and points to another node Consider A B C B, C = null A = [ data_a, B ] B = [ data_b, C ] C = [ data_c, null] --OR in general SLL=[dat1, [dat2, [dat3, [... datn, null]...]

Doubly-linked List: Concept Almost entirely like SLL However, each node also has a prev pointer This makes it far easier to traverse the list

Analysis Clearly, accessing the first and last elements is still O(1) Inserting at the end and front, and deleting from front are still O(1) However, now deleting from end is O(1) Traversing is still O(n) Deleting or inserting elsewhere is also O(n) (technically Θ(1)+search time, but this is O(n) for our purposes)

Stacks and Queues: Concept A stack is a data structure that allows you to only stack things on top of each other Push and pop only top element, FILO Used everywhere, compilers, interpreters, CPU... A queue is similar in concept to those queues you have seen Enqueue at back, dequeue at front, FIFO Used mostly as buffers

Priority queues: Concept Related to a queue, save that when an element is added, it is added with some priority So when dequeueing/popping, the element with the next highest priority is extracted (VIP service so to speak) We can now see that stacks and queues are just specific cases of priority queues Queue: priority is monotonically decreasing (last in has lowest priority) Stack: priority is monotonically increasing (last in has highest priority)

Analysis Performance is implementation specific Type Insert Delete Access Unsorted O(1) O(n) O(n) Sorted list O(n) O(1) {+ O(n)} O(1) {+ O(n)} Heap O(log n) O(log n) O(log n)

Hash-Table: Concept Arrays are indexed by position What if we want to store (Key,Value) pairs? Hash-tables provide the capacity to store a value of some type by referencing an associated key of some type It is, in essence, an array with a function that transforms the key to a (mostly) unique index In Python, we have dictionary and in Java it s called a Hashmap

Analysis Assuming the hash function (key index) takes constant time Inserting a new element (anywhere, so to speak) is O(n) Accessing an element from any key is O(1) Deleting an element ( anywhere ) is O(1) Determining whether any key is in the table is also O(1) Pointless to traverse items (by this I mean non-trivial)

Applications Whenever data needs to be indexed by a non-integral key That is, anything that relates some data to some other data Transformation tables Spell checking

Ordered Set: Concept Again analogous to an array We need to keep some set of data in some strict total ordering Through magic and clever structures, whenever some data is added to the set, it is placed in the correct location This makes getting data out also greatly simplified in some cases

Analysis To insert an element, we get O(log n) Deletion also is also of this order In fact, so is accessing an element And so is determining whether or not an element is in the set

Applications Whenever we need to maintain an ordered list of things Simplify queries on, and insertions and deletions of ordered data

Outline 1 2 3 4

Access, Insert, Delete, N.I.D DS Access Insert Delete Not In Data Array O(1) O(n) O(n) O(n) (S/D) Linked List O(n) * O(n) * O(n) * O(n) Hash-Table O(1) O(n) O(1) O(1) Ordered Set O(log n) O(log n) O(log n) O(log n) Priority Queue O(log n) O(log n) O(log n) O(log n)

Outline 1 2 3 4

Input/Output I have given you plenty of input Now is the best time of the lecture (no, not the end) - I get to ask you things... get your output Based on the previous slide, and your opions: What would you use for... and why? (Software) Interrupt Request storage? Represent the vertices of a 3D polygon? Keeping track of prime numbers for fast factorisation? Function and argument appliaction? (think RPN) Retrieving the color of a sprite depending on its shape? Emulating an old tape-drive? Calculating stats on class test marks?