Question

In: Computer Science

Java Language: Using program created in this lesson as a starting point, create a circular list...

Java Language:

Using program created in this lesson as a starting point, create a circular list with at least four nodes. A circular list is one where the last node is made to point to the first. Show that your list has a circular structure by printing its content using an iterative structure such as a while. This should cause your program to go into an infinite loop so you should include a conditional that limits the number of nodes to be printed to a specific value, for example 20.

Code:

class Node {

int value;

Node nextNode;

Node(int v, Node n)

{

value = v;

nextNode = n;

}

Node (int v)

{

this(v,null);

}

}


class Stack {

protected Node top;

Stack()

{

top = null;

}

boolean isEmpty()

{

return( top == null);

}

void push(int v)

{

Node tempPointer;

tempPointer = new Node(v);

tempPointer.nextNode = top;

top = tempPointer;

}

int pop()

{

int tempValue;

tempValue = top.value;

top = top.nextNode;

return tempValue;

}

void printStack()

{

Node aPointer = top;

String tempString = "";

while (aPointer != null)

{

tempString = tempString + aPointer.value + "\n";

aPointer = aPointer.nextNode;



System.out.println(tempString);

}

}

public class StackWithLinkedList{

public static void main(String[] args){

int popValue;

Stack myStack = new Stack();

myStack.push(5);

myStack.push(7);

myStack.push(9);

myStack.printStack();

popValue = myStack.pop();

popValue = myStack.pop();

myStack.printStack();

}

}

Solutions

Expert Solution

Node.java :

class Node {
      int value;
      Node nextNode;
      Node (int v){
          this.value=v;
      } 
}
-----------------------------

CLLOperations.java :

class CLLOperations{
          Node head=null,tail=null;
          //adding new node to CLL
      public void addNewNode(int value){
          Node newNode = new Node(value);
          if(this.head==null){
                  this.head=newNode;
                  this.tail=newNode;
                  newNode.nextNode=head;
          }else{
                  this.tail.nextNode=newNode;
                  this.tail= newNode;
                  this.tail.nextNode=head;                
          }
          System.out.println("Node "+value+"is added succesfully");
      }     
      //Displaying all nodes until count is 20 as per given question
      public void display(){
          Node current = this.head;
          if(this.head==null){
                  System.out.println("list is empty");
          }else{
                  for(int i=0;i<20;i++){
                          System.out.println(current.value);
                          current=current.nextNode;
          }
        }         
     }
}   
-----------------------------
CircularLinkedList.java

public class CircularLinkedList{
        public static void main(String[] args){
                CLLOperations cll = new CLLOperations();
                cll.addNewNode(10);
                cll.addNewNode(15);
                cll.addNewNode(11);
                cll.addNewNode(21);
                cll.display();
    }
}

The output of the program is as below:


Related Solutions

write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
Create a java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT