Lilian Blot LINEAR DATA STRUCTURE LINKED LIST Abstract Data Structure Autumn 2014 TPOP 1.

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Chapter 9: Data Structures I
Stack & Queues COP 3502.
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.
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Chapter 12: Data Structures
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 105 S Principles of Computer Science Linked Lists 1.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Lilian Blot Announcements The TPOP problem class this afternoon:  group 1 should come at 3.30pm and group 2 at 4pm. Teaching Evaluation Form  week 9.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Object Oriented Data Structures
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
A BSTRACT D ATA T YPES (ADT S ) COMP1927 Computing 2 16x1 Sedgewick Chapter 4.
COMPSCI 105 SS 2015 Principles of Computer Science
Tirgul 10. What We Will See Today:  Linked lists  Graphs  Trees  Iterators and Generators.
Topic 2 Collections. 2-2 Objectives Define the concepts and terminology related to collections Discuss the abstract design of collections.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Fundamentals of Programming II Linked Lists
CS505 Data Structures and Algorithms
G64ADS Advanced Data Structures
Chapter 12: Data Structures
Homework 4 questions???.
structures and their relationships." - Linus Torvalds
Pointers and Linked Lists
i206: Lecture 10: Lists, Stacks, Queues
structures and their relationships." - Linus Torvalds
Linear Data Structures Chapter 3
Computer Science 210 Computer Organization
Node Applications in Abstract Data Types
Stacks and Queues.
Introduction to Data Structure
CSCS-200 Data Structure and Algorithms
Revised based on textbook author’s notes.
structures and their relationships." - Linus Torvalds
Presentation transcript:

Lilian Blot LINEAR DATA STRUCTURE LINKED LIST Abstract Data Structure Autumn 2014 TPOP 1

Lilian Blot Today’s Learning Outcomes APIs: The Library Project Linear ADTs From Theory (Will) to Programming (Lilian)  Procedural Approach Implementing ADTs  Queues using Linked Lists Inner Classes  A class declaration inside a class declaration Autumn 2014 TPOP 2

Lilian Blot APIs In computer programming, an Application Programming Interface (API) is a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types. Autumn 2014 TPOP 3

Lilian Blot APIs An API defines functionalities that are independent of their respective implementations, which allows definitions and implementations to vary without compromising each other. A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together. Autumn 2014 TPOP 4

Lilian Blot API in Object Oriented Programming In its simplest form, an object API is a description of how objects work in a given object-oriented language  usually it is expressed as a set of classes with an associated list of class methods. The API in this case can be conceived of as the totality of all the methods publicly exposed by the classes (usually called the class interface).  This means that the API prescribes the methods by which one interacts with/handles the objects derived from the class definitions. Autumn 2014 TPOP 5

Lilian Blot The Library Project API Autumn 2014 TPOP 6 __init__(firstname, surname, postcode, uid) add_borrowed_item(item_uid) get_UID() get_borrowed() get_firstname() get_postcode() get_surname() has_items() remove_borrowed_item(item_uid) set_postcode(new_postcode) set_surname(new_name) Member BOOK = 'BOOK' CD = 'CD' DVD = 'DVD' OTHER = 'OTHER' VHS = 'VHS‘ __init__(title, author, media, uid) get_UID() get_author() get_borrower() get_media() get_title() isavailable() loan_to(member_uid) returned() Item __init__() add_item(item) add_member(member) borrow(item_uid, member_uid) delete_member(member_uid) get_member(uid) return_item(item_uid)... Library

Lilian Blot Using the Library Project API See code library_test_suit.py Autumn 2014 TPOP 7

Lilian Blot IMPLEMENTATION OF STACKS & QUEUES Abstract Data Type Autumn 2014 TPOP 8

Lilian Blot ADT API We will focus on two Abstract Data Types: FIFO (Queue)  Constructor  enqueue(obj) : add obj at the end of the queue  dequeue() : remove and return the object at the front of the queue LIFO (Stack)  Constructor  push(obj) : add obj at the top of the stack  pop() : remove and return the object at the top of the stack Autumn 2014 TPOP 9

Lilian Blot Classes Skeleton Autumn 2014 TPOP 10 class LQueue: def __init__(self): pass def enqueue(self, obj): pass def dequeue(self): pass Queue class LStack: def __init__(self): pass def push(self, obj): pass def pop(self): pass Stack

Lilian Blot Internal Representation Need to decide how data will be stored  Arrays  Dynamic arrays  Maps (bad choice)  Linked lists The Choice MUST be hidden from API users Implementation could be changed, however it MUST NOT affect programs using previous implementation. Autumn 2014 TPOP 11

Lilian Blot Defining Entities 12 Autumn 2014 TPOP None next data Node class Node: def __init__(self, data, nextNode): self.data = data self.next = nextNode # a Node object or # None if end of # linked structure def __repr__(self): return (‘<Node:’+ str(self.data)+ str(self.next) + ’>’) Code

Lilian Blot Back to our Problem Autumn 2014 TPOP 13 class LQueue: def __init__(self): pass def enqueue(self, obj): pass def dequeue(self): pass Queue class LStack: def __init__(self): pass def push(self, obj): pass def pop(self): pass Stack Using the class Node as an Inner class of LQueue

Lilian Blot Inner Class 14 Autumn 2014 TPOP class LQueue: ## inner class, watch the indentation ## internal representation using linked structure class Node: def __init__(self, data, nextNode): self.data = data self.next = nextNode def __repr__(self): return (‘<Node:’+ str(self.data)+ str(self.next) + ’>’) def __init__(self): pass … Code

Lilian Blot Defining Entities 15 Autumn 2014 TPOP None Node LQueue Empty Queue? head tailhead

Lilian Blot Implementing Queues using Linked List 16 Autumn 2014 TPOP class LQueue: ## inner class, watch the indentation ## internal representation using linked structure class Node:... def __init__(self): ’’’Construct an empty queue’’’ self._head = None # a Node object self._tail = None # a Node object self._size = 0 … Code

Lilian Blot Implementing enQueue 17 Autumn 2014 TPOP class LQueue: ## inner class class Node: …... def __init__(self):… def enqueue(self, obj): ’’’Add obj at the end of the queue’’’ new_node = LQueue.Node(obj, None) self._tail = new_node self._size += 1 Code WRONG! Node not correctly added to queue. You should draw the pointers to understand.

Lilian Blot Implementing enQueue 18 Autumn 2014 TPOP class LQueue: ## inner class class Node: …... def __init__(self):… def enqueue(self, obj): ’’’Add obj at the end of the queue’’’ new_node = LQueue.Node(obj, None) self._tail.next = new_node self._tail = new_node self._size += 1 Code WRONG! Problem when queue is empty

Lilian Blot Implementing enQueue 19 Autumn 2014 TPOP class LQueue: class Node: … def __init__(self):… def enqueue(self, obj): ’’’Add obj at the end of the queue’’’ new_node = LQueue.Node(obj, None) if self._size == 0: self._tail = new_node self._head = new_node else: self._tail.next = new_node self._tail = new_node self._size += 1 Code CORRECT!

Lilian Blot Implementing deQueue 20 Autumn 2014 TPOP class LQueue: ## inner class class Node: …... def __init__(self):… def enqueue(self, obj): … def dequeue(self): ’’’Remove obj at the front of the queue’’’ front_node = self._head self._head = self._head.next self._size -= 1 return front_node Code CORRECT!

Lilian Blot Summary We have seen: Inner classes How to implement a linear data structure  Queues using Linked List as opposed to arrays Autumn 2014 TPOP 21

Lilian Blot Exercises Autumn 2014 TPOP 22 Can you implement Deques and LStack using Linked Structures? Implement an OOP representation of the Deque ADT.

Lilian Blot 23 Lilian Blot Autumn 2012 TPOP 23

Lilian Blot Implementing enQueue 24 Autumn 2014 TPOP class LQueue: ## inner class class Node: …... def __init__(self):… def enqueue(self, obj): ’’’Add obj at the end of the queue’’’ new_node = LQueue.Node(obj, None) self._tail = new_node self._size += 1 Code

Lilian Blot Implementing enQueue 25 Autumn 2014 TPOP class LQueue: ## inner class class Node: …... def __init__(self):… def enqueue(self, obj): ’’’Add obj at the end of the queue’’’ new_node = LQueue.Node(obj, None) self._tail.next = new_node self._tail = new_node self._size += 1 Code

Lilian Blot Implementing enQueue 26 Autumn 2014 TPOP class LQueue: class Node: … def __init__(self):… def enqueue(self, obj): ’’’Add obj at the end of the queue’’’ new_node = LQueue.Node(obj, None) if self._size == 0: self._tail = new_node self._head = new_node else: self._tail.next = new_node self._tail = new_node self._size += 1 Code

Lilian Blot Implementing deQueue 27 Autumn 2014 TPOP class LQueue: ## inner class class Node: …... def __init__(self):… def enqueue(self, obj): … def dequeue(self): ’’’Remove obj at the front of the queue’’’ front_node = self._head self._head = self._head.next self._size -= 1 return front_node Code