// 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 write methods to do the following:
class MyLinkedList {
Node head;
// Insert a node at the end of the list
void insertLast(Integer data) {
Node newNode = new Node(data, null);
if (head == null) {
head = newNode;
return;
}
Node last = head;
while (last.link != null) {
last = last.link;
}
last.link = newNode;
}
// Insert a node at the front of the list
void insertFirst(Integer data) {
Node newNode = new Node(data, null);
if (head == null) {
head = newNode;
return;
}
newNode.link = head;
head = newNode;
}
// Display all nodes in the list
void display() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.link;
}
System.out.println();
}
// Method to check of the list is empty - isEmpty() (returns a Boolean)
Boolean isEmpty() {
return head == null;
}
// Method to return the size of the list
int size() {
int size = 0;
Node temp = head;
while (temp != null) {
size++;
temp = temp.link;
}
return size;
}
// Delete all nodes in the list
void deleteAllNodes() {
head = null;
}
// Search List for a node with a certain value (returns the node if found, null if not found)
Node search(Integer value) {
Node temp = head;
while (temp != null) {
if (temp.data.equals(value))
return temp;
temp = temp.link;
}
return null;
}
// Delete a node with a specific value in the list if found
void deleteIfFound(Integer value) {
Node curr = head, prev = head;
if (head.data.equals(value)) {
head = head.link;
return;
}
while (curr != null) {
if (curr.data.equals(value)) {
prev.link = curr.link;
}
prev = curr;
curr = curr.link;
}
}
// Method to find the node with the largest value in the list – getLargest()
void getLargest() {
Node temp = head, largest = head;
while (temp != null) {
if (temp.data.compareTo(largest.data) > 0)
largest = temp;
temp = temp.link;
}
System.out.println("Largest Value:" + largest.data);
}
// Method to return all the elements less than a certain value as a linked list of type MyLinkedList
MyLinkedList findLessThan(Integer x) {
MyLinkedList newList = new MyLinkedList();
Node temp = head;
while (temp != null) {
if (temp.data.compareTo(x) < 0) {
newList.insertLast(temp.data);
}
temp = temp.link;
}
return newList;
}
}
// Main Method to test all Linked list methods
public class NodeTest {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.display();
myLinkedList.insertLast(10);
myLinkedList.insertLast(15);
myLinkedList.insertLast(12);
System.out.println("list size: " + myLinkedList.size());
myLinkedList.display();
myLinkedList.insertFirst(53);
myLinkedList.insertLast(62);
myLinkedList.insertFirst(82);
myLinkedList.insertLast(102);
myLinkedList.insertLast(14);
System.out.println("list size: " + myLinkedList.size());
myLinkedList.display();
System.out.println("20 " + (myLinkedList.search(20) == null ? "Not Found" : "Found"));
System.out.println("15 " + (myLinkedList.search(62) == null ? "Not Found" : "Found"));
System.out.println("Delete 10 if found");
myLinkedList.deleteIfFound(10);
myLinkedList.display();
myLinkedList.getLargest();
System.out.println("Get New List less than 50");
MyLinkedList newList = myLinkedList.findLessThan(50);
System.out.println("Printing New List:");
newList.display();
System.out.println("Deleting all nodes");
myLinkedList.deleteAllNodes();
myLinkedList.display();
}
}
based on the pervious, please do the following completely or don't since it is one question with several parts, language used is java on netbeans.
Problem 2:
Use MyLinkedList in previous problem to create a TestIntegers method. TestIntegers method is to be called it in the main method to do the following:
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 Main {
public static void TestIntegers()
{
MyLinkedList
myLinkedList = new MyLinkedList();
myLinkedList.insertLast(10);
myLinkedList.insertLast(15);
myLinkedList.insertLast(12);
myLinkedList.insertLast(1);
myLinkedList.insertLast(25);
System.out.println("List:");
myLinkedList.display();
System.out.println("Adding New Emement(53) in front of
list");
myLinkedList.insertFirst(53);
System.out.println("list
size: " + myLinkedList.size());
myLinkedList.getLargest();
System.out.println("" +
(myLinkedList.search(20) == null ? "False" : "True"));
System.out.println("" +
(myLinkedList.search(10) == null ? "False" : "True"));
System.out.println("Elements less than 20");
MyLinkedList newList =
myLinkedList.findLessThan(20);
newList.display();
myLinkedList.display();
System.out.println("Delete 53(first node) if found");
myLinkedList.deleteIfFound(53);
myLinkedList.display();
System.out.println("Delete 12(middle node) if found");
myLinkedList.deleteIfFound(12);
myLinkedList.display();
System.out.println("Delete 25(end node) if found");
myLinkedList.deleteIfFound(25);
myLinkedList.display();
System.out.println("Deleting all nodes");
myLinkedList.deleteAllNodes();
myLinkedList.display();
}
public static void main(String[] args) {
TestIntegers();
// MyLinkedList
myLinkedList = new MyLinkedList();
//
myLinkedList.display();
//
myLinkedList.insertLast(10);
//
myLinkedList.insertLast(15);
//
myLinkedList.insertLast(12);
//
System.out.println("list size: " + myLinkedList.size());
//
myLinkedList.display();
//
myLinkedList.insertFirst(53);
//
myLinkedList.insertLast(62);
//
myLinkedList.insertFirst(82);
//
myLinkedList.insertLast(102);
//
myLinkedList.insertLast(14);
//
System.out.println("list size: " + myLinkedList.size());
//
myLinkedList.display();
//
System.out.println("20 " + (myLinkedList.search(20) == null ? "Not
Found" : "Found"));
//
System.out.println("15 " + (myLinkedList.search(62) == null ? "Not
Found" : "Found"));
//
System.out.println("Delete 10 if found");
//
myLinkedList.deleteIfFound(10);
//
myLinkedList.display();
// myLinkedList.getLargest();
//
System.out.println("Get New List less than 50");
// MyLinkedList newList
= myLinkedList.findLessThan(50);
//
System.out.println("Printing New List:");
//
newList.display();
//
System.out.println("Deleting all nodes");
//
myLinkedList.deleteAllNodes();
//
myLinkedList.display();
}
}
// 1. Add methods to get and set, Data and Link. Data should be any Comparable...
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 =...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
I have a Java Data Structures project and we're creating a linked list and an iterator for that linked list. The add method for the linked list has to be O(1). This is my code public class MyLinkedList extends CS20bLinkedList implements Iterable<Integer> { @Override public void add(int value){ Node tail = head; if(head == null){ head = new Node(value); //System.out.println("Very Fun! "+head.value); return; }else{ ...
Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
**HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...
Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node { int data; node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() { struct node*head=NULL; char commands; int value; ...
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;...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /* Name: Rabia Saad Al-wethnani ID: 43503535 Section: 2486 */ using namespace std; struct Node { int item; //data Node *next; //pointer to the next node in the list }; Node* createNode(int); int isPresent(Node*, int); void appendNode(Node*&, int); void displayList(Node*); void printLists(Node*, Node*); void swapNodes(Node* &head, int s); void findIntersection(Node* first, Node* P); void findUnion(Node* first, Node* P); int main() { Node* L = NULL,...