Question

In: Computer Science

Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...

Java program to implement circular linked list.

public class CircularLinkedList  {

    
    private Node tail;
    private int size;
    
    public CircularLinkedList() {
        tail= null;
        size = 0;

    }



     public int size(){
        return size;
    }

 
    public boolean isEmpty()
    {
        return size==0;
    }
    
        
        //if list is not empty return the first element
     
    public E first() {
        if (isEmpty()) return null;
        //code here
        return 0;
    }
  

    
        
        //if list not empty return last element
     
    public E last() {
        if (isEmpty()) return null;
        return tail.getElement();
    }

    /*
        move tail to the next node
     */
    public void rotate()
    {
    }

    /*
        add element to the first of the linked list
        increase the size
     */
    public void addFirst(E e)
    {
        
    }

    /*
        add element to the end of the linked list
        increase size
     */
    public void addLast(E e)
    {
    }

    /*
        take out the first element
        decrease the size
        return first element or null


     */
    public E removeFirst()
    {
        return null;
    }

------------------------------------------------------------------------------------------------------

implements

public class Node {
    // Instance variables:
    //element = data
    private E element;
    private Node next;
    /** Creates a node with null references to its element and next node. */
    public Node() {
        this(null, null);
    }
    /** Creates a node with the given element and next node. */
    public Node(E e, Node n) {
        element = e;
        next = n;
    }
    // Accessor methods:
    public E getElement() {
        return element;
    }
    public Node getNext() {
        return next;
    }
    // Modifier methods:
    public void setElement(E newElem) {
        element = newElem;
    }
    public void setNext(Node newNext) {
        next = newNext;
    }
}

Solutions

Expert Solution

public class CircularLinkedList {

public int size =0;
public Node head=null;
public Node tail=null;

//add a new node at the start of the linked list
public void addNodeAtStart(int data){
System.out.println("Adding node " + data + " at start");
Node n = new Node(data);
if(size==0){
head = n;
tail = n;
n.next = head;
}else{
Node temp = head;
n.next = temp;
head = n;
tail.next = head;
}
size++;
}

public void addNodeAtEnd(int data){
if(size==0){
addNodeAtStart(data);
}else{
Node n = new Node(data);
tail.next =n;
tail=n;
tail.next = head;
size++;
}
System.out.println("\nNode " + data + " is added at the end of the list");
}

public void deleteNodeFromStart(){
if(size==0){
System.out.println("\nList is Empty");
}else{
System.out.println("\ndeleting node " + head.data + " from start");
head = head.next;
tail.next=head;
size--;
}
}

public int elementAt(int index){
if(index>size){
return -1;
}
Node n = head;
while(index-1!=0){
n=n.next;
index--;
}
return n.data;
}

//print the linked list
public void print(){
System.out.print("Circular Linked List:");
Node temp = head;
if(size<=0){
System.out.print("List is empty");
}else{
do {
System.out.print(" " + temp.data);
temp = temp.next;
}
while(temp!=head);
}
System.out.println();
}

//get Size
public int getSize(){
return size;
}

public static void main(String[] args) {
CircularLinkedList c = new CircularLinkedList();
c.addNodeAtStart(3);
c.addNodeAtStart(2);
c.addNodeAtStart(1);
c.print();
c.deleteNodeFromStart();
c.print();
c.addNodeAtEnd(4);
c.print();
System.out.println("Size of linked list: "+ c.getSize());
System.out.println("Element at 2nd position: "+ c.elementAt(2));
}

}

class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}


Related Solutions

JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template......
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template... public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last()...
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private...
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */    private Node head;    private Node tail;       public class Node { // declare member variables (data and next)    Integer data;    Node next; // finish these constructors    public Node(int data, Node next) {               this.data=data;        this.next=next;    }...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT