算法开头1--数据结构(数组和链表)

1. 基础-区分数组和链表

  1. 对数组和链表的优缺点
数据结构优点缺点
数组通过索引可以直接访问任意元素在初始化就需要知道元素的数量
链表使用的空间大小和元素数量成正比需要通过引用访问任意元素
  1. 对于常用的数据结构的举例
数据结构抽象数据类型数据表示
父链接树UnionFind整形数组
二分查找树BST含有两个链接的结点
字符串String数组、偏移量和长度
散列表(拉链法)SeparateChainingHashST链表数组
散列表(线性探测法)LinearProbingHashST两个对象数组
图的邻接链表GraphBag对象的数组
单词查找树TrieST含有链接数组的结点
三向单词查找树TST含有三个链接的结点

1. 更常用的数据结构了

栈,链表,包

1. 栈
  1. 栈是后进先出 通常也是可说下压栈
  2. 在java中可以使用定义好的栈Stack,或者你自己可以实现适合自己的问题的栈
  3. 栈可以通过链表和数组都可以实现,这里是通过链表实现的
package com.basic.example;

import javax.xml.soap.Node;

/**
 * Created by IntelliJ IDEA.
 *
 * @version : 1.0
 * @auther : Firewine
 * @mail : 1451661318@qq.com
 * @Program Name: StackExam .java
 * @Create : 2019-02-23-15:22
 * @Description : 利用链表实现
 */
public class StackExam<Item> {
    //栈顶
    private Node first;

    //元素数量
    private int N;

    private class Node{
        //定义了节点的嵌套类
        Item item;
        Node next;
    }

    public boolean isEmpty(){return first == null;}
    public int size(){return  N;}
    public void push(Item item){
        //添加元素
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N++;
    }
    public Item pop(){
        //栈顶删除元素
        Item item = first.item;
        first = first.next;
        N--;
        return item;
    }
}
2. 队列
  1. 通常队列是 先进先出
  2. 在java中可以通过Queue来进行新建一个队列
  3. 下面是通过链表实现的队列
package com.basic.example;

import javax.xml.soap.Node;

/**
 * Created by IntelliJ IDEA.
 *
 * @version : 1.0
 * @auther : Firewine
 * @mail : 1451661318@qq.com
 * @Program Name: QueueExam .java
 * @Create : 2019-02-23-15:29
 * @Description :
 */
public class QueueExam <Item>{

    //最早添加的结点的连接
    private Node first ;

    //指向最近添加的结点的连接
    private Node last;

    // 队列中的元素数量
    private int N;

    private class Node{
        //定义了节点的嵌套类
        Item item;
        Node next;

    }
    public boolean isEmpty(){return first == null;}
    public int size(){
        return N;
    }

    public void enqueue(Item item){
        Node oldlast = last;
        last.item = item;
        last.next = null;
        if (isEmpty()){
            first = last;
        }else {
            oldlast.next = last;
        }
    }
    public Item dequeue(){
        Item item = first.item;
        first = first.next;
        if (isEmpty()){
            last =null;
        }
        N--;
        return item;
    }
}
3. 背包
  1. 在java中Bag可以创建
  2. 背包与前面两个不太一样,
    1. 首先 他不能够从中删除元素,
    2. 背包的目的是:帮助用例收集并迭代遍历所有收集 到的元素
  3. 重要的背包对于处理顺序是不重要的。,
  4. 下面是实现背包供学习使用
package com.basic.example;


import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 *
 * @version : 1.0
 * @auther : Firewine
 * @mail : 1451661318@qq.com
 * @Program Name: BagExam .java
 * @Create : 2019-02-23-15:37
 * @Description :
 */
public class BagExam<Item>  implements Iterable<Item> {

    //链表的首节点
    private Node first;

    private class Node{
        Item item;
        Node next;
    }
    public void add(Item item){
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }
    @Override
    public Iterator<Item> iterator() {
        return new ListIterator1();
    }
    private class ListIterator1 implements Iterator<Item>{
        private Node current = first;

        @Override
        public boolean hasNext() {
            return current != null;
        }
        @Override
        public void remove(){}

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}

转载于:https://www.cnblogs.com/YJBlog/p/10440696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值