1. 程式人生 > >數據結構學習筆記(二) 線性表的順序存儲和鏈式存儲

數據結構學習筆記(二) 線性表的順序存儲和鏈式存儲

出錯 初始化 node != test span 輸入 des val

線性表:由同類型數據元素構成有序序列的線性結構
  --》表中元素的個數稱為線性表的長度
  --》沒有元素時,成為空表
  --》表起始位置稱表頭,表結束位置稱表尾

順序存儲:

  

 1 package test;
 2     
 3     /**
 4      * 線性表(數組)
 5      *
 6      */
 7     public class Test {
 8         private static int m ;
 9         private static int[] a;
10         public static void main(String[] args) {
11 int[] a = init(); 12 show(a); 13 System.out.println("元素 2 所在位置:"+query(2,a)); 14 show(a); 15 System.out.println(insert(a,4,10)); 16 show(a); 17 System.out.println(delete(a, 4)); 18 show(a); 19 }
20 /** 21 * 初始化 22 */ 23 public static int[] init(){ 24 a = new int[10]; 25 for (int i = 0; i < 6; i++) { 26 a[i] = i ; 27 m = i; 28 } 29 return a; 30 } 31 32 /** 33
* 查找(返回位置) 34 */ 35 public static int query(int i,int[] a){ 36 for(int j = 0;j < a.length;j++){ 37 if(a[j] == i){ 38 return j; 39 } 40 } 41 return -1; 42 } 43 44 /** 45 * 插入(在i個位置插入),先移動 46 */ 47 public static String insert(int[] a,int i,int value){ 48 //先判斷是否還有位置 49 if(m == a.length-1){ 50 return "沒有位置了"; 51 } 52 //判斷i是否合法 53 if(i < 0 || i>=a.length){ 54 return "位置不合法"; 55 } 56 for (int j = m; j >= i; j--) { 57 a[j+1] = a[j]; 58 } 59 a[i] = value; 60 m++; 61 return "插入元素 10 成功"; 62 } 63 64 /* 65 * 刪除 66 */ 67 public static String delete(int[] a,int i){ 68 //判斷i是否合法 69 if(i < 0 || i>=a.length){ 70 return "位置不合法"; 71 } 72 for (int j = i; j < m; j++) { 73 a[j] = a[j+1]; 74 } 75 m--; 76 return "刪除元素 10 成功"; 77 } 78 79 /** 80 * 展示元素 81 */ 82 public static void show(int a[]){ 83 for (int i = 0; i <= m; i++) { 84 System.out.print(a[i]+" "); 85 } 86 System.out.println(); 87 } 88 }

鏈式存儲

package test;
    
    import java.util.Random;
    
    /**
     * 線性表(單鏈表)
     *
     */
    public class Test {
        public static Node first ;
        public static int m ;
        public static void main(String[] args) {
            initHead();
            show(first);
            if(queryBySequence(first,5)!= null){
                System.out.println("查找 5 序列號的元素"+queryBySequence(first,5).data);
            }else{
                System.out.println("輸入序列號不對");
            }
            
            if(queryByValue(first,58)!= null){
                System.out.println("查找值為58的元素:"+queryByValue(first,58).data);
            }else{
                System.out.println("查找值為58的元素不存在");
            }
            
            System.out.println("插入20"+insert(100,5,first));
            show(first);
            System.out.println(delete(first,5));
            show(first);
            
        }
        /**
         * 初始化
         */
        public static void initHead(){
            Random random = new Random();
            Node node = new Node(random.nextInt(100));
            first = node ;
            m++;
            Node node1= first;
            for (int i = 0; i < 10; i++) {
                node1 = init(node1);
                m++;
            }
        }
        public static Node init(Node node){
            Random random = new Random();
            Node nodes = new Node(random.nextInt(100));
            node.next = nodes;
            return nodes;
        }
        
        
        /**
         * 查找 按序號進行查找
         */
        public static Node queryBySequence(Node node,int k){
            int i = 0;
            while(node != null && i < k-1){
                node = node.next ;
                i++;
            }
            if(i == k-1){
                return node;
            }else{
                return null;
            }
        }
        
        /**
         * 查找 按值進行查找
         */
        public static Node queryByValue(Node node,int k){
            while(node != null && node.data != k){
                node = node.next ;
            }
            return node;
        }
    
        
        /**
         * 插入(在i個位置插入value),先移動
         */
        public static String insert(int value,int i,Node node){
            Node nodes = new Node(20);
            //插入表頭
            if(i == 1){
                nodes.next = first;
                first = nodes;        
                return "插入成功";
            }
            //判斷i-1是否存在
            if(queryBySequence(node,i-1)==null){
                return "插入位置出錯";
            }else{
                nodes.next = queryBySequence(node,i);
                queryBySequence(node,i-1).next = nodes;
                return "插入成功";
            }
        }
    
        
        /**
         * 刪除
         */
        public static String delete(Node node,int i){
            if(i == 1){
                first = first.next;
            }
            if(queryBySequence(node,i) == null){
                return "i 不存在";
            }else{
                Node node1 = queryBySequence(node,i).next;
                queryBySequence(node,i-1).next = node1;
                return "刪除成功";
            }
        }
        
        /**
         * 展示元素
         */
        public static void show(Node first){
            Node node = first;
            while(node != null){
                System.out.print(node.data+"  ");
                node = node.next;
            }
            System.out.println();
        }
    }

ps:Node類

  

package test;

public class Node {
    public int data ;
    public  Node next ;
    public Node(int data) {
        this.data = data;
    }
    
}

分治算法的時間復雜度:
  T(N) = 2T(N/2) + cN
  = 2[2T(N/2^2)] +cN/2)+cN
  = 2^kO(1) + ckN 其中N/2^k = 1 ==>k=logn(兩項相加時,取最大項)
  = nlogN

數據結構學習筆記(二) 線性表的順序存儲和鏈式存儲