Question

Sometimes it is convenient to maintain references to both the next node and the previous node...

Sometimes it is convenient to maintain references to both the next node and the previous node in a linked list.
This is called a doubly linked list and is illustrated in Figure 13.4 of the text. File DoubleLinked contains
definitions for a doubly linked list of integers. This class contains an inner class IntNode that holds information
for a single node in the list (its value and references to the next and previous nodes). The DoubleLinked class
also contains the following methods:


     public DoubleLinked()—constructor; creates an empty list of integers
     public void addToFront(int val)—takes an integer and puts it on the front of the list
     public void print()—prints the elements in the list from first to last
File DoubleLinkedTest contains a driver that allows you to experiment with these methods. Save both of
these files to your directory, compile and run DoubleLinkedTest, and play around with it to see how it works.
Then add the following methods to the DoubleLinked class. For each, add an option to the driver to test it.
1. public void addToEnd(int val)—takes an integer and puts it on the end of the list
2. public void removeFirst()—removes the first value from the list. If the list is empty, does nothing.
3. public void removeLast()—removes the last element of the list. If the list is empty, does nothing.
4. public void remove(int oldVal)—removes the first occurrence of oldVal in the list.

Deliverables:

DoubleLinked.java

DoubleLinkedTest.java

// ***************************************************************
// DoubleLinked.java
//
// A class using a doubly linked list to represent a list of integers.
//
// ***************************************************************
public class DoubleLinked
{
private IntNode list;
// ----------------------------------------------
// Constructor -- initializes list
// ----------------------------------------------
   public DoubleLinked()
   {
       list = null;
   }
// ----------------------------------------------
// Prints the list elements
// ----------------------------------------------
   public void print()
   {
       for (IntNode temp = list; temp != null; temp = temp.next)
       System.out.println(temp.val);
   }
// ----------------------------------------------
// Adds new element to front of list
// ----------------------------------------------
   public void addToFront(int val)
   {
       IntNode newNode = new IntNode(val);
       newNode.next = list;
       if (list != null)
       list.prev = newNode;
       list = newNode;
   }

//***************************************************************
// An inner class that represents a list element.
//***************************************************************
   private class IntNode
   {
       public int val;
       public IntNode next;
       public IntNode prev;
       public IntNode(int val)
       {
           this.val = val;
           this.next = null;
           this.prev = null;
       }
   }
}

// ***********************************************************
// DoubleLinkedTest.java
//
// Driver to test DoubleLinked methods.
// ***********************************************************
import java.util.Scanner;
public class DoubleLinkedTest
{
   private static Scanner scan;
   private static DoubleLinked list = new DoubleLinked();
   //----------------------------------------------------------------
   // Creates a list, then repeatedly prints the menu and does what
   // the user asks until they quit.
   //----------------------------------------------------------------
   public static void main(String[] args)
   {
       scan = new Scanner(System.in);
       printMenu();
       int choice = scan.nextInt();
       while (choice != 0)
       {
           dispatch(choice);
           printMenu();
           choice = scan.nextInt();
       }
   }
   //---------------------------------------
   // Does what the menu item calls for.
   //---------------------------------------
   public static void dispatch(int choice)
   {
       int newVal;
       switch(choice)
       {
           case 0:
           System.out.println("Bye!");
           break;
           case 1: //print
           System.out.println("** List elements **");
           list.print();
           break;
           case 2: //add to front
           System.out.println("Enter integer to add to front");
           newVal = scan.nextInt();
           list.addToFront(newVal);
           break;
           default:
           System.out.println("Sorry, invalid choice");
       }
   }
   //-----------------------------------------
   // Prints the user's choices
   //-----------------------------------------
   public static void printMenu()
   {
       System.out.println("\n Menu ");
       System.out.println(" ====");
       System.out.println("0: Quit");
       System.out.println("1: Print list");
       System.out.println("2: Add an integer to the front of the list");
       System.out.print("\nEnter your choice: ");
   }
}

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

DoubleLinked.java


//***************************************************************
//DoubleLinked.java
//
//A class using a doubly linked list to represent a list of integers.
//
//***************************************************************
public class DoubleLinked
{
private IntNode list;
//----------------------------------------------
//Constructor -- initializes list
//----------------------------------------------
   public DoubleLinked()
   {
       list = null;
   }
//----------------------------------------------
//Prints the list elements
//----------------------------------------------
   public void print()
   {
       for (IntNode temp = list; temp != null; temp = temp.next)
       System.out.println(temp.val);
   }
//----------------------------------------------
//Adds new element to front of list
//----------------------------------------------
   public void addToFront(int val)
   {
       IntNode newNode = new IntNode(val);
       newNode.next = list;
       if (list != null)
       list.prev = newNode;
       list = newNode;
   }

//***************************************************************
//An inner class that represents a list element.
//***************************************************************
   private class IntNode
   {
       public int val;
       public IntNode next;
       public IntNode prev;
       public IntNode(int val)
       {
           this.val = val;
           this.next = null;
           this.prev = null;
       }
   }
   public void addToEnd(int val)
   {
       IntNode newnode = new IntNode(val);
       //if list is empty, this will be the only node in it
       if (list == null)
           list = newnode;
      
       else
       {
           //make temp point to last thing in list
           IntNode temp = list;
           while (temp.next != null)
           temp = temp.next;
           //link new node into list
           temp.next = newnode;
           newnode.prev=temp;
       }
   }
   public void removeFirst()
   {
       if (list.next==null)
       {
           list=null;
       }
       if (list != null)
       {
       list = list.next;
       list.prev=null;
       }
   }
   public void removeLast()
   {
       if(list!=null)
       {
           IntNode temp= list;
           IntNode trailNode= null;
           while(temp.next!=null)
           {
               trailNode=temp;
               temp=temp.next;
           }
           if(trailNode !=null)
               trailNode.next=null;
               else
               {
                   list.prev=null;
                   list=null;
               }
       }
      
   }
   public void remove(int oldVal)
   {
       IntNode temp= list;
       IntNode prevNode= null;
       while(temp!=null && temp.val!=oldVal)
       {
           temp= temp.next;
       }
       if(temp !=null)
       {
           if(temp.prev!=null)
           {
               temp.prev.next=temp.next;
           }
       }
       else
       {
           list=temp.next;
       }
       if(temp.next!=null)
       {
           temp.next.prev= temp.prev;
       }
   }
}

DoubleLinkedTest.java

//***********************************************************
//DoubleLinkedTest.java
//
//Driver to test DoubleLinked methods.
//***********************************************************
import java.util.Scanner;
public class DoubleLinkedTest
{
   private static Scanner scan;
   private static DoubleLinked list = new DoubleLinked();
   //----------------------------------------------------------------
   // Creates a list, then repeatedly prints the menu and does what
   // the user asks until they quit.
   //----------------------------------------------------------------
   public static void main(String[] args)
   {
       scan = new Scanner(System.in);
       printMenu();
       int choice = scan.nextInt();
       while (choice != 0)
       {
           dispatch(choice);
           printMenu();
           choice = scan.nextInt();
       }
   }
   //---------------------------------------
   // Does what the menu item calls for.
   //---------------------------------------
   public static void dispatch(int choice)
   {
       int newVal;
       switch(choice)
       {
           case 0:
           System.out.println("Bye!");
           break;
           case 1: //print
           System.out.println("** List elements **");
           list.print();
           break;
           case 2: //add to front
           System.out.println("Enter integer to add to front");
           newVal = scan.nextInt();
           list.addToFront(newVal);
           break;
           case 3: //add to end
               System.out.println("Enter integer to add to end");
               newVal = scan.nextInt();
               list.addToEnd(newVal);
               break;
           case 4: //remove first element
               list.removeFirst();
               break;
           case 5:
               list.removeLast();
               break;
           case 6:
               System.out.println("Enter a value to remove");
               int oldVal=scan.nextInt();
               list.remove(oldVal);
           default:
           System.out.println("Sorry, invalid choice");
       }
   }
   //-----------------------------------------
   // Prints the user's choices
   //-----------------------------------------
   public static void printMenu()
   {
       System.out.println("\n Menu ");
       System.out.println(" ====");
       System.out.println("0: Quit");
       System.out.println("1: Print list");
       System.out.println("2: Add an integer to the front of the list");
       System.out.println("3: Add an integer to the end of the list");
       System.out.println("4: Remove an integer from the front of the list");
       System.out.println("5: Remove the last value");
       System.out.println("6: Replace first occurence of old value");
       System.out.print("\nEnter your choice: ");
   }
}


Add a comment
Know the answer?
Add Answer to:
Sometimes it is convenient to maintain references to both the next node and the previous node...
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
  • 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...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

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

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

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

  • public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; }...

    public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java

  • Public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=nul...

    public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java

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

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

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