zoukankan      html  css  js  c++  java
  • 数据结构与算法1

    计算机科学 信息表示 信息处理

    数据结构中常用的概念和术语

    数据

    数据是信息的载体,是能够被计算机识别,存储,计算(处理)的符号集合是计算机处理的对象的总称

    数据元素

    数据元素也称为结点,是组成数据的基本单位

    数据项

    数据是数据的最小单位

    数据对象

    具有相同特征的数据元素的集合,是数据的子集。

    数据结构

    计算机存储,组织数据的方式。主要包括

    逻辑结构

    从逻辑关系伞描述数据,与数据存储无关,且独立语言。

    (1)线性结构 (指有且有一个开始节点和一个终端节点,内部节点有且仅有一个前驱点和一个后继节点)

    (2)非线性结构

    存储结构

    数据运算

    算法

    算法是为求解一个问题需要遵循的,被清晰指定的简单指令集合。

    算法的特点

    待处理问题的相关信息作为输入数据

    对与一个既定的合法输入,多次执行同一个算法总是返回同一个结果

    算法中的指令都是可行的,并在有效的时间内完成。

    算法中指令数量是有限的,即在有限时间内,算法可以正确结束。

    算法执行完毕,能够正确输出的数据集合

    算法分析

    目的:选择一个合适的算法

    一个算法的好坏是从复杂度来衡量的

    复杂度分为时间复杂度和空间复杂度

    时间复杂度 如果问题规模是n则时间复杂的度是关于n的函数记作T(n)=O(f(n)

    )  算法的执行时间=算法循环次数X操作执行的时间。

    线性表功能呢接口定义

     public interface LinearList {
     boolean isEmpty();//判断线性表是否为空
     int size();//返回线性表元素数量
     Object get(int index);//返回索引为index的元素
     Object set(int index,Object element);//用指定元素替换线性表中指定的元素
     boolean add(int index,Object element);//在线性表指定位置插入指定元素
     boolean add(Object element);//向线性标的尾部添加元素
     Object remove (int index);//移除线性表中指定的元素
     void clear();  //从线性表中移除所有元素
    }

    实现类

      1 package com.ls;
      2 
      3 public class LIst implements LinearList {
      4     private Object[] sList; // 实际存储线性表元素的容器
      5     private int size; // 实际存储元素的数量
      6 
      7     
      8     
      9     public LIst(int lenght) {
     10         if (lenght < 0) {
     11             sList = new Object[10];
     12         } else {
     13             sList = new Object[lenght];
     14         }
     15     }
     16 
     17     public LIst() {
     18         this(10);
     19     }
     20     
     21     /** 
     22      * @see com.ls.LinearList#isEmpty()
     23      */
     24     public boolean isEmpty() {
     25 
     26         return size==0;
     27     }
     28 
     29     public int size() {
     30 
     31         return size;
     32     }
     33 
     34     /**
     35      * 检查索引值的有效性(用于向指定位置插入元素)
     36      */
     37     public void checkIndexForAdd(int index){
     38         if (index<0||index>size ) {
     39             throw new IndexOutOfBoundsException(" Index "+index+" Size "+size); 
     40             } 
     41     }
     42     public void checkIndex(int index){
     43         if (index>=size ) {
     44             throw new IndexOutOfBoundsException(" Index "+index+" Size "+size); 
     45             } 
     46     }
     47     
     48     
     49     /** 
     50      * @see com.ls.LinearList#get(int)
     51      */
     52     public Object get(int index) {
     53         checkIndex(index);
     54         return sList[index];
     55     }
     56 
     57     /** 
     58      * 用指定元素替换线性表中指定的元素
     59      */
     60     public Object set(int index, Object element) {
     61         checkIndex(index);
     62         Object old=sList[index];
     63         sList[index]=element;
     64         return old;
     65     }
     66 
     67     /** 
     68      * 在线性表指定位置插入指定元素
     69      */
     70     public boolean add(int index, Object element) {
     71         //检查索引有效性
     72         checkIndexForAdd(index);
     73         //判断容器是否还有容量
     74         if (size==sList.length) {   //没有容量
     75             Object[] temp=sList;//  将线性表保存到一个临时数组中 
     76             this.sList=new Object[temp.length*2]; //重新创建一个大数组
     77             
     78             for (int i=0;i<=temp.length;i++) { //将临时数组中的值取出放到大数组中
     79                 this.sList[i]=temp[i];
     80             }
     81         } 
     82             //元素后移
     83             for(int i=size-1;i>=index;i--){
     84                 sList[i+1]=this.sList[i];                
     85             }        
     86             sList[index]=element;
     87                 size++;
     88         return true;
     89     }
     90 
     91     /** 
     92      * @see com.ls.LinearList#add(java.lang.Object)
     93      */
     94     public boolean add(Object element) {
     95         
     96         return add(size,element);
     97     }
     98 
     99     /** 
    100      * @see com.ls.LinearList#remove(int)
    101      */
    102     public Object remove(int index) {
    103         //检查索引有效性
    104          checkIndex(index);
    105          Object old=sList[index];
    106         //元素前移
    107         for(int i=index;i<size-1;i++){
    108             sList[i]=this.sList[i+1];                
    109         }
    110         sList[size--]=null;//清空最后一个位置
    111         return old;//返回删除的元素
    112     }
    113 
    114     /**
    115      * 从线性表中移除所有元素
    116      */
    117     public void clear() {        
    118         if (size != 0) {            
    119             for (int i = 0; i < size; i++) {
    120                 this.sList[i]=null;
    121                 size=0;
    122             }
    123         } 
    124     }
    125 
    126 }
    线性表的创建

    单链表也叫线性链表 有存储数据的的数据域nodeValue和指向下一个节点的指针域next

     
     
     
     
  • 相关阅读:
    hive数据类型
    hive类型转化错误,会错误提示指定分区参数
    [ORACLE]java.sql.SQLRecoverableException: IO Error: Connection rese
    oracle start with connect by prior 递归查询用法
    redis参数改进建议
    Android Studio教程--从Github 下载一个Repository
    Android Studio教程--Android项目分享到Github
    Android Studio Gradle Build Running 特别慢的问题探讨
    The Genymotion Virtual device could not obtain an IP address解决办法
    Android Studio教程--给Android Studio安装Genymotion插件
  • 原文地址:https://www.cnblogs.com/wanghongjie/p/4700832.html
Copyright © 2011-2022 走看看