Presentation is loading. Please wait.

Presentation is loading. Please wait.

Node Applications in Abstract Data Types

Similar presentations


Presentation on theme: "Node Applications in Abstract Data Types"— Presentation transcript:

1 Node Applications in Abstract Data Types
Jack Tompkins CSC Introduction to Data Structures

2 Queue Fields head, tail, size
We need to decide which end of ADT to use as the rear and which to use as the front implementing First In First Out (FIFO) behavior We enter the queue at the rear – enqueue will add a node making it the tail and exit from the head –dequeue will remove and return the head for our lab, raises an IndexError if dequeue is attempted on an empty queue

3 Doubly-Linked Node A node keeps track of What is next AND
What is previous

4 1 class Queue: # implemented using a Node in Lab 5 2     def __init__(self): 3        pass 4 5     def is_empty(self): 6         pass 7 8     def enqueue(self, item): 9 10 11     def dequeue(self): # raises IndexError if queue is initially empty 12 13 14     def size(self): 15 16 17    def peek(self): 18 pass 19

5 1 class Queue: # implemented using a DLL based on DLNode in Lab 3 2     def __init__(self): 3        pass 4 5     def isEmpty(self): 6         pass 7 8     def enqueue(self, item): 9 10 11     def dequeue(self): 12 13 14     def size(self): 15

6 1 class Node: 2     def __init__(self, value): 3 ""“ Initializes a node by setting its data to value and 4 prev and next to None 5 :return: The reference for self 6         """ 7         self.data = value 8 self.prev = None 9    self.next = None 10 # setters and getters written but not used for this application

7 1 class DoublyLinkedNode: 2     def __init__(self, value): 3 ""“ Initializes a node by setting its data to value and 4 prev and next to None 5 :return: The reference for self 6         """ 7         self.data = value 8 self.prev = None 9    self.next = None

8 1 class Deque(Queue): # implemented by extending the Queue class in Lab 5 2     def __init__(self): 3        super().__init__() 4 5     def peek_back(self): 6         if self.tail: 7 return self.tail.data 8     def enqueue_front(self, item): 9        new_head = Node(item) 10 if self.is_empty(): 11      self.head = new_head 12        self.tail = new_head 13 else: 14 self.head.prev= new_head 15 new_head.next = self.head 16 self.head = new_head 17 self.size += 1

9 1 class Deque(Queue): # implemented by extending the Queue class in Lab 5 18 19 def dequeue_back(self): 20        if self.is_empty(): 21 raise IndexError(‘attempted dequeue_back on an empty deque’) 22 23     data = self.tail.data 24        if self.tail.prev: 25      self.tail = self.tail.prev 26 self.tail.next = None 27 self.size -= 1 28 return data

10 Testing the Node based Queue & Deque
Place this in a PyCharm Project folder with node.py, queue.py, and deque.py

11 Using a Singly-Linked List Using a Doubly-Linked Node
Summary Using a Python List Using a Singly-Linked List Using a Doubly-Linked Node Stack Push O(1) Pop Queue Enqueue O(n) O(1)* Dequeue Deque AddFront AddRear RemoveFront RemoveRear List Add/Remove End O(1)/O(n) Add/Remove Front Add/Remove index k Get at index k


Download ppt "Node Applications in Abstract Data Types"

Similar presentations


Ads by Google