Question: The Assignment 1)Please describe the function of the following methods add(), deleteMin(), and size() that are supported by the priority queue interface. 2)Implement those methods
The Assignment
1)Please describe the function of the following methods add(), deleteMin(), and size() that are supported by the priority queue interface.
2)Implement those methods using a singly-linked list.
3) convert the given java program to C++ , write comments where needed and include a summary of the program:
package com.JavaDS;
public class SinglyLinkList{
private Node head;
private Node tail;
//method to implement add operation on Singly Linked List
public void add(T element){
Node nd = new Node();
nd.setValue(element);
System.out.println("Adding: "+element);
if(head == null){
head = nd;
tail = nd;
} else {
tail.setNextRef(nd);
tail = nd;
}
}
//method to implement add operation in another way on Singly linked list
public void addAfter(T element, T after){
Node tmp = head;
Node refNode = null;
System.out.println("Searching in all nodes..");
while(true){
if(tmp == null){
break;
}
if(tmp.compareTo(after) == 0){
refNode = tmp;
break;
}
tmp = tmp.getNextRef();
}
if(refNode != null){
Node nd = new Node();
nd.setValue(element);
nd.setNextRef(tmp.getNextRef());
if(tmp == tail){
tail = nd;
}
tmp.setNextRef(nd);
} else {
System.out.println("Element not found...");
}
}
//method to implement remove operation at start node of Singly Linked List
public void deleteFront(){
if(head == null){
System.out.println("Underflow...");
}
Node tmp = head;
head = tmp.getNextRef();
if(head == null){
tail = null;
}
System.out.println("Deleted: "+tmp.getValue());
}
//method to implement remove operation after a given value in Singly Linked List
public void deleteAfter(T after){
Node tmp = head;
Node refNode = null;
System.out.println("Searching for all nodes..");
while(true){
if(tmp == null){
break;
}
if(tmp.compareTo(after) == 0){
refNode = tmp;
break;
}
tmp = tmp.getNextRef();
}
if(refNode != null){
tmp = refNode.getNextRef();
refNode.setNextRef(tmp.getNextRef());
if(refNode.getNextRef() == null){
tail = refNode;
}
System.out.println("Deleted: "+tmp.getValue());
} else {
System.out.println("Element not found!!!");
}
}
//method to find the size of the Singly linked list
public long getSize(){
return size;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
