could somone please help me to complete this !
public class MyLinkedList<AnyType> {
private Node<AnyType> head, tail;
private int size;
public MyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
//1.Insert a node at the end of the list
public void insert(AnyType data) {
Node<AnyType> newNode = new Node();
newNode.data = data;
if (head == null) {
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
//2.Insert a node at the front of the list
public void insertFront(AnyType data) {
Node<AnyType> newNode = new Node();
newNode.data = data;
if (head == null) {
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
} else {
newNode.next = head;
head = newNode;
}
size++;
}
//3.Display all nodes in the list
public void display() {
if (head == null) {
System.out.println("Empty List!");
} else {
Node newNode = head;
for (int i = 0; i < size; i++) {
System.out.print(newNode.data.toString() + " ");
newNode = newNode.next;
}
System.out.println("");
}
}
//4.Method to check of the list is empty - isEmpty() (returns a
Boolean)
public boolean isEmpty() {
return (size == 0);
//return(teal==null);
}
//5.Method to return the size of the list
public int getSize(MyLinkedList integerList) {
Node<AnyType> Pointer = new Node();
Pointer = head;
int counter = 0;
while (Pointer != null) {
counter++;
Pointer = Pointer.next;
}
//return counter;
return size;
}
//6.Delete all nodes in the list
public void Clear() {
head = null;
tail = null;
size = 0;
}
//7.Search List for a node with a certain value (returns the
node if found, null if not found)
public Node<AnyType> serach(AnyType value) {
Node<AnyType> Pointer = new Node();
Pointer = head;
for (int i = 0; i < size; i++) {
if (Pointer.data == value) {
return Pointer;
}
Pointer = Pointer.next;
}
//
Pointer = null;
return Pointer;
}
//8.Delete a node with a specific value in the list if found
public void delete(AnyType value) {
Node<AnyType> P1 = new Node();
Node<AnyType> P2 = P1.next;
//
if (isEmpty())
System.out.println("its Empty ! ");
else if (value.equals(head.data)) {
head = head.next;
size--;
} else {
Node<AnyType> p1 = head;
Node<AnyType> p2 = p1.next;
while (p2 != null) {
if (value.equals(p2.data)) {
p1.next = p2.next;
if (p2.next == null) {
tail = p1;
}
}
}
}
}
//9.Method to find the node with the largest value in the list –
getLargest()
public Node<AnyType> getLargest() {
Node<AnyType> Pointer = head;
Node<AnyType> largest = head;
for (int i = 0; i < size; i++) {
int pData = Integer.parseInt((Pointer.data.toString()));
int lData = Integer.parseInt((Pointer.data.toString()));
if (pData < lData) {
largest = Pointer;
}
Pointer = Pointer.next;
}
return largest;
}
//10.Method to return all the elements less than a certain value
as a linked list of type MyLinkedList
public MyLinkedList<AnyType> getListLessThan(AnyType Value)
{
MyLinkedList<AnyType> result = new MyLinkedList();
Node<AnyType> Pointer = head;
for (int i = 0; i < size; i++) {
int pData = Integer.parseInt((Pointer.data.toString()));
int valueInt = Integer.parseInt((Value.toString()));
if (pData < valueInt) {
result.insert(Pointer.data);
}
//
}
return result;
}
thats was my code please help me thank you
Use MyLinkedList in previous problem to create a TestIntegers method and TestStrings:
TestStrings:
Create a new List (call it stringList) of type MyLinkedList
Insert five strings to the end of the list (not ordered). Display
all the items in the List
Insert elements at the front of the List and display it Display the
size of the list
Display the largest element in the list
Search for a particular element in the List by printing true if
found or false.
Get and display all elements less than the String “Khaled”
Delete a node in the List and display the list (in the beginning,
middle and at the end) Delete the entire List and display if the
list is empty or not
for the test integer
Create a new List (call it integerList) of type MyLinkedList
Insert five integer numbers to the end of the list (not ordered). Wrap the numbers in the class
“Integer”
Display all the items in the List
Insert elements at the front of the List and display it
Display the size of the list
Display the largest element in the list
Search for a particular element in the List by printing true if found or false.
Get and display all elements less than the Integer 20
Delete a specific node in the List and display the list (in the beginning, middle and at the end)
Delete the entire List and display if the list is empty or not
public class MyLinkedList<AnyType extends
Comparable<AnyType>> {
private Node<AnyType> head, tail;
private int size;
public MyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
//1.Insert a node at the end of the list
public void insert(AnyType data) {
Node<AnyType> newNode = new Node();
newNode.data = data;
if (head == null) {
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
//2.Insert a node at the front of the list
public void insertFront(AnyType data) {
Node<AnyType> newNode = new Node();
newNode.data = data;
if (head == null) {
head = newNode;
tail = newNode;
head.next = null;
tail.next = null;
} else {
newNode.next = head;
head = newNode;
}
size++;
}
//3.Display all nodes in the list
public void display() {
if (head == null) {
System.out.println("Empty List!");
} else {
Node newNode = head;
for (int i = 0; i < size; i++) {
System.out.print(newNode.data.toString() + " ");
newNode = newNode.next;
}
System.out.println("");
}
}
//4.Method to check of the list is empty - isEmpty()
(returns a Boolean)
public boolean isEmpty() {
return (size == 0);
//return(teal==null);
}
//5.Method to return the size of the list
public int getSize() {
Node<AnyType> Pointer = new Node();
Pointer = head;
int counter = 0;
while (Pointer != null) {
counter++;
Pointer = Pointer.next;
}
//return counter;
return size;
}
//6.Delete all nodes in the list
public void Clear() {
head = null;
tail = null;
size = 0;
}
//7.Search List for a node with a certain value
(returns the node if found, null if not found)
public Node<AnyType> search(AnyType value)
{
Node<AnyType> Pointer = new Node();
Pointer = head;
for (int i = 0; i < size; i++) {
if (Pointer.data == value) {
return Pointer;
}
Pointer = Pointer.next;
}
//
Pointer = null;
return Pointer;
}
//8.Delete a node with a specific value in the list if
found
public void delete(AnyType value) {
if(!isEmpty())
{
Node<AnyType> curr = head;
Node<AnyType> prev = null;
while(curr !=
null)
{
if(curr.data.equals(value))
{
if(prev == null)
head =
curr.next;
else
prev.next
= curr.next;
size--;
return ;
}
prev = curr;
curr = curr.next;
}
}
}
//9.Method to find the node with the largest value
in the list – getLargest()
public Node<AnyType> getLargest() {
Node<AnyType> Pointer = head;
Node<AnyType> largest = head;
if(isEmpty())
return
null;
while(Pointer != null)
{
if(Pointer.data.compareTo(largest.data) > 0)
largest = Pointer;
Pointer =
Pointer.next;
}
return largest;
}
//10.Method to return all the elements less than a
certain value as a linked list of type MyLinkedList
public MyLinkedList<AnyType>
getListLessThan(AnyType Value) {
MyLinkedList<AnyType>
result = new MyLinkedList<AnyType>();
Node<AnyType> Pointer =
head;
while(Pointer != null)
{
if(Pointer.data.compareTo(Value) < 0)
result.insert(Pointer.data);
Pointer =
Pointer.next;
}
return result;
}
public static void TestStrings()
{
MyLinkedList<String>
stringList = new MyLinkedList<String>();
stringList.insert("One");
stringList.insert("Two");
stringList.insert("Three");
stringList.insert("Four");
stringList.insert("Five");
System.out.println("String List :
");
stringList.display();
stringList.insertFront("Six");
stringList.insertFront("Seven");
stringList.insertFront("Eight");
stringList.insertFront("Nine");
stringList.insertFront("Ten");
System.out.println("String List :
");
stringList.display();
System.out.println("Size of String
List : "+stringList.getSize());
Node<String> largestNode =
stringList.getLargest();
if(largestNode != null)
System.out.println("Largest of String List :
"+largestNode.data);
else
System.out.println("Empty list");
Node<String> node =
stringList.search("Six");
if(node != null)
System.out.println("Six found");
else
System.out.println("Six not found");
MyLinkedList<String> list =
stringList.getListLessThan("Khaled");
System.out.println("Elements less
than the String Khaled: ");
list.display();
stringList.delete("Ten");
stringList.delete("Five");
stringList.delete("Three");
System.out.println("String List
after deletion : ");
stringList.display();
stringList.Clear();
if(stringList.isEmpty())
System.out.println("String List is empty");
else
System.out.println("String List is not empty");
}
public static void TestIntegers()
{
MyLinkedList<Integer>
integerList = new MyLinkedList<Integer>();
integerList.insert(new
Integer(5));
integerList.insert(new
Integer(3));
integerList.insert(new
Integer(10));
integerList.insert(new
Integer(128));
integerList.insert(new
Integer(67));
System.out.println("Integer List :
");
integerList.display();
integerList.insertFront(new
Integer(60));
integerList.insertFront(new
Integer(7));
System.out.println("Integer List :
");
integerList.display();
System.out.println("Size of Integer
List : "+integerList.getSize());
Node<Integer> largestNode
= integerList.getLargest();
if(largestNode != null)
System.out.println("Largest of String List :
"+largestNode.data);
else
System.out.println("Empty list");
Node<Integer> node =
integerList.search(56);
if(node != null)
System.out.println("56 found");
else
System.out.println("56 not found");
MyLinkedList<Integer> list =
integerList.getListLessThan(20);
System.out.println("Elements less
than the 20: ");
list.display();
integerList.delete(7);
integerList.delete(10);
integerList.delete(67);
System.out.println("Integer List
after deletion : ");
integerList.display();
integerList.Clear();
if(integerList.isEmpty())
System.out.println("Integer List is empty");
else
System.out.println("Integer List is not empty");
}
public static void main(String[] args)
{
System.out.println("Test Strings :
");
TestStrings();
System.out.println("\nTest
Integers");
TestIntegers();
}
}
class Node<AnyType>
{
AnyType data;
Node<AnyType> next;
}
//end of program
Output:
could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...
// 1. Add methods to get and set, Data and Link. Data should be any Comparable object. class Node { Integer data; // Integer is Comparable Node link; public Node(Integer data, Node link) { this.data = data; this.link = link; } public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } // b. Create MyLinkedList class and...
Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
1. private Node head; private Node tail; public class Node { String name; Node next; Node prev; } public void displayInReverse(){ //write your code here to display names in reverse } 2. public class NodeOne { String name; NodeOne next; public NodeOne(String name) { this.name = name; } } // Complete the code snippet below so that the delete method works properly public void delete(String name) { NodeOne temp = head, prev = head; while (temp != null) { if...
LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...