Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation 5 CS0445 Data Structures

Similar presentations


Presentation on theme: "Recitation 5 CS0445 Data Structures"— Presentation transcript:

1 Recitation 5 CS0445 Data Structures
Mehmud Abliz

2 Program Assignment 3 A deque (short for double-ended queue) is a data structure for which elements can be added to or removed from the front or back.

3 Program Assignment 3 The DequeInterface is shown below:
public interface DequeInterface { public void addToFront(Object newEntry); public void addToBack(Object newEntry); public Object removeFront(); public Object removeBack(); public Object getFront(); public Object getBack(); public boolean isEmpty(); public void clear(); public void display(); }

4 Program Assignment 3 Suppose we start with an empty Deque d. In each line below, d is shown after the operation has been performed. d.addToFront(“Jim”); // [“Jim”] d.addToBack(“Jess”); // [“Jim”, “Jess”] d.addToFront(“Jill”); // [“Jill”, “Jim”, “Jess”] d.addToBack(“Jane”); // [“Jill”, “Jim”, “Jess”, “Jane”] String s = d.getFront(); d.addToBack(s); // [“Jill”, “Jim”, “Jess”, “Jane”, “Jill”] d.removeFront(); // [“Jim”, “Jess”, “Jane”, “Jill”] d.addToFront(d.removeBack());// [“Jill”, “Jim”, “Jess”, “Jane”]

5 Your task 1. Your task is to implement the class LinkedDeque which implements the DequeInterface as a singly-linked list (see LList). The instance variables are shown below. public class LinkedDeque implements DequeInterface{ private Node firstNode; private Node lastNode; private int length; }

6 Your task 2. In addition, you will implement an iterator for the LinkedDeque class by implementing a private inner class similar to what is done on pages for LLists. The class will be called IteratorForLinkedDeque and will implement the IteratorInterface (page 155).

7 Example public class LinkedDeque implements DequeInterface {
private Node firstNode; private Node lastNode; private int length; // <Implementation of the constructor and methods of LinkedDeque go here> // ... public IteratorInterface getListIterator() return new IteratorForLinkedDeque(); } private class IteratorForLinkedDeque implements IteratorInterface

8 Example { private Node currentNode; private Node priorNode;
public IteratorForLinkedDeque() currentNode = firstNode; priorNode = null; } // implementation of the methods in IteratorInterface go here // ... private class Node // implementation of Node class goes here


Download ppt "Recitation 5 CS0445 Data Structures"

Similar presentations


Ads by Google