Question

In: Computer Science

Add a method to OurQueue class that dequeues the Nth item on a queue and returns...

Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue.

Test it with the following code:

            Console.WriteLine("Testing DequeueNth");
            OurQueue<string> ourQ = new OurQueue<string>(12);
            // Empty Q
            try
            {
                ourQ.DequeueNth(0);
                Console.WriteLine("\a Error on empty list");
            }
            catch
            {
                Console.WriteLine("Empty Queue worked");
            }

            for (int i = 0; i < 9; ++i)
                ourQ.Enqueue("a" + i);
            for (int i = 0; i < 7; ++i)
                ourQ.Dequeue();
            for (int i = 0; i < 5; ++i)
                ourQ.Enqueue("b" + i);

            Console.WriteLine(ourQ);
            Console.WriteLine("Deleted {0}", ourQ.DequeueNth(5));
            Console.WriteLine(ourQ);
            Console.WriteLine("Deleted {0}", ourQ.DequeueNth(4));
            Console.WriteLine(ourQ);

Solutions

Expert Solution

public class ListQueue<T> : List<T>
{
new public void Add(T item) { throw new NotSupportedException(); }
new public void AddRange(IEnumerable<T> collection) { throw new NotSupportedException(); }
new public void Insert(int index, T item) { throw new NotSupportedException(); }
new public void InsertRange(int index, IEnumerable<T> collection) { throw new NotSupportedException(); }
new public void Reverse() { throw new NotSupportedException(); }
new public void Reverse(int index, int count) { throw new NotSupportedException(); }
new public void Sort() { throw new NotSupportedException(); }
new public void Sort(Comparison<T> comparison) { throw new NotSupportedException(); }
new public void Sort(IComparer<T> comparer) { throw new NotSupportedException(); }
new public void Sort(int index, int count, IComparer<T> comparer) { throw new NotSupportedException(); }

public void Enqueue(T item)
{
base.Add(item);
}

public T Dequeue()
{
var t = base[0];
base.RemoveAt(0);
return t;
}

public T Peek()
{
return base[0];
}
}


Related Solutions

Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Add a clone method to the BST program. This method returns a deep copy of a...
Add a clone method to the BST program. This method returns a deep copy of a tree. Coding Language C++ Use this main method to test your code: int main() { Tree T; T.insert(50); T.insert(20); T.insert(80); T.insert(60); T.insert(90); T.display(); Tree T1 = T; Tree T2 = T.clone(); cout << "\n\nT1 and T2 before insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); T.insert(999); cout << "\n\nT1 and T2 after insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); system("pause"); // may need to remove this on some systems return 0; } #include...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
Delete an item from a circular queue and return the index of the next item. (in...
Delete an item from a circular queue and return the index of the next item. (in Java)
Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
What is the issue is in the add method in the SongList Class? It doesn't seem...
What is the issue is in the add method in the SongList Class? It doesn't seem to add the songs when running testing. public class Song { // instance variables private String m_artist; private String m_title; private Song m_link; // constructor public Song(String artist, String title) { m_artist = artist; m_title = title; m_link = null; } // getters and setters public void setArtist(String artist) { m_artist = artist; } public String getArtist() { return m_artist; } public void setTitle(String...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT