码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构学习笔记(二) 线性表的顺序存储和链式存储

时间:2017-05-22 20:05:17      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:return   imp   query   结束   长度   线性结构   random   seq   and   

线性表:由同类型数据元素构成有序序列的线性结构
  --》表中元素的个数称为线性表的长度
  --》没有元素时,成为空表
  --》表起始位置称表头,表结束位置称表尾

顺序存储:

  

 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

 

数据结构学习笔记(二) 线性表的顺序存储和链式存储

标签:return   imp   query   结束   长度   线性结构   random   seq   and   

原文地址:http://www.cnblogs.com/trashman/p/6891020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!