Question

JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...

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");

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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");

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    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...

  • In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as...

    In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as follows: In the case in which the deleted node has two children, write the code in two ways. a) always replace the deleted node with the largest node on the left. b) count how many deletions have been done, and for even deletions replace the deleted node with the largest node on the left, for odd deletions, replace the deleted node with the smallest...

  • public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...

    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; } }...

  • Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException;...

    Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{    BSTTreeNode left, right;    String data; public BSTTreeNode(){    left = null;    right = null;    data = null; } public BSTTreeNode(String n){    left = null;    right = null;    data = n; } public void setLeft(BSTTreeNode n){    left = n; } public void setRight(BSTTreeNode n){   ...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    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...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    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...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    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...

    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,...

    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...

    // 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT