Sorting string alphabetically in linked list(Java)
import java.util.*; public class contacts { protected Node head,tail; public contacts() { head=tail=null; } public boolean isEmpty() { return head==null; } public void newContact(String name,String address, String rel, int number) { if(head==null) head =tail= new Node(name,address,rel,number,head); else { Node p,t; for(p=head,t=head.next;t!=null;p=p.next,t=t.next); if(t!=null) p.next=new Node(name,address,rel,number,t); else tail.next=tail=new Node(name,address,rel,number,null); } System.out.println(); } public void changePhone(String a,int oldp,int newp) { Node tmp; for (tmp=head ; tmp!=null && !(a.equalsIgnoreCase(tmp.name)); tmp=tmp.next); if (tmp.number==oldp) { tmp.number=newp; System.out.println("number changed"); } else System.out.println("wrong number, cannot change"); } public void searchByName(String a) { Node t; for (t=head ; t!=null && !(a.equalsIgnoreCase(t.name)); t=t.next); if(t==null) System.out.println("There is no contact with this name"); else System.out.println("Found "+"\t"+ t.name+"\t" +t.address+"\t"+ t.rel +"\t"+t.number); } public void delete(String a) { if (isEmpty()) System.out.println("No contacts"); else { if (head==tail && head.name.equals(a)) head=tail=null; else if (head.name.equals(a)) head=head.next; else { Node p,t; for (p=head ,t=head.next ; t!=null && !t.name.equals(a) ; p=p.next , t=t.next); if (t==null) System.out.println("No contact with this name "); else { p.next=t.next; if(tail==t) tail=p; } } } } public void print() { Node tmp; if(head==null) System.out.println("There is no contact"); else for(tmp=head;tmp!=null;tmp=tmp.next) { System.out.println(tmp.name+"\t"+tmp.address+"\t"+tmp.rel+"\t" +tmp.number); } } }