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

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

Embed Size (px)

Citation preview

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

Lilian BlotLilian Blot

LINEAR DATA STRUCTURELINKED LIST

Abstract Data Structure

Autumn 2014TPOP

1

Page 2: 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 2014TPOP

2

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

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 2014TPOP

3

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

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 2014TPOP

4

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

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 2014TPOP

5

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

Lilian Blot

The Library Project API

Autumn 2014TPOP

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

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

Lilian Blot

Using the Library Project API

See code library_test_suit.py

Autumn 2014TPOP

7

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

Lilian BlotLilian Blot

IMPLEMENTATION OFSTACKS

&QUEUES

Abstract Data Type

Autumn 2014TPOP

8

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

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 queueLIFO (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 2014TPOP

9

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

Lilian Blot

Classes Skeleton

Autumn 2014TPOP

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

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

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 2014TPOP

11

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

Lilian Blot

Defining Entities12

Autumn 2014TPOP

Nonenext

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

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

Lilian Blot

Back to our Problem

Autumn 2014TPOP

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

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

Lilian Blot

Inner Class14

Autumn 2014TPOP

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

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

Lilian Blot

Defining Entities15

Autumn 2014TPOP

NoneNode

LQueue

Empty Queue?head

tail

head

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

Lilian Blot

Implementing Queues using Linked List

16

Autumn 2014TPOP

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

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

Lilian Blot

Implementing enQueue 17

Autumn 2014TPOP

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 corre

ctly added

to queue. You should draw

the pointers to

understand.

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

Lilian Blot

Implementing enQueue 18

Autumn 2014TPOP

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

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

Lilian Blot

Implementing enQueue 19

Autumn 2014TPOP

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!

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

Lilian Blot

Implementing deQueue 20

Autumn 2014TPOP

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!

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

Lilian Blot

Summary

We have seen: Inner classes

How to implement a linear data structure Queues using Linked List as opposed to arrays

Autumn 2014TPOP

21

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

Lilian Blot

Exercises

Autumn 2014TPOP

22

Can you implement Deques and LStack using Linked Structures?

Implement an OOP representation of the Deque ADT .

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

Lilian Blot

2323

Lilian Blot Autumn 2012TPOP

23