⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 address.java

📁 java应用双链表实现的通信录
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;
class note
{
  String name;
  String Lname;
  double telephone_num;
  String email;
  String address;
  note  next;
  note  prior;

 note(String NAME,String TEL,String Email,String Address)
  {
   // this(NAME,TEL,Email,Address,null,null);
  }
 note(String Name,double TEL,String Email,String Address,note Next,note Prior)
  {
    name=Name;
    Lname=name;
    Lname=Lname.toLowerCase();
    telephone_num=TEL;
    email=Email;
    address=Address;
    next=Next;
    prior=Prior;


  }
}
class build
{
        note head,tail,current,current_head,current_tail; int k=0;
        build()throws IOException
        {
                 init();
                 String NAME,EMAIL,ADDRESS;
                 double TELEPHONE_NUM;
                 StreamTokenizer Fin=(new StreamTokenizer(new BufferedReader(new FileReader("D:\\data.txt"))));
                 Fin.wordChars(33,122);
                 Fin.nextToken();
                do
                {

                        NAME=Fin.sval;
                        Fin.nextToken();
                        TELEPHONE_NUM=Fin.nval;
                        Fin.nextToken();
                        EMAIL=Fin.sval;
                        Fin.nextToken();
                        ADDRESS=Fin.sval;
                        add(NAME,TELEPHONE_NUM,EMAIL,ADDRESS);

                }while(Fin.nextToken()!=StreamTokenizer.TT_EOF);
                current=current_head=current_tail=head;
        }

  public void init()
  {
    head=tail=current=current_head=current_tail=null;
  }
  public boolean isEmpty()
  {
    return head==null;
  }

  public void add(String Name,double TEL,String Email,String Address)
  {
         String lname=Name;
         lname=lname.toLowerCase();
    if (head==null)
    {
      head=new note(Name, TEL, Email, Address, null, null);
      current=head;

    }
    else
    {       	//current=new note(Name,TEL,Email,Address,current.next,current.prior);
            note temp1,temp2;
            temp1=head;temp2=head.next;
            for(;lname.compareTo(temp1.Lname)>0&&temp2!=null;)//lname should be in the front of temp1.Lname
        {
            if(temp2!=null)
            {
              temp1=temp2;
              temp2=temp2.next;
            }
        }

                    if(temp1==head&&lname.compareTo(temp1.Lname)<=0)//add to head
                    {
                            head=new note(Name, TEL, Email, Address, temp1, null);
                            temp1.prior=head;
                         current=head;
                    }
                    else if(temp2==null&&lname.compareTo(temp1.Lname)>0)//add to tail
                    {
                            tail=new note(Name,TEL,Email,Address,null,temp1);
                            temp1.next=tail;
                            current=tail;
                    }
                    else//add to the middle
                    {
                            current=new note(Name,TEL,Email,Address,temp1,temp1.prior);
                            temp1.prior.next=current;
                            temp1.prior=current;
                    }


    }

  }
  public boolean remove(String Name)
  {
              note temp1,temp2;
              temp1=head;temp2=head.next;
              String lname=Name;
              lname=lname.toLowerCase();
              for(;lname.equals(temp1.Lname)==false&&temp1!=null;)
                {
                  temp1=temp2;
                  if(temp2!=null)
                  temp2=temp2.next;
                  else break;
                }
              if(temp1==null)
                      return false;
              else
              {
                if(temp1==head)
                {
                         if(tail==null)
                     {
                             head=tail=current=null;
                             return true;
                     }
                     else if(tail==head.next)
                     {
                             head=tail;
                             tail=null;
                             head.prior=null;
                             head.next=null;
                             current=head;
                             return true;
                     }
                     else
                     {
                             head=temp1.next;
                             head.prior=null;
                             current=head;
                             return true;
                     }
                }
                else if(temp1==tail)
                {
                        if(tail.prior==head)
                        {
                                head.next=null;
                                current=head;
                                tail=null;
                                return true;
                        }
                        else
                        {
                                 tail=tail.prior;
                                 tail.next=null;
                                current=tail;
                                return true;
                        }
                }
                else
                {
                        temp1.prior.next=temp1.next;
                        temp1.next.prior=temp1.prior;
                        current=temp1.next;
                        return true;
                }
              }
  }
  public boolean find(String Name)
  {
           Name=Name.toLowerCase();
             note temp1,temp2;
            temp1=head;temp2=temp1.next;
            for(;(Name.equals(temp1.Lname)==false)&&temp1!=null;)
            {
              temp1=temp2;
              if(temp1!=null)
              {
                temp2=temp2.next;
              }
              else break;
            }
            if(temp1==null)
                    return false;
            else
            {
                    print(temp1);
                    return true;
              }

  }
  public boolean edit(int n,String Nname,double TEL,String Email,String Address)
  {
    if (n > k)
      return false;
    else {
      note temp;
      int i = 1;
      for (temp = current_head; i < n; ) {

        temp = temp.next;
        i++;
      }
      current = temp;
      remove(current.name);
      add(Nname, TEL, Email, Address);
      return true;

    }
  }
    public boolean delete(int n) {
      if (n > k)
        return false;
      else {
        note temp;
        int i = 1;
        for (temp = current_head; i < n; ) {

          temp = temp.next;
          i++;
        }
        current = temp;
        remove(current.name);
        return true;
      }

    }
public void save() throws IOException
{
     note temp;
      FileWriter fw=new FileWriter("D:\\data.txt");//PrintWriter pw=new PrintWriter(new FileWriter("D:\\data.txt"))
      PrintWriter pw=new PrintWriter(fw);
 for(temp=head;temp!=null;temp=temp.next)
  {
         pw.println(temp.name+" "+temp.telephone_num+" "+temp.email+" "+temp.address);
  }
      pw.close();
      fw.close();

}

  public void print(note Name)
  {
          int i=1;
          clear();
          System.out.println(i+"\nNAME: "+Name.name+"\nTEL:  "
                          +Name.telephone_num+"\nEMAIL: "+Name.email+"\nADDRESS: "+Name.address);
          current_head=current_tail=current=Name;
          System.out.println("*******************************************************************");
          k=1;
  }
  public void first(int n)
  {
          note temp1,temp2;
          int i=1;

          for(temp1=head,temp2=head.next;temp1!=null&&i<=n;)
          {
           System.out.print(i+"\nNAME: "+temp1.name+"\nTEL:  ");
           System.out.println(temp1.telephone_num+"\nEMAIL: "+temp1.email+"\nADDRESS: "+temp1.address);
           k=i;
           temp1=temp2;
           if(temp1!=null)
           {
             temp2=temp2.next;
             i++;
           }
           else
           {
               System.out.println("You have reached end of the list.");
               break;
           }

        }

          current=current_head=head;
          if(temp1==null)
                  current_tail=tail;
          else
                  current_tail=temp1.prior;
          System.out.println("*******************************************************************");
  }
  public void last(int n)
  {

          note temp1,temp2,temp;
          int i=1;
          if(tail==null)
          print(head);
          else
          {
              temp1=tail;
              temp2=temp1.prior;
              for(;temp1!=null&&i<=n;)
              {
                temp1=temp2;
                if(temp1!=null)
                {
                  temp2=temp2.prior;
                  i++;
                }
                else
                  break;
              }
              if(temp1==null)
              {
                i=1;
                current=current_head=current_tail=head;
                for(temp1=head;temp1!=null;temp1=temp1.next,i++)
                {
                  System.out.print(i+"\nNAME: "+temp1.name+"\nTEL:  ");
                  System.out.println(temp1.telephone_num+"\nEMAIL: "+temp1.email+"\nADDRESS: "+temp1.address);
                  k=i;
                }
              }
              else
              {
                 i=1;
                 for(current_head=temp1.next,current_tail=tail,temp1=current_head;temp1!=null;temp1=temp1.next,i++)
                 {
                   System.out.print(i+"\nNAME: "+temp1.name+"\nTEL:  ");
                  System.out.println(temp1.telephone_num+"\nEMAIL: "+temp1.email+"\nADDRESS: "+temp1.address);
                  k=i;
                 }
              }

          }
          System.out.println("*******************************************************************");
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -