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

來源:互聯網
上載者:User

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

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

順序儲存:

  

 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.