Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

Students also viewed these Databases questions