Forum Stats

  • 3,826,658 Users
  • 2,260,686 Discussions
  • 7,897,044 Comments

Discussions

Deleting the last Element

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Jan 14, 2016 2:04AM in New To Java

Hi,

I am trying to delete last element from a singly LL. But its not entering in the method deleteFromTail( ) and also printing a message delete2 which i have not written anywhere in my program.. I have not executed the code on any other computer.

import javax.swing.*;

class StudNode{

   int id;

   String name;

   StudNode next;

   StudNode(int id1, String name1) {

      id = id1;

      name = name1;

   }

}

public class StudListDel2{

   StudNode head, tail;

   StudListDel2( ) {

      head= tail = null;

   }

void create4ElementList( ) {

      //add at the tail

      int id =1;

       int i=0;

      String name="CE" + id;

      StudNode newNode;

      newNode = new StudNode(id, name);

      head = newNode;

      tail = newNode;

      for( i=0; i<4; ++i) {

         id++;

         name = "CE" + id;

         newNode = new StudNode(id, name);

         tail.next = newNode;

         tail = newNode;

         //tail.next.next=null;

      }

}

void display4List( ) {

   tail = head;

   while(tail!=null) {

      JOptionPane.showMessageDialog(null,"name=" + tail.name + "id = " + tail.id);

      tail= tail.next;

   }

}

void deleteFromTail( ){

   //if only one node in the list

   JOptionPane.showMessageDialog(null,"delete******=1");

   /*if (head==tail)

      head = tail = null;

   else {

           JOptionPane.showMessageDialog(null,"delete******=2");

           StudNode temp;

           temp = head;

           while(temp.next != tail){

              temp = temp.next;

              JOptionPane.showMessageDialog(null,"Inside Del2 while" + tail.name + "id = " + tail.id);

           }

          

           tail=temp;

           tail.next=null;

           JOptionPane.showMessageDialog(null,"delete3=");

         }

              */

}

public static void main(String[ ] args) {

   StudList obj = new StudList( );

   obj.create4ElementList( );

   obj.display4List();

   JOptionPane.showMessageDialog(null,"deleteOOO1=");

   obj.deleteFromTail( );

   JOptionPane.showMessageDialog(null,"deleteFFF4=");

   obj.display4List( );

}

}

     

Somebody please guide me.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 14, 2016 2:04AM Answer ✓

    Hi,

    Its working. I cant believe it. I just changed the name of file from StudListDel2.java to StudListDel3.java & changed the name of associated class, it started working.

    I dont know if logic is correct. But logic is correct because I got the delete logic from a book only i used 'while' loop instead of 'for'. If any body having time, plz read the code of void deleteFromTail( ){ is correct or not  so that i should not be passing wrong information.

    The complete code is:

    public class StudListDel3{

       StudNode1 head, tail, last;

       StudListDel3( ) {

          last = head= tail = null;

       }

    void create4ElementList( ) {

          //add at the tail

          int id =1;

           int i=0;

          String name="CE" + id;

          StudNode1 newNode;

          newNode = new StudNode1(id, name);

          head = newNode;

          last=tail = newNode;

          for( i=0; i<4; ++i) {

             id++;

             name = "CE" + id;

             newNode = new StudNode1(id, name);

             last.next=tail.next = newNode;

             last= tail = newNode;

             //tail.next.next=null;

          }

    }

    void display4List( ) {

       tail = head;

       while(tail!=null) {

          JOptionPane.showMessageDialog(null,"name=" + tail.name + "id = " + tail.id);

          tail= tail.next;

       }

    }

    void deleteFromTail( ){

       //if only one node in the list

       JOptionPane.showMessageDialog(null,"delete******=1");

       if (head==tail)

          head = tail = null;

       else {

               JOptionPane.showMessageDialog(null,"delete******=2");

               StudNode1 temp;

               temp = head;

               JOptionPane.showMessageDialog(null,"before while delete******=3");

               while(temp.next != last){

                  JOptionPane.showMessageDialog(null,"Inside while 1");

                  temp = temp.next;

                  JOptionPane.showMessageDialog(null,"Inside Del2 while name=" + temp.name + "id = " + temp.id);

               }

             

               last=tail=temp;

               last.next=tail.next=null;

               JOptionPane.showMessageDialog(null,"delete3=");

             }

                

    }

    public static void main(String[ ] args) {

       StudListDel3 obj = new StudListDel3( );

       obj.create4ElementList( );

       obj.display4List();

       JOptionPane.showMessageDialog(null,"deleteOOO1=");

       obj.deleteFromTail( );

       JOptionPane.showMessageDialog(null,"deleteFFF4=");

       obj.display4List( );

    }

    }

        

    Thanks for your response. Its really beneficial when we see feed back from great programmers like you. i am not a programmer.

    Zulfi.

Answers

  • Unknown
    edited Jan 13, 2016 2:07PM

    When you want help with code you have to POST the code you actually need help with.

    What you posted won't even compile let alone run.

    StudList obj = new StudList( );
    

    There is no such 'StudList' class in what you posted

    also printing a message delete2 which i have not written anywhere in my program
    

    Yes - you did

    JOptionPane.showMessageDialog(null,"delete******=2");
    JOptionPane.showMessageDialog(null,"Inside Del2 while" + tail.name + "id = " + tail.id);
    

    Both of those mention delete and 2.

    You need to SHOW US, not just tell us:

    1. WHAT you do

    2. HOW you do it

    3. WHAT results you get

    4. WHAT results you expected to get

    Post an EXACT COPY of the actual code you are using and a command window executing that code that shows the actual result.

    User_AYF65
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 14, 2016 1:45AM

    Hi,

    Actually I found the problem. The code was executing on my home computer but when i ran it on my office it started showing compilation errors. It was running a class which was not part (defined) of my code because  that class inside the package. So i changed the name of all the classes & then i got rid of the compilation errors. "delete2=" message might be because of following class:

    StudList obj = new StudList( );


    this class should be "StudListdel2'.


    Now i am able to run the code & i am not getting the message delete2.


    But its not performing the task. "ITS NOT DELETING THE LAST ELEMENT". Last element id was "5" & its still showing it. It means my logic:


    while(temp.next != last){

    is not working (i.e loop is not stopping at the last element. It is in the following method:

    void deleteFromTail( )



    I am using JOptionPane(....) so i cant paste the output.



    The complete altered  code is:


    import javax.swing.*;

    class StudNode1{

       int id;

       String name;

       StudNode1 next;

       StudNode1(int id1, String name1) {

          id = id1;

          name = name1;

       }

    }

    public class StudListDel2{

       StudNode1 head, tail, last;

       StudListDel2( ) {

          last = head= tail = null;

       }

    void create4ElementList( ) {

          //add at the tail

          int id =1;

           int i=0;

          String name="CE" + id;

          StudNode1 newNode;

          newNode = new StudNode1(id, name);

          head = newNode;

          last=tail = newNode;

          for( i=0; i<4; ++i) {

             id++;

             name = "CE" + id;

             newNode = new StudNode1(id, name);

             last.next=tail.next = newNode;

             last= tail = newNode;

             //tail.next.next=null;

          }

    }

    void display4List( ) {

       tail = head;

       while(tail!=null) {

          JOptionPane.showMessageDialog(null,"name=" + tail.name + "id = " + tail.id);

          tail= tail.next;

       }

    }

    void deleteFromTail( ){

       //if only one node in the list

       JOptionPane.showMessageDialog(null,"delete******=1");

       if (head==tail)

          head = tail = null;

       else {

               JOptionPane.showMessageDialog(null,"delete******=2");

               StudNode1 temp;

               temp = head;

               JOptionPane.showMessageDialog(null,"before while delete******=3");

               while(temp.next != last){

                  JOptionPane.showMessageDialog(null,"Inside while 1");

                  temp = temp.next;

                  JOptionPane.showMessageDialog(null,"Inside Del2 while name=" + temp.name + "id = " + temp.id);

               }

              

               last=tail=temp;

               last.next=tail.next=null;

               JOptionPane.showMessageDialog(null,"delete3=");

             }

                 

    }

    public static void main(String[ ] args) {

       StudListDel2 obj = new StudListDel2( );

       obj.create4ElementList( );

       obj.display4List();

       JOptionPane.showMessageDialog(null,"deleteOOO1=");

      obj.deleteFromTail( );

       JOptionPane.showMessageDialog(null,"deleteFFF4=");

       obj.display4List( );

    }

    }

         

    Some body please guide.

    Zulfi.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jan 14, 2016 2:04AM Answer ✓

    Hi,

    Its working. I cant believe it. I just changed the name of file from StudListDel2.java to StudListDel3.java & changed the name of associated class, it started working.

    I dont know if logic is correct. But logic is correct because I got the delete logic from a book only i used 'while' loop instead of 'for'. If any body having time, plz read the code of void deleteFromTail( ){ is correct or not  so that i should not be passing wrong information.

    The complete code is:

    public class StudListDel3{

       StudNode1 head, tail, last;

       StudListDel3( ) {

          last = head= tail = null;

       }

    void create4ElementList( ) {

          //add at the tail

          int id =1;

           int i=0;

          String name="CE" + id;

          StudNode1 newNode;

          newNode = new StudNode1(id, name);

          head = newNode;

          last=tail = newNode;

          for( i=0; i<4; ++i) {

             id++;

             name = "CE" + id;

             newNode = new StudNode1(id, name);

             last.next=tail.next = newNode;

             last= tail = newNode;

             //tail.next.next=null;

          }

    }

    void display4List( ) {

       tail = head;

       while(tail!=null) {

          JOptionPane.showMessageDialog(null,"name=" + tail.name + "id = " + tail.id);

          tail= tail.next;

       }

    }

    void deleteFromTail( ){

       //if only one node in the list

       JOptionPane.showMessageDialog(null,"delete******=1");

       if (head==tail)

          head = tail = null;

       else {

               JOptionPane.showMessageDialog(null,"delete******=2");

               StudNode1 temp;

               temp = head;

               JOptionPane.showMessageDialog(null,"before while delete******=3");

               while(temp.next != last){

                  JOptionPane.showMessageDialog(null,"Inside while 1");

                  temp = temp.next;

                  JOptionPane.showMessageDialog(null,"Inside Del2 while name=" + temp.name + "id = " + temp.id);

               }

             

               last=tail=temp;

               last.next=tail.next=null;

               JOptionPane.showMessageDialog(null,"delete3=");

             }

                

    }

    public static void main(String[ ] args) {

       StudListDel3 obj = new StudListDel3( );

       obj.create4ElementList( );

       obj.display4List();

       JOptionPane.showMessageDialog(null,"deleteOOO1=");

       obj.deleteFromTail( );

       JOptionPane.showMessageDialog(null,"deleteFFF4=");

       obj.display4List( );

    }

    }

        

    Thanks for your response. Its really beneficial when we see feed back from great programmers like you. i am not a programmer.

    Zulfi.

This discussion has been closed.