Question

In: Computer Science

Modify the classes so that it accepts integers instead of characters. (If you understand the concept...

Modify the classes so that it accepts integers instead of characters. (If you understand the concept of generic classes, convert the Node and Linked List classes to a generic so that they can be instantiated with either integers or characters)

JAVA Code

class aNode {

char data;
aNode next;

aNode(char mydata) { // Constructor
data = mydata;
next = null;
}
};
//-----------------------------------------------------

class linkedList {

aNode head; // Head of the linked list
int size;

linkedList() { // Constructor
head = null;
size = 0;
}
//-----------------------------------------------------

public void insert_at_beginning(char value) {
aNode newNode = new aNode(value); // create aNew node
newNode.next = head;
head = newNode;
size++;
}
//-----------------------------------------------------

public void insert_at_end(char value) {
aNode newNode = new aNode(value); // create aNew node
if (isEmpty()) {
newNode.next = head;
head = newNode;
size++;
} else {
//find the last node
aNode ptr;
ptr = head;
while (ptr.next != null) {
ptr = ptr.next;
}
ptr.next = newNode; //add the node to the end
size++;
}
}
//-----------------------------------------------------

public void insert_after(char value, char searchValue) {
if (isEmpty()) {
System.out.println("Linked List is empty, no way to insert " + value + " after " + searchValue);
} else {
//find the node with searchValue
aNode ptr;
boolean found = false;
ptr = head;
while (ptr != null && found == false) {
if (ptr.data == searchValue) {
found = true;
} else {
ptr = ptr.next;
}
}
if (ptr == null) {
System.out.println("Did not find " + searchValue + "Nothing Inserted");
} else {
aNode newNode = new aNode(value); // create aNew node
newNode.next = ptr.next;
ptr.next = newNode; //add the node after the searchValue
size++;
}
}
}
//-----------------------------------------------------
// Delete the first node with the value

public void delete(char deleteValue) {
if (isEmpty()) {
System.out.println("Linked List is empty, nothing to delete");
} else {
aNode deletePtr = head; // create a reference to head
if (head.data == deleteValue) {
head = head.next; // remove the head
deletePtr = null; // make the node available for garbage collection.
size--;
} else {
aNode prevPtr;
deletePtr = prevPtr = head;
boolean found = false;
//find the value to be deleted
while (deletePtr != null && found == false) {
if (deletePtr.data == deleteValue) { // Read about the difference between == and .equals()
found = true;
prevPtr.next = deletePtr.next;
deletePtr = null; // make deletePtr available to garbage collection
size--;
} else {
prevPtr = deletePtr;
deletePtr = deletePtr.next;
}
}
if (found == false) {
System.out.println("Not able to find/delete " + deleteValue + " in the Linked List");
}
}
}
}
//-----------------------------------------------------

public boolean isEmpty() {
if (head == null) {
return (true);
} else {
return (false);
}
}
//-----------------------------------------------------

public void print() {
aNode ptr;
ptr = head;
System.out.print("Head--> ");
while (ptr != null) {
System.out.print(ptr.data + " --> ");
ptr = ptr.next;
}
System.out.println("NULL");
}
//-----------------------------------------------------

public int getSize() {
return (size);
}
//-----------------------------------------------------

public void freeAll() {

aNode freePtr = head;
while (head != null) {
head = head.next;
// the next two lines are unnecessary, but are included for
// illustration of how memory is freed up
//
freePtr = null; // make the node available for garbage collector
freePtr = head; // now let the freePtr to the new head
}
head = null;
size = 0;

Solutions

Expert Solution

class Node<T> {

T data;

Node<T> next;

Node(T mydata) { // Constructor

data = mydata;

next = null;

}

};

//-----------------------------------------------------

class LinkedList<T> {

Node<T> head; // Head of the linked list

int size;

LinkedList() { // Constructor

head = null;

size = 0;

}

//-----------------------------------------------------

public void insert_at_beginning(T value) {

Node<T> newNode = new Node<T>(value); // create aNew node

newNode.next = head;

head = newNode;

size++;

}

//-----------------------------------------------------

public void insert_at_end(T value) {

Node<T> newNode = new Node<T>(value); // create aNew node

if (isEmpty()) {

newNode.next = head;

head = newNode;

size++;

} else {

//find the last node

Node<T> ptr;

ptr = head;

while (ptr.next != null) {

ptr = ptr.next;

}

ptr.next = newNode; //add the node to the end

size++;

}

}

//-----------------------------------------------------

public void insert_after(T value, T searchValue) {

if (isEmpty()) {

System.out.println("Linked List is empty, no way to insert " + value + " after " + searchValue);

} else {

//find the node with searchValue

Node<T> ptr;

boolean found = false;

ptr = head;

while (ptr != null && found == false) {

if (ptr.data == searchValue) {

found = true;

} else {

ptr = ptr.next;

}

}

if (ptr == null) {

System.out.println("Did not find " + searchValue + "Nothing Inserted");

} else {

Node<T> newNode = new Node<T>(value); // create aNew node

newNode.next = ptr.next;

ptr.next = newNode; //add the node after the searchValue

size++;

}

}

}

//-----------------------------------------------------

// Delete the first node with the value

public void delete(T deleteValue) {

if (isEmpty()) {

System.out.println("Linked List is empty, nothing to delete");

} else {

Node<T> deletePtr = head; // create a reference to head

if (head.data == deleteValue) {

head = head.next; // remove the head

deletePtr = null; // make the node available for garbage collection.

size--;

} else {

Node<T> prevPtr;

deletePtr = prevPtr = head;

boolean found = false;

//find the value to be deleted

while (deletePtr != null && found == false) {

if (deletePtr.data == deleteValue) { // Read about the difference between == and .equals()

found = true;

prevPtr.next = deletePtr.next;

deletePtr = null; // make deletePtr available to garbage collection

size--;

} else {

prevPtr = deletePtr;

deletePtr = deletePtr.next;

}

}

if (found == false) {

System.out.println("Not able to find/delete " + deleteValue + " in the Linked List");

}

}

}

}

public boolean isEmpty() {

if (head == null) {

return (true);

} else {

return (false);

}

}

public void print() {

Node<T> ptr;

ptr = head;

System.out.print("Head--> ");

while (ptr != null) {

System.out.print(ptr.data + " --> ");

ptr = ptr.next;

}

System.out.println("NULL");

}

public int getSize() {

return (size);

}

public void freeAll() {

Node<T> freePtr = head;

while (head != null) {

head = head.next;

freePtr = null; // make the node available for garbage collector

freePtr = head; // now let the freePtr to the new head

}

head = null;

size = 0;

}

};

class LinkedListMain {

public static void main(String args[]) {

LinkedList<Integer> l = new LinkedList<Integer>();

l.insert_at_beginning(1);

l.insert_at_beginning(2);

l.insert_after(3, 1);

l.print();

LinkedList<Character> l1 = new LinkedList<Character>();

l1.insert_at_beginning('a');

l1.insert_at_beginning('b');

l1.insert_after('c', 'a');

l1.print();

}

}


Related Solutions

Modify the following code to use 'envp' instead of 'environ'. Be sure that you understand how...
Modify the following code to use 'envp' instead of 'environ'. Be sure that you understand how the code works. Provide liberal comments to explain what your pointer arithmetic is computing. Also, answer the following questions and explain your answers: i) WHERE in process memory is it most likely that each of the values exist at run time? ii) WHERE in process memory is it most likely the actual strings containing environment variables are stored? #include #include extern char **environ; //...
Please explain to me the answers so that I can understand the concept. Thank you! You...
Please explain to me the answers so that I can understand the concept. Thank you! You must evaluate a proposal to buy a new milling machine. The purchase price of the milling machine, including shipping and installation costs, is $126,000, and the equipment will be fully depreciated at the time of purchase. The machine would be sold after 3 years for $85,000. The machine would require a $3,500 increase in net operating working capital (increased inventory less increased accounts payable)....
CS 206 Visual Programming, Netbeans Understand and modify the attached GCDMethod.java so it can find the...
CS 206 Visual Programming, Netbeans Understand and modify the attached GCDMethod.java so it can find the greatest common divisor of three numbers input by the user.  Hints: modify the gcd() method by adding the third parameter. import java.util.Scanner; public class GCDMethod { /** Main method */ public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Prompt the user to enter two integers System.out.print("Enter first integer: "); int n1 = input.nextInt(); System.out.print("Enter second integer: ");...
Exercises 4. Using the IDE make a new project/program by changing SmallIO.java so that integers instead...
Exercises 4. Using the IDE make a new project/program by changing SmallIO.java so that integers instead of Strings are entered by the user. 5. Change the program so that it only loops until the number -999 is entered by the user. 6. Modify the last program so that it (a) keeps and displays a running total and the average so far of the entered numbers (b) displays the largest and the smallest numbers so far of the entered numbers. NOTE:...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
what do you understand about concept? what are different types of concept. what kind of concept...
what do you understand about concept? what are different types of concept. what kind of concept do you see most in nursing practice and research
How do you understand the concept of an activated complex? What is it?
How do you understand the concept of an activated complex? What is it?
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
How would you explain the concept of QALY? When is it appropriate to use QALYs instead...
How would you explain the concept of QALY? When is it appropriate to use QALYs instead Of simply improved life expectancy as the outcome measure in economic evaluation?
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT