单链表的java实现即链表的常见操作

来源:互联网 发布:别哭了宝贝网络歌手 编辑:程序博客网 时间:2024/06/13 20:06
package Test1;public class MyList {    protected IntNode head,tail;    //构造函数    public MyList(){    head=null;    tail=null;    }    //判断是否为空    public boolean isEmpty(){    return head==null;    }    //返回大小    public int size(){    int size=0;    for(IntNode it=head;it!=null;it=it.next)    size++;    return size;    }    //在连头添加节点    public void addToHead(int el){    head=new IntNode(el,head);    if(tail==null)    tail=head;    }    //在连尾添加节点    public void addToTail(int el){    if(!isEmpty()){    tail.next=new IntNode(el,null);    tail=tail.next;    }else    head=tail=new IntNode(el,null);    }    public void addToTail(IntNode el){    if(!isEmpty())    {    tail.next=el;    el.next=null;    }else    {    head=el;    el.next=null;    }    }    //从连头删除节点    public boolean deleteFromHead(){         if(isEmpty())     return false;     if(head==tail)     head=tail=null;     else     head=head.next;     return true;    }    //从连尾删除节点    public boolean deleteFromTail(){    if(isEmpty())    return false;    if(head==tail)    head=tail=null;    else{    IntNode tmp;    for(tmp=head;tmp.next!=tail;tmp=tmp.next);    tail=tmp;    tail.next=null;    }    return true;    }    //打印出链表的内容    public void printAll(){    for(IntNode i=head;i!=null;i=i.next)    System.out.print(i.info);    }    //判断一个整数是否在链表中    public boolean isInList(int el){    boolean res=false;    for(IntNode i=head;i!=null;i=i.next)    {    if(i.info==el){    res=true;    break;    }    }    return res;    }    //删除指定的元素  删除返回true   没有返回false    public boolean delete(int el){    boolean res=false;    for(IntNode i=head;i!=null;i=i.next)    {    if(i.info==el)    {    if(i==head) deleteFromHead();    else if(i==tail) deleteFromTail();    else     {    IntNode tmp;    for(tmp=head;tmp.next!=i;tmp=tmp.next);    tmp.next=i.next;    }    }    }    return res;    }    //删除链表中的第 i个节点 删除成功返回true,失败返回false    public boolean delete0(int index){    boolean res=false;    int i=1;    IntNode it=head;       while(i<index&&it!=null)       {       it=it.next;       i++;       }       if(i==index&&it!=null)       {       if(it.next==null)       deleteFromTail();       else if(it==head)       deleteFromHead();       else       {          IntNode tmp=head;       for(;tmp.next!=it;tmp=tmp.next);       tmp.next=it.next;       }       res=true;       }       return res;           }    //反转链表    public void reverseList(){    if(head==null||head.next==null)    return ;    else    {    IntNode it=head.next;    IntNode itf=head;    IntNode itb=it.next;    while(itb != null)    {    it.next=itf;    itf=it;    it=itb;    itb=it.next;    }    it.next=itf;    head.next=null;    IntNode tmp;    tmp=head;    head=tail;    tail=tmp;    }    }    //合并两个有序的链表    //L1:1->3->6    //L2:4->5->7    //合并后:1->2->4->5->6->7    public static MyList  mergeList(MyList l1,MyList l2){     MyList res=new MyList();     IntNode p1=l1.head;     IntNode p2=l2.head;     while(p1!=null&&p2!=null)     {     if(p1.info<p2.info)     {     res.addToTail(p1.info);     p1=p1.next;     }else     {     res.addToTail(p2.info);     p2=p2.next;     }     }     if(p1==null){     for(IntNode i=p2;i!=null;i=i.next)     res.addToTail(i.info);     }else     {     for(IntNode i=p1;i!=null;i=i.next)     res.addToTail(i.info);     }     return res;        }    //判断两个单向链表是否有相同的内容    public boolean equal(MyList list){    return head==list.head;    }       //在a之后,b之前插入节点    public void insertToPrev(IntNode el,IntNode pa){    if(pa==head)           addToHead(el.info);    else{    IntNode prev=head;    for(;prev.next!=pa;prev=prev.next);    prev.next=el;    el.next=pa;    }        }    public void insertToBack(IntNode el,IntNode pb){    if(pb==tail)    addToTail(el.info);    else{    el.next=pb.next;    pb.next=el;    }    }        //将L链表连接到尾部    public void listCat(MyList list){    for(IntNode it=list.head;it!=null;it=it.next)    addToTail(it.info);    }        //进行升序排列    public void sort(){    for(IntNode i=head;i.next!=null;i=i.next)    {    IntNode minPointer=i;    int min=i.info;    for(IntNode j=head.next;j!=null;j=j.next)    {    if(j.info<min)    {    minPointer=j;    min=j.info;    }    }    if(minPointer!=i){    int tmp=i.info;    i.info=minPointer.info;    minPointer.info=tmp;    }    }    }    //测试用列    public static void main(String[] args){    MyList l1=new MyList();    l1.addToTail(1);    l1.addToTail(9);    l1.addToTail(6);    l1.sort();    l1.printAll();    System.out.println(l1.size());    MyList l2=new MyList();    l2.addToHead(4);    l2.addToHead(7);    l2.addToHead(6);    l2.sort();    l2.printAll();    System.out.println();    MyList l3=MyList.mergeList(l1, l2);    l3.printAll();    System.out.println(l3.size());    }}class IntNode{public int info;public IntNode next;public IntNode(int info,IntNode next){this.info=info;this.next=next;}public IntNode(int info){this(info,null);}}
0 0
原创粉丝点击