zoukankan      html  css  js  c++  java
  • 数据结构java实现代码

    双向链表

    public class Node {
     
     Object o;
     Node up;
     Node down;
     public Object getO() {
      return o;
     }
     public void setO(Object o) {
      this.o = o;
     }
     public Node getUp() {
      return up;
     }
     public void setUp(Node up) {
      this.up = up;
     }
     public Node getDown() {
      return down;
     }
     public void setDown(Node down) {
      this.down = down;
     }
     
     
    }
    public class SuperList {
     Node first;
     Node last;
     int size;
     
     public void add(Object o) {
      Node n=new Node();
      n.setO(o);
      
      if(first==null) {
       first=n;
       last=n;
       n.setUp(null);
       n.setDown(null);
       
      }else {
       last.setDown(n);
       n.setUp(last);
       n.setDown(null);
       last=n;
       
      }
      size++;
      
     }
     
     public boolean check(int index) {
      if(index<0||index>size) {
       System.out.println("索引出问题");
       return false;
      }
      return true;
     }
     
     public int getSize() {
      return size;
      
     }
     
     public Node getNode(int index) {
      boolean b=check(index);
      if(b==false) {
       return null;
      }
      Node n=first;
      for(int i=0;i<index;i++) {
       n=n.getDown();
       
      }
      
      return n;
     }
     
     public void remove(int index) {
      Node n=getNode(index);
      n.getUp().setDown(n.getDown());
      n.getDown().setUp(n.getUp());
      
      size--;
      
     }
     
     public void set(int index,Object o) {
      Node n=getNode(index);
      
      n.setO(o);
      
     }
     
     public Object get(int index) {
      return getNode(index).getO();
      
     }
    }
     
     
    顺序栈实现
    public class stack {
     
     Object[] data;
     int top=-1;//栈首指针
     int max;
     
     stack(){
      
     }
     stack(int max){
      this.max=max;
      data=new Object[max];
      
      
     }
     //判断栈是否满
     public boolean check() {
      if(top<max-1) {
       return false;
      }
      return true;
     }
     
     public void push(Object o) {
      if(check()) {
       System.out.println("栈已经满了,无法继续入栈");
       return;
      }
      top+=1;
      data[top]=o;
      
      
     }
     
     public Object pop() {
      if(top==-1) {
       System.out.println("栈为空,无法出栈");
       return null;
      }
      Object ob=peek();
      top-=1;
      return ob;
     }
     
     public Object peek() {
      return data[top];
      
     }
     
     
    }
     
    顺序队列实现
    public class Queue {
     Object[] data;
     int front=0;
     int rear=0;
     int max;
     
     public Queue() {
      
     }
     Queue(int max){
      this.max=max;
      data=new Object[max];
     }
     
     public boolean isEmpty() {
      if(front==rear) {
       return true;
      }else {
       return false;
      }
      
     }
     
     public int size() {
      return rear-front;
      
     }
     
     public boolean isFull() {
      if(size()==max) {
       return true;
      }
      return false;
     }
     
     public Object chudui() {
      if(isEmpty()) {
       System.err.println("对为空,无法出队");
       return null;
      }
      Object o=data[front];
      
      front+=1;
      
      return o;
      
      
     }
     
     public void rudui(Object o) {
      if(isFull()) {
       System.out.println("对已经满了,无法入队");
       return;
      }
      rear+=1;
      
      
      
     }
     
     
     
    }
     
    循环队列实现
     
    public class xunhuanQueue {
     Object[] data;
     int front=0;
     int rear=0;
     int max;
     
     public xunhuanQueue(int max) {
      this.max=max;
      data=new Object[max];
      
     }
     
     public boolean isEmpty() {
      if(rear==front) {
       return true;
      }
      
      return false;
     }
     
     public boolean isFull() {
      if((rear+1)%max==front) {
       return true;
      }
      
      return false;
     }
     
     public int Size() {
      
      return rear-front;
     }
     
     public void rudui(Object o) {
      if(isFull()) {
       System.out.println("队满,无法入队");
       return;
      }
      data[rear]=o;
      rear=(rear+1)%max;
      
      
     }
     
     public void chudui() {
      if(isEmpty()) {
       System.out.println("队空,无法出队");
       return;
      }
      data[front]=null;
      front=(front+1)%max;
      
      
     }
     
    }
     
    二叉排序树实现
     
    public class Node {
     
     int o;
     Node leftChild;
     Node rightChild;
     public int getO() {
      return o;
     }
     public void setO(int o) {
      this.o = o;
     }
     public Node getLeftChild() {
      return leftChild;
     }
     public void setLeftChild(Node leftChild) {
      this.leftChild = leftChild;
     }
     public Node getRightChild() {
      return rightChild;
     }
     public void setRightChild(Node rightChild) {
      this.rightChild = rightChild;
     }
     
     
     
    }
    public class BinaryTree {
     
     Node root;
     
     public void insert(int o) {
      Node n=new Node();
      n.setO(o);
      
      if(root==null) {
       n.setLeftChild(null);
       n.setRightChild(null);
       root=n;
       
      }
      
      insert(root,n);
      
     }
     
     public void insert(Node parent,Node n) {
      if(parent.getO()==n.getO()) {
       return;
      }
      
      if(parent.getO()>n.getO()) {
       if(parent.getLeftChild()==null) {
        parent.setLeftChild(n);
       }
       insert(parent.getLeftChild(),n);
       return;
      }
      
      if(parent.getO()<n.getO()) {
       if(parent.getRightChild()==null) {
        parent.setRightChild(n);
       }
       insert(parent.getRightChild(),n);
       return;
      }
     }
     
     public boolean isEmpty() {
      if(root==null) {
       return true;
       
      }
      return false;
     }
     
     public void XianSearch() {
      if(isEmpty()) {
       System.out.println("树为空,无法遍历");
       return ;
      }
      XianSearch(root);
      
     }
     
     public void XianSearch(Node parent) {
      if(parent!=null) {
       System.out.println(parent.getO());
       XianSearch(parent.getLeftChild());
       XianSearch(parent.getRightChild());
       
      }
      
     }
     public void zhongSearch() {
      if(isEmpty()) {
       System.out.println("树为空无法宾利");
       return;
      }
      zhongSearch(root);
      
     }
     public void zhongSearch(Node parent) {
      if(parent!=null) {
       zhongSearch(parent.getLeftChild());
       System.out.println(parent.getO());
       zhongSearch(parent.getRightChild());
      }
      
      
     }
     public void houSearch() {
      if(isEmpty()) {
       System.out.println("树为空无法i案例");
       return;
      }
      houSearch(root);
      
     }
     public void houSearch(Node parent) {
      if(parent!=null) {
       houSearch(parent.getLeftChild());
       houSearch(parent.getRightChild());
       System.out.println(parent.getO());
       
      }
      
      
     }
     
     public void clear() {
      root=null;
      
     }
     
     public int getHeight() {
      if(isEmpty()) {
       return 0;
      }
      
      return getHeight(root);
      
      
     }
     
     public int getHeight(Node parent) {
      if(parent!=null) {
       
       int l=getHeight(parent.getLeftChild());
       
       int r=getHeight(parent.getRightChild());
       
       return l>r?l+1:r+1;
       
      }
      
      return 0;
      
     }
     public int getNodes() {
      if(isEmpty()) {
       System.out.println("树为空");
      }
      return getNodes(root);
      
      
     }
     
     public int getNodes(Node parent) {
      if(parent!=null) {
       
       int left=getNodes(parent.getLeftChild());
       int right=getNodes(parent.getRightChild());
       
       return left+right+1;
      }
      return 0;
     }
    }
  • 相关阅读:
    [Javascript] twitterbootstrap: 解决2.1版本中modal加载远程内容时,只加载一次的问题
    向大型网站学习SEO优化之道
    [Yii Framework] 使用Yii Framework开发微信公众平台
    [jQuery] ajax跨域处理方式
    [jQuery] form提交到iframe之后,获取iframe里面内容
    [Ubuntu] fatal: recursion detected in die handler
    [Yii Framework] Yii如何实现前后台的session分离
    [ubuntu] ubuntu13.04 64bit,安装FastDFS4.06过程遇到的问题和解决方案
    [转] 怎样在Ubuntu上安装Git服务器
    [ubuntu] ubuntu13.04切换桌面/工作区的方法
  • 原文地址:https://www.cnblogs.com/ttaall/p/12001229.html
Copyright © 2011-2022 走看看