• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【数据结构和算法】【体系化学习】-线性数据结构之-线性表(数组,链表,栈,队列)

一、概念

 

二、双栈

 

三、栈

4.1、栈-队列的实现方式

public class ArrayStack<T> {
    /**
     * topIndex=0,作为1个栈空的判断临界条件
     * topIndex=1,表示放入栈中的第一个元素的位置
     */
    private int topIndex;
    private int maxLength;
    private Object[] datas;

    public ArrayStack(int length){
        this.topIndex=0;
        this.maxLength=length;
        this.datas=new Object[this.maxLength];
    }

    /**
     * 放入一个元素
     * @param data
     * @param <T>
     */
    public <T> void putData(T data){
        //step1:判断是否栈满
        if(topIndex==maxLength-1){
            throw new RuntimeException("栈满");
        }else{
            //step2:将元素放入栈中
            topIndex++;
            datas[topIndex]=data;
        }
    }

    /**
     * 取出一个元素
     * @return
     */
    public  T popData(){
        //step1:判断栈是不是已空
        if(this.topIndex==0){
            throw new RuntimeException("栈空");
        }else{
            //step2:从栈中取出元素
            Object result=datas[topIndex];
            datas[topIndex]=null;
            topIndex--;
            return (T) result;
        }
    }


    public static void main(String[] args) {
        ArrayStack<String> arrayStack=new ArrayStack<>(5);
        arrayStack.putData("sxf");
        arrayStack.putData("sxq");
        arrayStack.putData("chn");
        arrayStack.putData("sxs");
        try {
            arrayStack.putData("123");
        }catch (Exception e){
            System.out.println(e);
        }
        System.out.println(arrayStack.popData());
        System.out.println(arrayStack.popData());
        System.out.println(arrayStack.popData());
        System.out.println(arrayStack.popData());
        try {
            System.out.println(arrayStack.popData());
        }catch (Exception e){
            System.out.println(e);
        }

    }
}
View Code

4.2、栈-链表的实现方式

public class LinkedStack<T> {
    /**
     * 头指针,第一个节点为标记节点(value=null)
     */
    private Node headNode;
    private int nodeCount;

    public LinkedStack(){
        this.headNode=new Node(null);
        this.nodeCount=0;
    }

    /**
     * 放入一个元素入栈
     * @param data
     * @param <T>
     */
    public <T> void putData(T data){
        //step1:形成一个node;
        Node node=new Node(data);
        //step2:加入栈中
        node.next=headNode.next;
        headNode.next=node;
    }

    /**
     * 从栈中弹出一个元素
     * @return
     */
    public T popData(){
        //step1:判断是否栈空
        if(headNode.next==null){
            throw new RuntimeException("栈空");
        }
        //step2:出栈
        Node result = headNode.next;
        headNode.next=result.next;
        return (T) result.value;
    }

    private static class Node{
        protected Object value;
        protected Node next;

        public Node(Object obj){
            this.value=obj;
        }
    }


    public static void main(String[] args) {
        LinkedStack<String> linkedStack=new LinkedStack<>();
        linkedStack.putData("sxf");
        linkedStack.putData("sxq");
        linkedStack.putData("chn");
        linkedStack.putData("sxs");

        System.out.println(linkedStack.popData());
        System.out.println(linkedStack.popData());
        System.out.println(linkedStack.popData());
        System.out.println(linkedStack.popData());
        try {
            System.out.println(linkedStack.popData());
        }catch (Exception e){
            System.out.println("栈空");
        }

    }
}
View Code

四、队列

4.1、队列-数组的实现方式

public class MyArrayQueue<T> {

    /**
     * 队列长度
     */
    private int queueLength;
    /**
     * 队列头指针
     */
    private int headPointer;
    /**
     * 队列尾指针
     */
    private int tailPointer;
    /**
     * 队列的数据个数
     */
    private int dataCount;

    private Object[] data;

    public MyArrayQueue(int queueLength) {
        if (queueLength <= 0) {
            throw new IllegalArgumentException("队列长度不合法");
        }
        //队列存放元素的个数+1,是队列的真正长度。
        //队列的头指针,指向的位置是不能存储元素的。 原因:环形数组,两个指针便于计算
        this.queueLength = queueLength+1;
        this.headPointer = this.tailPointer = 0;
        this.data = new Object[queueLength+1];
    }

    /**
     * 队列是否满了
     *
     * @return
     */
    public boolean isQueueFull() {
        //头节点指针+1 == 尾巴节点指针
        return (this.headPointer + 1) % queueLength == this.tailPointer;
    }

    /**
     * 队列是否空了
     *
     * @return
     */
    public boolean isQueueEmpty() {
        //头节点指针==尾巴节点指针
        return this.headPointer == this.tailPointer;
    }

    /**
     * 队列的数据长度
     * @return
     */
    public int queueDataLength(){
        return this.dataCount;
    }

    /**
     * 向队列中放一个元素(放在尾巴节点上)
     *
     * @param data
     * @param <T>
     */
    public <T> void putData(T data) {
        //step1:判断队列是否满了
        if ((this.tailPointer + 1) % queueLength == this.headPointer) {
            throw new RuntimeException("队列已满");
        }
        //step2:计算元素存放的位置
        this.tailPointer = (this.tailPointer + 1) % queueLength;
        this.data[tailPointer] = data;
        this.dataCount++;
    }

    /**
     * 从队列中取一个元素(从头节点上趣元素)
     *
     * @return
     */
    public T popData() {
        //step1:判断队列是否为空
        if (this.tailPointer == this.headPointer) {
            throw new RuntimeException("队列已空");
        }
        //step2:计算取元素的下标
        this.headPointer = (this.headPointer + 1) % queueLength;
        Object obj = this.data[this.headPointer];
        this.data[this.headPointer] = null;
        this.dataCount--;
        return (T) obj;
    }


    public static void main(String[] args) {
        MyArrayQueue<String> myArrayQueue = new MyArrayQueue<>(5);
        myArrayQueue.putData("sxf");
        myArrayQueue.putData("sxq");
        myArrayQueue.putData("chn");
        myArrayQueue.putData("sxs");
        myArrayQueue.putData("sxy");

        try {
            myArrayQueue.putData("null");
        } catch (Exception e) {
            System.out.println(e);
        }

        System.out.println(myArrayQueue.popData());
        System.out.println(myArrayQueue.popData());
        System.out.println(myArrayQueue.popData());
        System.out.println(myArrayQueue.popData());
        System.out.println(myArrayQueue.popData());

        try {
            System.out.println(myArrayQueue.popData());
        } catch (Exception e) {
            System.out.println(e);
        }

    }

}
View Code

4.2、队列-链表的实现方式

public class MyLinkedQueue {

    private static final Node tmp = new Node(null);

    /**
     * 队列的头节点
     */
    private Node headNode;
    /**
     * 队列的尾巴节点
     */
    private Node tailNode;
    /**
     * 队列的数据元素个数
     */
    private int dataCount;

    /**
     * 队列的初始化,头尾节点,都指向一个空节点
     */
    public MyLinkedQueue() {
        this.dataCount = 0;
        this.headNode = tmp;
        this.tailNode = tmp;
        this.headNode.nextNode = null;
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean queueEmpty() {
        return this.headNode == this.tailNode;
    }

    /**
     * 队列中的元素个数
     *
     * @return
     */
    public int queueSize() {
        return this.dataCount;
    }

    /**
     * 向队列中放入元素
     *
     * @param object
     */
    public void putData(Object object) {
        //step1:生成队列节点
        Node node = new Node(object);
        //step2:当前尾巴节点的下一个节点指向新加入的节点
        this.tailNode.nextNode = node;
        //step3:将新加入的节点变成最新的尾巴节点
        this.tailNode = node;
        this.dataCount++;
    }

    /**
     * 从队列中取出元素
     *
     * @return
     */
    public Object popData() {
        //step1:判断队列是否为空
        if(this.headNode==this.tailNode){
            throw new RuntimeException("队列为空");
        }
        //step2:取队头的下一个节点
        Node node=this.headNode.nextNode;
        //step3:将要弹出队列的下一个元素,赋值给头节点的下一个元素
        this.headNode.nextNode=node.nextNode;
        //step4:判断当前头节点指向的下一个元素是否为空
        if(this.headNode.nextNode==null){
            //表示队列已空,则重新设置队尾元素和队头元素,指向同一个对象
            this.tailNode=this.headNode;
        }
        //元素个数减1个
        this.dataCount--;
        return node.value;
    }

    private static class Node {
        protected Object value;
        protected Node nextNode;

        public Node(Object value) {
            this.value = value;
        }
    }


    public static void main(String[] args) {
        MyLinkedQueue myLinkedQueue=new MyLinkedQueue();
        myLinkedQueue.putData("sxf");
        myLinkedQueue.putData("sxq");
        myLinkedQueue.putData("chn");

        System.out.println(myLinkedQueue.popData());
        System.out.println(myLinkedQueue.popData());
        System.out.println(myLinkedQueue.popData());


        System.out.println(myLinkedQueue.popData());
    }
}
View Code

 

五、数组

5.1、对称矩阵

5.2、三角矩阵

5.3、稀疏矩阵

 

posted on 2021-08-28 18:35  无信不立  阅读(54)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3