Java 写的一个单链表

我写的一个java的单链表,可以从表头插入Node,比较简单,可以再扩充功能!! 

 

package com.hukefeng.lang;

 

import java.util.*;

 

public class LangTest {

 

    /**

     * @param args

     */

    public static void main(String[] args)throws Exception {

       // TODO Auto-generated method stub

 

       NodeList nl=new NodeList();

       nl.insertNode("Hello");

       nl.insertNode("World");

       nl.insertNode("and");

       nl.insertNode("Hu kefeng");

       System.out.println("Length of the Nodelist: "+nl.getLength());

           nl.listNode();

 

   

      

 

    }

 

}

 

class Node {

 

    private Object obj = null;

 

    public Node next = null;

 

    public Node(Object obj) {

       this.obj = obj;

    }

 

    public Object getObj() {

       return obj;

    }

 

}

 

class NodeList {

 

    // define the pointer of the linklist

    private Node head = null;

   

    private int Length = 0;

 

    public void listNode() {

           Node pointer=head;

           while (pointer != null) {

              System.out.println(pointer.getObj().toString());

              pointer=pointer.next;

      

           }

       }

 

 

    /**

     * @return

     * @author hukefeng

     *

     */

    public boolean isEmpty() {

      

 

       return head == null;

    }

 

    // insert the node

    public void insertNode(Object obj) {

       Node node=new Node(obj);

      

      

       node.next=head;

       head=node;

       Length++;

 

 

    }

/**

 * @author Hu kefeng

 *

 * @return Node

 */

    public Node deleteNode() {

       Node temp = null;

       if (head == null) {

           throw new NoSuchElementException();

       }

       if (head != null) {

           temp = head.next;

           head = head.next;

       }

       return temp;

    }

 

    public void deleteAll() {

       this.Length = 0;

       this.head = null;

   

 

    }

 

    public int getLength() {

       return Length;

    }

 

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值