Download presentation
Presentation is loading. Please wait.
Published byLawrence Simpson Modified over 9 years ago
1
Lilian Blot LINEAR DATA STRUCTURE LINKED LIST Abstract Data Structure Autumn 2014 TPOP 1
2
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
3
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
4
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
5
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
6
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
7
Lilian Blot Using the Library Project API See code library_test_suit.py Autumn 2014 TPOP 7
8
Lilian Blot IMPLEMENTATION OF STACKS & QUEUES Abstract Data Type Autumn 2014 TPOP 8
9
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
10
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
11
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
12
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
13
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
14
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
15
Lilian Blot Defining Entities 15 Autumn 2014 TPOP None Node LQueue Empty Queue? head tailhead
16
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
17
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.
18
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
19
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!
20
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!
21
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
22
Lilian Blot Exercises Autumn 2014 TPOP 22 Can you implement Deques and LStack using Linked Structures? Implement an OOP representation of the Deque ADT.
23
Lilian Blot 23 Lilian Blot Autumn 2012 TPOP 23
24
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
25
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
26
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
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.