ITPub博客

首页 > Linux操作系统 > Linux操作系统 > JAVA实现单链表

JAVA实现单链表

原创 Linux操作系统 作者:cppsoldier 时间:2008-10-28 10:47:43 0 删除 编辑

package com.hb.ds.sigleLinkList;

class ListNode {
 ListNode next;
 int value;
 
 public ListNode(int value){
  this.next = null;
  this.value = value;
 }
 
 
 public void printNodeValue(){
  System.out.print(this.value+" ");
 }
}

public class SingleLinkList {

 private ListNode first;
 private int length;
 
 public SingleLinkList(){
  this.first = null;
  this.length =0;
 }
 
 public void insertBeforeFirst(int value){
  ListNode node = new ListNode(value);
  
  if(first==null){
   first = node;
  }else{
   node.next = first;
   first = node;
  }
  length++;
 }
 
 public void printList(){
  ListNode temp = first;
  while(temp != null){
   temp.printNodeValue();
   temp = temp.next;
  }
 }
 
 public static void main(String[] args){
  SingleLinkList slinkList = new SingleLinkList();
  slinkList.insertBeforeFirst(12);
  slinkList.insertBeforeFirst(11);
  slinkList.insertBeforeFirst(10);
  slinkList.insertBeforeFirst(9);
  slinkList.insertBeforeFirst(8);
  
  slinkList.printList();
  
  
 }
 
}

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9474419/viewspace-478448/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: JAVA实现二叉树
下一篇: JAVA实现Stack
请登录后发表评论 登录
全部评论

注册时间:2008-10-09

  • 博文量
    18
  • 访问量
    16448