`
strong_fee
  • 浏览: 175846 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

考研决定和初步计划(链表的节点类)

阅读更多

           在下决定之前,向已经在工作岗位上的前辈请教过考研还是工作的问题。我知道只有这些人才最有资格讨论这个问题,当然出来研究领域的大牛。我也知道会有很多不同的答案,甚至有的非常的绝对或者说极端,但我更相信一句话:事在人为!
           我决定考研了,理由我这里就不多说了。此时此刻的该受是自己的学习能力还不够,研究问题的脑袋还没有成熟,我向变得更加冷静,更加沉着,力求达到自己的考研目的。就像有的前辈说的:除了基本外,争取有利于自己事业的一技之长。
           但决定归决定,现在还没有到冲刺的时候,所以现在仍然是提高自己基础的时候,特别是这个时候自己已经有了一定基础,有更有利的条件自己去研究解决一些问题。
           为了增强自己的学习能里,于是在认真学习这几门专业课之时候,自学一下java数据结构(学院开的是用c描述的,多少有差别),争取早点学完。
    今天做完了第一个程序:链表的基本功能:

 

package comeon.feiqiang;   
  
/**  
 * this class is for a Node Oprator
 * include tow menber variables data and link
 * with the methods provided,you can create a new IntNode with two parameters,
 * create a new Node before the current Node,create a copy with head,create a copy with head and tail
 * get
 * 
 */  
  
public class IntNode {
    public int data;   
    public IntNode link;   
  
    public IntNode(int initialData, IntNode initialLink) {   
        data = initialData;   
        link = initialLink;   
    }   
  
    public void addNodeBefore(int element) {   
        link = new IntNode(element, link);   
    }   
  
    /**  
     * @param head  
     * @return the head of the list  
     */  
    public static IntNode listCopy(IntNode head) {   
        IntNode answer = null;   
        if (head == null) {   
            return null;   
        }   
        IntNode cursor = head;   
        answer = new IntNode(head.data, null);   
        IntNode tail = answer;   
        while (cursor.link != null){   
            cursor = cursor.link;   
            tail.link=new IntNode(cursor.data, tail.link);   
            tail = tail.link;   
        }   
        return answer;   
    }   
  
    /**  
     * @param IntNode  
     *            source  
     * @return an array with the head and the tail of a copy of a source list  
     */  
    public static IntNode[] copyWithDail(IntNode source) {   
        IntNode copyHead = null;   
        IntNode copyTail = null;   
        IntNode[] answer = new IntNode[2];   
        if (source == null) {   
            return answer;   
        }   
        copyHead = new IntNode(source.data, null);   
        copyTail = copyHead;   
        source = source.link;   
        while (source != null) {   
            copyTail.link = new IntNode(source.data, copyTail.link);   
            source = source.link;   
            copyTail = copyTail.link;   
        }   
        answer[0] = copyHead;   
        answer[1] = copyTail;   
        return answer;   
    }   
  
    /**  
     * @param head  
     * @return the length of the list  
     */  
    public static int listLength(IntNode head) {   
        int answer = 0;   
        IntNode cursor = head;   
        while (cursor != null) {   
            answer++;   
            cursor = cursor.link;   
        }   
        return answer;
    }   
  
    /**  
     *   
     * @param start  
     * @param end  
     * @return a copy of the current list from the start Node to the end Node  
     */  
    public static IntNode[] listPart(IntNode start, IntNode end) {   
        IntNode copyHead = null;   
        IntNode copyTail = null;   
        IntNode answer[] = new IntNode[2];   
        if (start == null) {   
            throw new IllegalArgumentException("start is null");   
        }   
        if (end == null) {   
            throw new IllegalArgumentException("end is null");   
        }   
        copyHead = new IntNode(start.data, null);   
        copyTail = copyHead;   
        while (start != end) {   
            start = start.link;   
            if (start == null) {   
                throw new IllegalArgumentException(   
                        "end node was not found on this list");   
            }   
            copyTail.link = new IntNode(start.data, copyTail.link);   
            copyTail = copyTail.link;   
        }   
        answer[0] = copyHead;   
        answer[1] = copyTail;   
        return answer;   
    }   
  
    /**  
     * @param head  
     * @param position  
     * @return the IntNode on the position  
     */  
    public static IntNode listPosition(IntNode head, int position) {   
        if (position <= 0) {   
            throw new IllegalArgumentException("position is not positive");   
        }   
        IntNode cursor = head;   
        for (int i = 1; i < position && cursor != null; i++) {   
            cursor = cursor.link;   
        }   
        return cursor;   
    }   
  
    /**  
     * @param IntNode  
     *            head  
     * @param IntNode  
     *            target  
     * @return the IntNode at the data of target  
     */  
    public static IntNode listSearch(IntNode head, int target) {   
        IntNode cursor = head;
        while (cursor != null) {   
            if (cursor.data == target) {   
                return cursor;   
            }   
            cursor = cursor.link;   
        }   
        return null;
    }   
  
    public static void print(IntNode head) {   
        IntNode cursor = head;   
        while (cursor != null) {   
            System.out.println(cursor.data);   
            cursor = cursor.link;
        }   
    }   
}  

  

2
0
分享到:
评论
3 楼 zhjb7 2008-09-28  
千万注意要考一定要考好学校哦,并且要跟的导师不要做java方向哦,切记!
2 楼 strong_fee 2008-09-26  
lighter 写道

as we all know that,the person who make the dicision always is himself,you choose,you decide,then keep going on,no matter how difficult it is.

thank you
1 楼 lighter 2008-09-26  
as we all know that,the person who make the dicision always is himself,you choose,you decide,then keep going on,no matter how difficult it is.

相关推荐

Global site tag (gtag.js) - Google Analytics