欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

理論補充

在這里插入圖片描述

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

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

  • 棧頂:變化的一端
  • 棧底:固定的一端
代碼實現(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("請輸入你的選擇");
            key = scanner.next();
            switch (key){case "show":
                    stack.list();
                    break;
                case "push":
                    System.out.println("請輸入一個數(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é)點
    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ù)!");
        }
        //定義零時變量,不修改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é)點
    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ù)!");
        }
        //定義零時變量,不修改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)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當前文章:數(shù)據(jù)結(jié)構(gòu)和算法——基于Java——4.1棧(數(shù)組實現(xiàn)棧、鏈表實現(xiàn)棧)-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://chinadenli.net/article34/digpse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)外貿(mào)網(wǎng)站建設(shè)手機網(wǎng)站建設(shè)靜態(tài)網(wǎng)站網(wǎng)站收錄軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設(shè)