java基础--数据结构

 

数据结构

一逻辑结构

    1.集合机构:集合间数据没有关系

    2.线性结构 :元素之间一对一的关系

    3.树形结构 :元素之间一对多的关系

    4.图形结构:元素之间是多对多的关系

二物理结构:存储器/内存

    顺序存储结构:是把数据元素存放在地址连续的存储单元里。例如数组,查询快,添加删除慢,因为如果在中间插入和删除的话,需要改变每个值的位置

    链式存储结构 : 是把数据元素存放在任意的存储单元里,可以连续,也可以不连续,例如 队列(除了存放数据还要多存一个指针),查询慢,添加删除快,因为需根据第一个值得索引一直找到最后一个元素的索引,

    单链表

地址是指下一个元素的引用(指针)

节点包括(元素和下一个元素的地址引用)

 

度量一个算法的执行时间

     时间复杂度   O(1)  O(n) O(n^2)

                   常用的时间复杂度所耗费的时间从小到大依次是:O(1)<O(logn)<O(n)<O(nlogn)<O(n^2)<O(n^3)<O(2^n)<O(n!)<O(n^n)

     空间复杂度  

 

package com.tyxh.link;
//节点类
public class Node {
     protected Node next; //指针域
     protected int data;//数据域
     
     public Node( int data) {
           this. data = data;
     }
     
     //显示此节点
     public void display() {
          System. out.print( data + " ");
     }
}

package com.tyxh.link;
//单链表
public class LinkList {
     public Node first; // 定义一个头结点
     private int pos = 0;// 节点的位置

     public LinkList() {
           this. first = null;
     }

     // 插入一个头节点
     public void addFirstNode( int data) {
          Node node = new Node(data);
          node. next = first;
           first = node;
     }

     // 删除一个头结点,并返回头结点
     public Node deleteFirstNode() {
          Node tempNode = first;
           first = tempNode. next;
           return tempNode;
     }

     // 在任意位置插入节点 在index的后面插入
     public void add(int index, int data) {
          Node node = new Node(data);
          Node current = first;
          Node previous = first;
           while ( pos != index) {
              previous = current;
              current = current. next;
               pos++;
          }
          node. next = current;
          previous. next = node;
           pos = 0;
     }

     // 删除任意位置的节点
     public Node deleteByPos( int index) {
          Node current = first;
          Node previous = first;
           while ( pos != index) {
               pos++;
              previous = current;
              current = current. next;
          }
           if(current == first) {
               first = first. next;
          } else {
               pos = 0;
              previous. next = current. next;
          }
           return current;
     }

     // 根据节点的data删除节点(仅仅删除第一个)
     public Node deleteByData( int data) {
          Node current = first;
          Node previous = first; //记住上一个节点
           while (current. data != data) {
               if (current. next == null) {
                    return null;
              }
              previous = current;
              current = current. next;
          }
           if(current == first) {
               first = first. next;
          } else {
              previous. next = current. next;
          }
           return current;
     }

     // 显示出所有的节点信息
     public void displayAllNodes() {
          Node current = first;
           while (current != null) {
              current.display();
              current = current. next;
          }
          System. out.println();
     }

     // 根据位置查找节点信息
     public Node findByPos( int index) {
          Node current = first;
           if ( pos != index) {
              current = current. next;
               pos++;
          }
           return current;
     }

     // 根据数据查找节点信息
     public Node findByData( int data) {
          Node current = first;
           while (current. data != data) {
               if (current. next == null)
                    return null;
              current = current. next;
          }
           return current;
     }
}

package com.tyxh.link;
//测试类
public class TestLinkList {
     public static void main(String[] args) {
          LinkList linkList = new LinkList();
          linkList.addFirstNode(20);
          linkList.addFirstNode(21);
          linkList.addFirstNode(19);
           //19,21,20
          linkList.add(1, 22); //19,22,21,20
          linkList.add(2, 23); //19,22,23,21,20
          linkList.add(3, 99); //19,22,23,99,21,20
          linkList.displayAllNodes();
//        Node node = linkList.deleteFirstNode();
//        System.out.println("node : " + node.data);
//        linkList.displayAllNodes();
//        node = linkList.deleteByPos(2);
//        System.out.println("node : " + node.data);
//        linkList.displayAllNodes();
//        linkList.deleteFirstNode();
          Node node = linkList.deleteByData(19);
//        Node node = linkList.deleteByPos(0);
          System. out.println( "node : " + node. data);
          linkList.displayAllNodes();
          Node node1 = linkList.findByPos(0);
          System. out.println( "node1: " + node1. data);
          Node node2 = linkList.findByData(22);
          System. out.println( "node2: " + node2. data);
     }
}

 双向链表

 

/**
 * 双向链表
 * 对象套对象,通过第一个对象一直往后找
 * @author Administrator
 *
 */
public class TjLinkList {
    
    private int size;
    
    private Node first;
    
    private Node last;
    
    //添加
    public void add(Object object){
        if(first==null){
            Node node = new Node();
            node.setPrevious(null);
            node.setObject(object);
            node.setNext(null);
            //第一个节点既是第一个也是最后一个
            first = node;
            last = node;
        }else{
            //往last后面加
            Node node = new Node();
            //当前节点的上一个节点是最后一个节点
            node.setPrevious(last);
            node.setObject(object);
            node.setNext(null);
            //最后一个节点的下一个节点是当前节点
            last.setNext(node);
            //把当前节点至成最后一个节点
            last = node;
        }
        size++;
    }
    
    //获取节点的值
    public Object get(int index){
            Node temp = null;
            if(first!=null){
                temp = first;
                for (int i = 0; i < index; i++) {
                    temp = temp.next;
                }
            }
            return temp.getObject();
    }
    
    //移除节点
    public void remove(int index){
        //先找到那个节点
        Node temp = null;
        if(first!=null){
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
        }
        if(temp!=null){
            Node pre = temp.previous;
            Node next = temp.next;
            //注意先后顺序,先把temp上一个节点的下一个节点改成temp的下一个节点
            pre.next = next;
            //再把temp下一个节点的上一个节点改成temp的上一个节点
            next.previous = pre;
            size--;
        }
    }
    
    //插入节点
    public void add(int index,Object object){
        //首先获得原来那个索引下的节点
        Node temp = null;
        if(first!=null){
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
        }
        
        if(temp!=null){
            //新节点
            Node newNode = new Node();
            newNode.object = object;
            Node pre = temp.previous;
            pre.next = newNode;
            
            newNode.previous = temp;
            temp.previous = newNode;
        }
    }
    
    public int size(){
        return this.size;
    }

    public Node getFirst() {
        return first;
    }

    public void setFirst(Node first) {
        this.first = first;
    }

    public Node getLast() {
        return last;
    }

    public void setLast(Node last) {
        this.last = last;
    }

    public static void main(String[] args) {
        TjLinkList linkList = new TjLinkList();
        linkList.add("aaa");
        linkList.add("bbb");
        System.out.println(linkList.size());
        System.out.println(linkList.get(0));
        System.out.println(linkList.get(1));
    }

}


class Node{
    
    Node previous;
    Object object;
    Node next;
    public Node() {
        super();
    }
    
    public Node getPrevious() {
        return previous;
    }

    public void setPrevious(Node previous) {
        this.previous = previous;
    }

    public Object getObject() {
        return object;
    }
    public void setObject(Object object) {
        this.object = object;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    
}

 

 

 

   

   

  

   

    

 

         

posted @ 2016-11-12 23:31  jentary  阅读(302)  评论(0编辑  收藏  举报