Homework Help Question & Answers

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
Answer

This Homework Help Question: "Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how..." No answers yet.

Be the First!

Request Answer

Request answer!

We need 10 more requests to produce the answer to this homework help question. Share with your friends to get the answer faster!

0 /10 have requested the answer to this homework help question.

Request! (Login Required) Share with friends


Once 10 people have made a request, the answer to this question will be available in 1-2 days.
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 Coin

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

  • C++ help. I need help changing my singly linked list to a doubly linked list. i'm really...

    #include <iostream>using namespace std;// Linked List Class Templatetemplate <typename T>class LinkedList {// Node structurestruct Node {T data;Node* next;Node(T t, Node* n) : data(t), next(n) {}};Node* head; // the head Node pointer is always present when a list is createdint size, fail; // when an operation succeeds, it sets the fail to 0; otherwise 1.public:LinkedList() : head( NULL ), size(0), fail(0) {}LinkedList(T ele) : head( NULL ), fail(0) { size=0; insert(ele); }template<int N>LinkedList( T (&arr) [N]) : head( NULL ), fail(0)...

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

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

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

  • A User-Defined Linked List

    Modify the class LinkedList given in the lecture by adding to it the functions listed below for Exercise 2. In each case, the appropriate error message should begenerated if an invalid condition occurs.a. toString(): modify the display function to overload the toString function of the Object class.b. int length(): create this function to determine the number of items in the list (accessor function).c. void clear(): create this function to remove all of the items from the list. After this operation...

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

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

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