Question

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.

  1. import java.util.Scanner;
  2. 
     
  3. /*  Class Node  */
  4. class Node
  5. {
  6.     protected int data;
  7.     protected Node next, prev;
  8. 
     
  9.     /* Constructor */
  10.     public Node()
  11.     {
  12.         next = null;
  13.         prev = null;
  14.         data = 0;
  15.     }
  16.     /* Constructor */
  17.     public Node(int d, Node n, Node p)
  18.     {
  19.         data = d;
  20.         next = n;
  21.         prev = p;
  22.     }
  23.     /* Function to set link to next node */
  24.     public void setLinkNext(Node n)
  25.     {
  26.         next = n;
  27.     }
  28.     /* Function to set link to previous node */
  29.     public void setLinkPrev(Node p)
  30.     {
  31.         prev = p;
  32.     }    
  33.     /* Funtion to get link to next node */
  34.     public Node getLinkNext()
  35.     {
  36.         return next;
  37.     }
  38.     /* Function to get link to previous node */
  39.     public Node getLinkPrev()
  40.     {
  41.         return prev;
  42.     }
  43.     /* Function to set data to node */
  44.     public void setData(int d)
  45.     {
  46.         data = d;
  47.     }
  48.     /* Function to get data from node */
  49.     public int getData()
  50.     {
  51.         return data;
  52.     }
  53. }
  54. 
     
  55. /* Class linkedList */
  56. class linkedList
  57. {
  58.     protected Node start;
  59.     protected Node end ;
  60.     public int size;
  61. 
     
  62.     /* Constructor */
  63.     public linkedList()
  64.     {
  65.         start = null;
  66.         end = null;
  67.         size = 0;
  68.     }
  69.     /* Function to check if list is empty */
  70.     public boolean isEmpty()
  71.     {
  72.         return start == null;
  73.     }
  74.     /* Function to get size of list */
  75.     public int getSize()
  76.     {
  77.         return size;
  78.     }
  79.     /* Function to insert element at begining */
  80.     public void insertAtStart(int val)
  81.     {
  82.         Node nptr = new Node(val, null, null);        
  83.         if(start == null)
  84.         {
  85.             start = nptr;
  86.             end = start;
  87.         }
  88.         else
  89.         {
  90.             start.setLinkPrev(nptr);
  91.             nptr.setLinkNext(start);
  92.             start = nptr;
  93.         }
  94.         size++;
  95.     }
  96.     /* Function to insert element at end */
  97.     public void insertAtEnd(int val)
  98.     {
  99.         Node nptr = new Node(val, null, null);        
  100.         if(start == null)
  101.         {
  102.             start = nptr;
  103.             end = start;
  104.         }
  105.         else
  106.         {
  107.             nptr.setLinkPrev(end);
  108.             end.setLinkNext(nptr);
  109.             end = nptr;
  110.         }
  111.         size++;
  112.     }
  113.     /* Function to insert element at position */
  114.     public void insertAtPos(int val , int pos)
  115.     {
  116.         Node nptr = new Node(val, null, null);    
  117.         if (pos == 1)
  118.         {
  119.             insertAtStart(val);
  120.             return;
  121.         }            
  122.         Node ptr = start;
  123.         for (int i = 2; i <= size; i++)
  124.         {
  125.             if (i == pos)
  126.             {
  127.                 Node tmp = ptr.getLinkNext();
  128.                 ptr.setLinkNext(nptr);
  129.                 nptr.setLinkPrev(ptr);
  130.                 nptr.setLinkNext(tmp);
  131.                 tmp.setLinkPrev(nptr);
  132.             }
  133.             ptr = ptr.getLinkNext();            
  134.         }
  135.         size++ ;
  136.     }
  137.     /* Function to delete node at position */
  138.     public void deleteAtPos(int pos)
  139.     {        
  140.         if (pos == 1) 
  141.         {
  142.             if (size == 1)
  143.             {
  144.                 start = null;
  145.                 end = null;
  146.                 size = 0;
  147.                 return; 
  148.             }
  149.             start = start.getLinkNext();
  150.             start.setLinkPrev(null);
  151.             size--; 
  152.             return ;
  153.         }
  154.         if (pos == size)
  155.         {
  156.             end = end.getLinkPrev();
  157.             end.setLinkNext(null);
  158.             size-- ;
  159.         }
  160.         Node ptr = start.getLinkNext();
  161.         for (int i = 2; i <= size; i++)
  162.         {
  163.             if (i == pos)
  164.             {
  165.                 Node p = ptr.getLinkPrev();
  166.                 Node n = ptr.getLinkNext();
  167. 
     
  168.                 p.setLinkNext(n);
  169.                 n.setLinkPrev(p);
  170.                 size-- ;
  171.                 return;
  172.             }
  173.             ptr = ptr.getLinkNext();
  174.         }        
  175.     }    
  176.     /* Function to display status of list */
  177.     public void display()
  178.     {
  179.         System.out.print("\nDoubly Linked List = ");
  180.         if (size == 0) 
  181.         {
  182.             System.out.print("empty\n");
  183.             return;
  184.         }
  185.         if (start.getLinkNext() == null) 
  186.         {
  187.             System.out.println(start.getData() );
  188.             return;
  189.         }
  190.         Node ptr = start;
  191.         System.out.print(start.getData()+ " <-> ");
  192.         ptr = start.getLinkNext();
  193.         while (ptr.getLinkNext() != null)
  194.         {
  195.             System.out.print(ptr.getData()+ " <-> ");
  196.             ptr = ptr.getLinkNext();
  197.         }
  198.         System.out.print(ptr.getData()+ "\n");
  199.     }
  200. }
  201. 
     
  202. /* Class DoublyLinkedList */
  203. public class DoublyLinkedList
  204. {    
  205.     public static void main(String[] args)
  206.     {            
  207.         Scanner scan = new Scanner(System.in);
  208.         /* Creating object of linkedList */
  209.         linkedList list = new linkedList(); 
  210.         System.out.println("Doubly Linked List Test\n");          
  211.         char ch;
  212.         /*  Perform list operations  */
  213.         do
  214.         {
  215.             System.out.println("\nDoubly Linked List Operations\n");
  216.             System.out.println("1. insert at begining");
  217.             System.out.println("2. insert at end");
  218.             System.out.println("3. insert at position");
  219.             System.out.println("4. delete at position");
  220.             System.out.println("5. check empty");
  221.             System.out.println("6. get size");
  222. 
     
  223.             int choice = scan.nextInt();            
  224.             switch (choice)
  225.             {
  226.             case 1 : 
  227.                 System.out.println("Enter integer element to insert");
  228.                 list.insertAtStart( scan.nextInt() );                     
  229.                 break;                          
  230.             case 2 : 
  231.                 System.out.println("Enter integer element to insert");
  232.                 list.insertAtEnd( scan.nextInt() );                     
  233.                 break;                         
  234.             case 3 : 
  235.                 System.out.println("Enter integer element to insert");
  236.                 int num = scan.nextInt() ;
  237.                 System.out.println("Enter position");
  238.                 int pos = scan.nextInt() ;
  239.                 if (pos < 1 || pos > list.getSize() )
  240.                     System.out.println("Invalid position\n");
  241.                 else
  242.                     list.insertAtPos(num, pos);
  243.                 break;                                          
  244.             case 4 : 
  245.                 System.out.println("Enter position");
  246.                 int p = scan.nextInt() ;
  247.                 if (p < 1 || p > list.getSize() )
  248.                     System.out.println("Invalid position\n");
  249.                 else
  250.                     list.deleteAtPos(p);
  251.                 break;     
  252.             case 5 : 
  253.                 System.out.println("Empty status = "+ list.isEmpty());
  254.                 break;            
  255.             case 6 : 
  256.                 System.out.println("Size = "+ list.getSize() +" \n");
  257.                 break;                         
  258.             default : 
  259.                 System.out.println("Wrong Entry \n ");
  260.                 break;   
  261.             }    
  262.             /*  Display List  */ 
  263.             list.display();
  264.             System.out.println("\nDo you want to continue (Type y or n) \n");
  265.             ch = scan.next().charAt(0);    
  266. 
     
  267.         } while (ch == 'Y'|| ch == 'y');               
  268.     }
  269. }
0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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;...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains...

    iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...

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
ADVERTISEMENT