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.