java数据结构之单链表(添加尾指针)

import  cj.util. * ;

class  Link < E >   {
    
public E data;
    
public Link<E> next;
    
public Link(E data) {
        
this.data = data;
    }

}


class  LinkList < E >   {
    
public boolean ismade;  // must call firstMade() first
    public Link<E> head;
    
public Link<E> tail;     
    
public int num;
    
    
public LinkList() {
        head 
= null;
        tail 
= null;
        ismade 
= false;
        num 
= 0;
    }


    
public boolean isEmpty() {
        
return num == 0;
    }

    
    
public void checkIsmadeTrue() throws Exception {
        
if(ismade)
            
throw new Exception("don't call twice firstMade!");
    }

    
    
public void checkIsmadeFalse() throws Exception {
        
if(!ismade)
            
throw new Exception("please call firstMade at first");
    }

    
    
public void display() {
        Link
<E> cur = head;
//        while (cur != tail) {
//            P.prn(cur.data + " ");
//            cur = cur.next;
//        }
        
// If linklist isnot init, cannot execute
        if(cur == null{
            P.prn(
"do you want to die?");
        }

        
do {
            P.pr(cur.data 
+ " ");
            cur 
= cur.next;
        }
 while (cur != head);
        
        P.prnEnter();
    }

    
    
public void firstMade(E value)
    
{
        
// check ismade , just call once
        Link<E> link = new Link<E>(value);
//        link.next = head;
        head = link;
        tail 
= link;
        head.next 
= tail;
        tail.next 
= head;
        ismade 
= true;
        num
++;
    }


    
// insert from head
    public void insertHead(E value) throws Exception {
        
// you can check ismade
        checkIsmadeFalse();
        Link
<E>    link = new Link<E>(value);
        link.next 
= head;
        head 
= link;
        tail.next 
= link;
        num
++;
    }

    
    
//
    public void insertTail(E value) throws Exception {
        
// you can check itmade
        checkIsmadeFalse();
        Link
<E>    link = new Link<E>(value);
        tail.next 
= link;
        tail 
= link;
        link.next 
= head;
        num
++;
    }

    
    
// delete head
    public Link<E> deleteHead() {
        Link
<E> temp = head;
        head 
= head.next;
        tail.next 
= head;
        num
--;
        
return temp;
    }

    
    
// delete tail, difficult!! use a Prev Pointer is better
    public Link<E> deleteTail() {
        Link
<E> cur = head;
        Link
<E> temp;
        
// get the tail's prev one link
        while(cur.next.next != head)
            cur 
= cur.next;
        temp 
= cur.next;
        cur.next 
= head;
        tail 
= cur;
        
return temp;
    }

    
    
// get the  Node through index
    public Link<E> get(int index) {
        
if(index > this.getNum() || index < 0{
            
try {
                
throw new IndexOutOfBoundsException();
            }
catch (Exception e) {
                e.printStackTrace();
            }

        }

        Link
<E> cur = head;
        
// make the index from 0 and find the tail quickly
        if(index == this.getNum())
            
return tail;
        
        
for (int i = 0; i < getNum(); i++{
            
if (i == index) 
                
return cur;
            
else 
                cur 
= cur.next;
        }

        
return null;
    }

    
    
public Link<E> get(E data) {
        
if(data == null || head == null)
            
try {
                
throw new Exception("data is null");
            }
 catch (Exception e) {
                e.printStackTrace();
            }

        Link
<E> cur = head;
        
do{
            
if(cur.data.equals(data))
                
return cur;
            
else 
                cur 
= cur.next;
        }
while(cur != head);
        
return null;
    }

    
// remove a Node
    public void remove(int index) {
        
if(index > this.getNum() || index < 0{
            
try {
                
throw new IndexOutOfBoundsException();
            }
catch (Exception e) {
                e.printStackTrace();
            }

        }

        
else if(index == 0{
            
this.deleteHead();
        }

        
else if(index == this.getNum()) {
            
this.deleteTail();
        }

        
else {
            
this.get(index - 1).next = this.get(index + 1);
        }

    }

    
    
public LinkList<E> reverse() throws Exception {
        LinkList
<E> temp = new LinkList<E>();
        Link
<E> cur = head;
        temp.firstMade(cur.data);
        cur 
= cur.next;
        
for(int i=1; i<this.getNum() && cur != null; i++, cur = cur.next) {
            temp.insertHead(cur.data);
        }

        
return temp;
    }

    
    
public LinkList<E> clone() {
        LinkList
<E> temp = new LinkList<E>();
        Link
<E> cur = head;
        temp.firstMade(cur.data);
        cur 
= cur.next;
        
for(int i=1; i<this.getNum() && cur != null; i++, cur = cur.next) {
            
try {
                temp.insertTail(cur.data);
            }
 catch (Exception e) {
                e.printStackTrace();
            }

        }

        
return temp;
    }

    
    
public Link<E> getHead() {
        
return head;
    }


    
public void setHead(Link<E> head) {
        
this.head = head;
    }


    
public int getNum() {
        
return num;
    }


    
public void setNum(int num) {
        
this.num = num;
    }

    
    
public boolean isIsmade() {
        
return ismade;
    }


    
public void setIsmade(boolean ismade) {
        
this.ismade = ismade;
    }


    
public Link<E> getTail() {
        
return tail;
    }


    
public void setTail(Link<E> tail) {
        
this.tail = tail;
    }

}

public   class  SingleLink  {

    
/**
     * 
@param args
     * 
@throws Exception 
     
*/

    
public static void main(String[] args) throws Exception {
        
// TODO Auto-generated method stub
        LinkList<Long> sl = new LinkList<Long>();
        
        sl.firstMade((Long.valueOf(
1)));
        
for(int i=0; i<10; i++{
            Long value 
= (long)(Math.random()*100);
            sl.insertHead(value);
        }

        sl.display();
//        sl.deleteHead();
//        sl.deleteTail();
        Link<Long> l = sl.get(9);
        Link
<Long> l1 = sl.get(l.data);
//        sl.display();
        P.prn(l.data);
        P.prn(l1.data);
//        sl.remove(9);
//        sl = sl.reverse();
        LinkList<Long> sl1 = sl.clone();
        sl1.display();
    }


}


clone函数不能添加异常声明, 只能用try catch包裹
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值