真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

數(shù)據(jù)結(jié)構(gòu)和算法——基于Java——4.1棧(數(shù)組實(shí)現(xiàn)棧、鏈表實(shí)現(xiàn)棧)-創(chuàng)新互聯(lián)

理論補(bǔ)充

在這里插入圖片描述

公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出興縣免費(fèi)做網(wǎng)站回饋大家。

先進(jìn)后出 FILO(First-Input-Last-Out)的有序列表,限制線性表中元素的插入和刪除只能在線性表的同一端進(jìn)行

  • 棧頂:變化的一端
  • 棧底:固定的一端
代碼實(shí)現(xiàn) 2.1 數(shù)組模擬棧
package com.b0.stack;

import java.util.Scanner;

public class ArrayStackDemo {public static void main(String[] args) {ArrayStack stack = new ArrayStack(5);
        String key = "";
        boolean loop = true;//控制是否退出菜單
        Scanner scanner = new Scanner(System.in);
        while (loop){System.out.println("show:表示顯示棧");
            System.out.println("exit:退出程序");
            System.out.println("push:入棧");
            System.out.println("pop:出棧");
            System.out.println("請(qǐng)輸入你的選擇");
            key = scanner.next();
            switch (key){case "show":
                    stack.list();
                    break;
                case "push":
                    System.out.println("請(qǐng)輸入一個(gè)數(shù)");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try {int res = stack.pop();
                        System.out.println("出棧數(shù)據(jù)是:"+res);
                    }catch (Exception e){System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~");
    }
}
class ArrayStack{private int maxSize;//棧的大小
    private int[] stack;//數(shù)組模擬棧
    private int top = -1;//棧頂,初始化為-1
    //構(gòu)造器
    public ArrayStack(int maxSize) {this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //判斷棧滿
    public boolean isFull(){return top == maxSize-1;
    }
    //???    public boolean isEmpty(){return top == -1;
    }
    //入棧
    public void push(int num){//判斷是否棧滿
        if (isFull()){System.out.println("棧滿");
            return;
        }
        top++;
        stack[top] = num;
    }
    //出棧
    public int pop(){//判斷是否???        if (isEmpty()){//拋出異常
            throw new RuntimeException("棧空,沒有數(shù)據(jù)!");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //遍歷棧
    public void list(){if (isEmpty()){System.out.println("???!");
            return;
        }
        while (top != -1){//從棧頂開始遍歷
            System.out.println(stack[top]);
            top--;
        }
    }
}
2.2 鏈表模擬棧(頭插法)

在這里插入圖片描述

package com.b0.stack;

public class LinkedStackDemo {public static void main(String[] args) {LinkedStack linkedStack = new LinkedStack();
        linkedStack.push(1);
        linkedStack.push(2);
        linkedStack.push(3);
        linkedStack.push(4);
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
    }
}
class LinkedStack{//頭節(jié)點(diǎn)
    Node head = new Node(0);
    //???尾插法
    public boolean isEmpty(){return head.next == null;
    }
    //入棧,頭插法
    public void push(int num){Node node = new Node(num);
        if (isEmpty()){head.next = node;
        }
        node.next = head.next;
        head.next = node;
    }
    public int pop(){if (isEmpty()){//拋出異常
            throw new RuntimeException("???,沒有數(shù)據(jù)!");
        }
        //定義零時(shí)變量,不修改head指針指向
        Node cur = head;
        int value = cur.next.no;
        cur.next = cur.next.next;
        return value;
    }
}
class Node{public int no;
    public Node next;

    public Node(int no) {this.no = no;
    }
}
2.3 鏈表模擬棧(尾插法,不推薦)
package com.b0.stack;

public class LinkedStackDemo {public static void main(String[] args) {LinkedStack linkedStack = new LinkedStack();
        linkedStack.push(1);
        linkedStack.push(2);
        linkedStack.push(3);
        linkedStack.push(4);
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
        System.out.println("出棧值為:"+linkedStack.pop());
    }
}
class LinkedStack{//頭節(jié)點(diǎn)
    Node head = new Node(0);
    //???    public boolean isEmpty(){return head.next == null;
    }
    //入棧
    public void push(int num){Node node = new Node(num);
        Node cur = head;
        while (true){if (cur.next == null)break;
            cur = cur.next;
        }
        cur.next = node;
    }
    //出棧
    public int pop(){if (isEmpty()){//拋出異常
            throw new RuntimeException("???,沒有數(shù)據(jù)!");
        }
        //定義零時(shí)變量,不修改head指針指向
        Node cur = head;
        int value;
        while (true){if (cur.next.next == null){value = cur.next.no;
                cur.next = null;
                break;
            }
           cur = cur.next;
        }
        return value;
    }
}
class Node{public int no;
    public Node next;

    public Node(int no) {this.no = no;
    }
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧


網(wǎng)站欄目:數(shù)據(jù)結(jié)構(gòu)和算法——基于Java——4.1棧(數(shù)組實(shí)現(xiàn)棧、鏈表實(shí)現(xiàn)棧)-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://weahome.cn/article/digpse.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部