Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked List in Java: Displaying last node incorrectly
    primarykey
    data
    text
    <p>Following are the two classes:</p> <pre><code> import java.io.*; class Node { int data; Node link; BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); void accept()throws IOException { System.out.println("Enter a number: "); data=Integer.parseInt(buf.readLine()); } } import java.io.*; class LinkedList { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); Node start=new Node(); void create()throws IOException { Node n=new Node(); n.accept(); start.link=n; } void addbeg()throws IOException { Node n=new Node(); n.accept(); n.link=start.link; start.link=n; } void addend()throws IOException { Node n=new Node(); n.accept(); Node n1=new Node(); n1=start.link; while(n1.link!=null) { n1=n1.link; } n1.link=n; n.link=null; } void addafter()throws IOException { boolean flag=false; System.out.println("Enter data after which node is to be added: "); int d=Integer.parseInt(buf.readLine()); Node n=new Node(); n.accept(); Node n1=start.link; while(n1.link!=null) { if(n1.data==d) { flag=true; break; } n1=n1.link; } if(flag==true) { n.link=n1.link; n1.link=n; } else System.out.println("Data is not present in the list"); } void addbefore()throws IOException { boolean flag=false; Node n=new Node(); n.accept(); System.out.println("Enter the data before which node is to be added: "); int d=Integer.parseInt(buf.readLine()); Node n1=start.link; Node prev=start; while(n1.link!=null) { if(n1.data==d) { n.link=n1; prev.link=n; flag=true; break; } else { prev=n1; n1=n1.link; } } if(flag==false) { System.out.println("Data not found"); } } void delbeg() { Node n=start.link; start.link=n.link; n.link=null; } void delend() { Node n=start.link; Node prev=start; while(n.link!=null) { prev=n; n=n.link; } prev.link=null; } void delspecific()throws IOException { boolean flag=false; System.out.println("Enter data to be deleted: "); int d=Integer.parseInt(buf.readLine()); Node n=start.link; Node prev=start; while(n.link!=null) { if(n.data==d) { prev.link=n.link; n.link=null; flag=true; } else { prev=n; n=n.link; } } if(flag==false) { System.out.println("Data is not present in the list"); } } void display() { System.out.println("Linked List: "); Node n=start.link; do { System.out.print(n.data+" -&amp;gt; "); n=n.link; } while(n.link!=null); System.out.println(); } void main()throws IOException { boolean flag=false; while(!flag) { System.out.println("Enter choice: "); System.out.println("1. Create "); System.out.println("2. Add at the Beginning "); System.out.println("3. Add at the End "); System.out.println("4. Add after a particular node "); System.out.println("5. Add before a particular node "); System.out.println("6. Delete at the Beginning "); System.out.println("7. Delete at the End "); System.out.println("8. Delete a specific node "); System.out.println("9. View "); System.out.println("10. End "); int ch=Integer.parseInt(buf.readLine()); switch(ch) { case 1:create(); break; case 2:addbeg(); break; case 3:addend(); break; case 4:addafter(); break; case 5:addbefore(); break; case 6:delbeg(); break; case 7:delend(); break; case 8:delspecific(); break; case 9:display(); break; case 10:flag=true; System.exit(0); break; } } } } </code></pre> <p>When I execute any function and view the linked list, the last node is always missing. I have been working on this program for the past four hours and was surprised when the last node was not displayed. Help please</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload