1 / 18

Abstract Data Structure

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.

selah
Download Presentation

Abstract Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Data Structure Linear data structure linked list TPOP

  2. Previously on TPOP • Keyword class • Key method __init__(…) • Key parameter self Code class DataStructureName: ``` doc-string ``` __init__(self, <parameters>): <body> TPOP

  3. 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

  4. 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

  5. Data Structure TPOP

  6. Defining Entities Node next None datum TPOP

  7. Defining Entities LinkedList Node None head Empty List? head TPOP

  8. Implementing Queues using Linked List LinkedList Node head None Code class LinkedQueue: TPOP

  9. Implementing Queues using Linked List LinkedList Node head None Code class LinkedQueue: classNode: ## inner class def__init__(self, datum, nextNode): TPOP

  10. 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

  11. 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

  12. 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

  13. Implementing Functions • printQueue(x) WRONG! Code defprintQueue(queue): print“queue: <“, whilequeue.headis not None: printqueue.head.datum, queue.head= queue.head.next print“>” TPOP

  14. Implementing Functions • printQueue(x) Code defprintQueue(queue): print“queue: <“, currentNode= queue.head whilecurrentNodeis not None: printcurrentNode.datum, currentNode= currentNode.next print“>” TPOP

  15. 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

  16. 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

  17. 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

  18. Exercises • Can you implement Deques using Singly Linked Lists? • Implement the Deque ADT. TPOP

More Related