JAVA- Complete the code by following guidelines in comments.
class CircularList
{
private Link current;
private Link prev;
public CircularList() {
// implement: set both current and prev to null
}
public boolean isEmpty() {
// implement
return true;
}
public void insert(int id) {
// implement: insert the new node behind the current node
}
public Link delete() {
// implement: delete the node referred by current
return null;
}
public Link delete(int id) {
// implement: delete the node with value id
// if no node with the id exists, return null
return null;
}
public void displayList() {
// implement: print all the list element values once, each value
seperated by comma
}
}
class Link
{
private int id;
private Link next;
public Link(int id) {
// implement
}
public String toString() {
return String.valueOf(id);
}
}
public class Lab5
{
public static void main(String[] args) {
CircularList theList = new CircularList();
theList.insert(10);
theList.insert(20);
theList.insert(15);
theList.insert(5);
theList.insert(30);
theList.displayList();
System.out.println(theList.delete());
theList.delete(15);
theList.displayList();
while (!theList.isEmpty()) {
Link aLink = theList.delete();
System.out.println("Deleted: " + aLink);
}
if (!theList.isEmpty())
System.out.println("Program error");
else
System.out.println("Program success");
}
}
Below I have copied your code and completed it as per the guidelines-
class CircularList
{
private Link current;
private Link prev;
public CircularList() {
current=null;
prev=null;
// implement: set both current and prev to null
}
public boolean isEmpty() {
return(current==null);
// implement
}
public void insert(int id)
{
Link newlink = newlink(id);
if(current == null)
{
current = prev = newLink;
}
else
{
current.previous = newlink;
}
prev = newLink;
newLink.next = current
// implement: insert the new node behind the current node
}
public Link delete()
{
Link temp = current;
if(current.next == null)
{
current = null;
return;
}
else
{
current.id = current.next.id;
temp = current.next;
temp = null;
current.next = current.next.next;
// implement: delete the node referred by current
}
public Link delete(int id)
{
if( current.id == id)
{
current.previous = current;
current.previous.previous = prev;
}
else
{
while(current.id != id)
{
current = current.previous;
if( current == null)
return null; // not found
}
if ( current == prev )
{
current=prev.next;
prev = prev.previous;
}
// implement: delete the node with value id
// if no node with the id exists, return null
return null;
}
public void displayList()
{
System.out.println("Values in the list are")'
while(prev != null)
{
System.out.println(current.id +" , ");
current=current.previous;
prev=prev.previous;
}
// implement: print all the list element values once, each value
seperated by comma
}
class Link
{
private int id;
private Link next;
public Link(int id1)
{
id1=id
}
// implement
}
public String toString() {
return String.valueOf(id);
}
}
public class Lab5
{
public static void main(String[] args) {
CircularList theList = new CircularList();
theList.insert(10);
theList.insert(20);
theList.insert(15);
theList.insert(5);
theList.insert(30);
theList.displayList();
System.out.println(theList.delete());
theList.delete(15);
theList.displayList();
while (!theList.isEmpty()) {
Link aLink = theList.delete();
System.out.println("Deleted: " + aLink);
}
if (!theList.isEmpty())
System.out.println("Program error");
else
System.out.println("Program success");
}
}
JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...
JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
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 =...
// 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...
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);...
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...
Modify listlink.java program (non generic) by adding the following methods: public void insertsorted(x); // Inert x in a sorted list. public void deletex(x); //Search for x in the sorted list, if found, delete it from the sorted list. Assume you have a data file p2.txt with the following contents: 8 4 15 23 12 36 5 36 42 3 5 14 4 and your java program is in xxxxx.java file, where xxxxx is the first 5 characters of your last...