Chinaunix首页 | 论坛 | 博客
  • 博客访问: 612803
  • 博文数量: 96
  • 博客积分: 1464
  • 博客等级: 上尉
  • 技术积分: 1539
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-12 23:24
文章分类

全部博文(96)

文章存档

2013年(29)

2012年(53)

2011年(14)

分类: Java

2013-03-08 23:08:12

有一段时间没看数据结构了,导致链表有些生疏了,特别是java链表,改怎么写了···
额,好吧,看来以前是没学懂啊,不然怎么会忘了,再来一次

点击(此处)折叠或打开

  1. public class Node {
  2.     private Object data;
  3.     private Node next;
  4.     
  5.     public Node(Object data,Node next){
  6.         this.data = data;
  7.         this.next = next;
  8.     }
  9.     
  10.     public Object getData(){
  11.         return this.data;
  12.     }
  13.     
  14.     public void setData(Object data){
  15.         this.data = data;
  16.     }
  17.     
  18.     public Node getNext(){
  19.         return this.next;
  20.     }
  21.     
  22.     public void setNext(Node next){
  23.         this.next = next;
  24.     }
  25. }

点击(此处)折叠或打开

  1. public class List {
  2.     private Node head;
  3.     private int length;
  4.     
  5.     public List(){
  6.         head = new Node(null,null);
  7.         length = 0;
  8.     }
  9.     
  10.     public Node getHead(){
  11.         return this.head;
  12.     }
  13.     
  14.     public void addHead(Object data){
  15.         Node node = new Node(data,null);
  16.         node.setNext(head.getNext());
  17.         head.setNext(node);
  18.         length++;
  19.     }
  20.     
  21.     public void addTail(Object data){
  22.         Node node = new Node(data,null);
  23.         Node temp = head;
  24.         while(null != temp.getNext()){
  25.             temp = temp.getNext();
  26.         }
  27.         temp.setNext(node);
  28.         length++;
  29.     }
  30.     
  31.     public void addByIndex(Object data,int index){
  32.         Node node = new Node(data,null);
  33.         Node temp = head;
  34.         for(int i=0;i<index-1;i++){
  35.             temp = temp.getNext();
  36.         }
  37.         node.setNext(temp.getNext());
  38.         temp.setNext(node);
  39.         length++;
  40.     }
  41.     
  42.     public void deleteByIndex(int index){
  43.         if(index<1 || index>length){
  44.             System.out.println("指定位置不存在");
  45.         }else{
  46.             Node temp = head;
  47.             for(int i=0; i<index-1;i++){
  48.                 temp = temp.getNext();
  49.             }
  50.             temp.setNext(temp.getNext().getNext());
  51.             length--;
  52.         }
  53.     }
  54.     
  55.     public void deleteAll(){
  56.         head.setNext(null);
  57.     }
  58.     
  59.     public void findByIndex(int index){
  60.         if(index<1 || index>length){
  61.             System.out.println("指定位置不存在");
  62.         }else{
  63.             Node temp = head;
  64.             for(int i=0;i<index-1;i++){
  65.                 temp = temp.getNext();
  66.             }
  67.             System.out.println("位置"+index+"的元素是:"+temp.getNext().getData());
  68.         }
  69.     }
  70.     
  71.     public void print(){
  72.         Node temp = head.getNext();
  73.         for(int i=0;i<length;i++){
  74.             System.out.print(" "+temp.getData());
  75.             temp = temp.getNext();
  76.         }
  77.     }
  78.     
  79.     public void lengthPP(){
  80.         this.length++;
  81.     }
  82. }


点击(此处)折叠或打开

  1. public class Instance {
  2.     public static void main(String[] args){
  3.         //造一个随机数组
  4.         int length = 9;
  5.         int[] array = new int[length];
  6.         array[length-2]=7;
  7.         array[length-1]=7;
  8.         for(int i=0;i<length-3;i++){
  9.             array[i] = (int) (Math.random()*10);
  10.         }
  11.         
  12.         for(int i=0;i<length;i++){
  13.             System.out.print(array[i]+" ");
  14.         }
  15.         System.out.println();
  16.         
  17.         //找出随机数组中相等的数
  18.         for(int i=0;i<length;i++){
  19.             for(int j=0;j<length;j++){
  20.                 if((i < j) && (array[i] == array[j]))
  21.                     System.out.println(i+":"+array[i]+"等于 "+j+":"+array[j]);
  22.             }
  23.         }
  24.         
  25.         //使用单链表对随机数组进行排序
  26.         List list = new List();
  27.         Node head = list.getHead();
  28.         list.addHead(array[0]);
  29.         Node temp = head;
  30.         for(int i=1;i<length;i++){
  31.             temp = head;
  32.             while(array[i] > Integer.valueOf(temp.getNext().getData().toString())){
  33.                 temp = temp.getNext();
  34.                 if(temp.getNext()==null)
  35.                     break;
  36.             }
  37.             if(temp.getNext() == null){
  38.                 temp.setNext(new Node(array[i],null));
  39.             }else{
  40.                 Node node = new Node(array[i],null);
  41.                 node.setNext(temp.getNext());
  42.                 temp.setNext(node);
  43.             }
  44.             list.lengthPP();
  45.         }
  46.         
  47.         list.print();
  48.         
  49.     }
  50. }









阅读(1186) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~