180 likes | 284 Views
Abstract Data Structure. Linear data structure linked list. Previously on TPOP. Keyword class Key method __ init __(…) Key parameter self. Code. class DataStructureName : ``` doc-string ``` __ init __(self, <parameters>): <body>. Previously on TPOP.
E N D
Abstract Data Structure Linear data structure linked list TPOP
Previously on TPOP • Keyword class • Key method __init__(…) • Key parameter self Code class DataStructureName: ``` doc-string ``` __init__(self, <parameters>): <body> TPOP
Previously on TPOP • ClassAddress is the definition of an object • my_address is an instance of Address • number, city, postcode,… are attributes • __init__ is a constructor • __repr__ is a method TPOP
Today’s Learning Outcomes • 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 TPOP
Data Structure TPOP
Defining Entities Node next None datum TPOP
Defining Entities LinkedList Node None head Empty List? head TPOP
Implementing Queues using Linked List LinkedList Node head None Code class LinkedQueue: TPOP
Implementing Queues using Linked List LinkedList Node head None Code class LinkedQueue: classNode: ## inner class def__init__(self, datum, nextNode): TPOP
Implementing Queues using Linked List LinkedList Node head None Code class LinkedQueue: classNode: ## inner class def__init__(self, datum, nextNode): self.datum = datum self.next = nextNode TPOP
Implementing Queues using Linked List • Construct an empty queue: Code class LinkedQueue: classNode: ## inner class def__init__(self, datum, nextNode): self.datum = datum self.next = nextNode def__init__(self): …? TPOP
Implementing Queues using Linked List • Construct an empty queue: Code class LinkedQueue: classNode: ## inner class def__init__(self, datum, nextNode): self.datum = datum self.next = nextNode def__init__(self): self.head = None ## a Node object self.tail = None ## a Node object self.size = 0 TPOP
Implementing Functions • printQueue(x) WRONG! Code defprintQueue(queue): print“queue: <“, whilequeue.headis not None: printqueue.head.datum, queue.head= queue.head.next print“>” TPOP
Implementing Functions • printQueue(x) Code defprintQueue(queue): print“queue: <“, currentNode= queue.head whilecurrentNodeis not None: printcurrentNode.datum, currentNode= currentNode.next print“>” TPOP
Implementing Functions • enQueue(queue, x) WRONG! Code defenQueue(queue, x): newNode= LinkedQueue.Node(x, None) tailNode= queue.tail tailNode.next= newNode queue.size+= 1 TPOP
Summary We have seen: • Inner classes • How to implement a linear data structure • Queues using Linked List as opposed to arrays • How to implement functions using linear data structures TPOP
Exercises • Implement the dequeue(queue) function • Implement a first(queue) function that returns the first element in queue but does not remove it from the queue. • For both functions, what would you do if the queue is empty? Argue your choice with a fellow student. TPOP
Exercises • Can you implement Deques using Singly Linked Lists? • Implement the Deque ADT. TPOP