【数据结构】每天五分钟,快速入门数据结构(四)—— 栈和队列

一 栈

1.栈的特点:先进后出

2.定义:栈(Stack)是一种后进先出的线性表结构。它只允许在同一端进行添加或删除元素的操作,这一端叫做“变化端”,也叫做“栈顶”,另一端叫做“固定端”,也叫做“栈底”。

3.时间复杂度:因为删除和插入操作都只在栈顶执行,因此时间复杂度为o(1)。

4.实现一个栈

1)基本思想:

        入栈操作(插入数据):栈顶++,存入数据

        出栈操作(删除数据):数据取出,栈顶--

        栈满:栈顶 == maxSize - 1

        栈为空:栈顶 == -1

 2)构建一个基础的栈结构

    private int[] arr; // 定义一个数据作为栈基
	private int maxSize; // 栈的最大容量
	private int top = -1; // 定义栈顶
	
	public Stack(int maxSize) {
		this.maxSize = maxSize;
		arr = new int[maxSize];
	}
	

 3)入栈方法:

public void push(int value) {
		if(isFull()) {
			System.out.print("栈已满,请扩容");
			return;
		}
		top++;
		arr[top] = value;
	}

4)出栈方法:

public int pop() {
		if(isEmpty()) {
			throw new RuntimeException("栈为空,没有数据");
		}
		int value = arr[top];
		top--;
		return value;
	}

 5)判断栈满或栈空:

// 判断栈是否已满
	public boolean isFull() {
		return top == maxSize - 1;
	}
	
	// 判断栈是否为空
	public boolean isEmpty() {
		return top == -1;
	}

6)输出: 

public void list() {
		if(isEmpty()) {
			throw new RuntimeException("栈为空,没有数据");
		}
		for(int i = top; i >= 0; i--){
			System.out.println("stack[" + i + "] = " + arr[i]);
		}
	}

完整代码如下: 

public class Stack {
	private int[] arr; // 定义一个数据作为栈基
	private int maxSize; // 栈的最大容量
	private int top = -1; // 定义栈顶
	
	public Stack(int maxSize) {
		this.maxSize = maxSize;
		arr = new int[maxSize];
	}
	
	// 入栈
	public void push(int value) {
		if(isFull()) {
			System.out.print("栈已满,请扩容");
			return;
		}
		top++;
		arr[top] = value;
	}
	

	// 出栈
	public int pop() {
		if(isEmpty()) {
			throw new RuntimeException("栈为空,没有数据");
		}
		int value = arr[top];
		top--;
		return value;
	}
	
	// 判断栈是否已满
	public boolean isFull() {
		return top == maxSize - 1;
	}
	
	// 判断栈是否为空
	public boolean isEmpty() {
		return top == -1;
	}
	
	// 打印输出
	public void list() {
		if(isEmpty()) {
			throw new RuntimeException("栈为空,没有数据");
		}
		for(int i = top; i >= 0; i--){
			System.out.println("stack[" + i + "] = " + arr[i]);
		}
	}
	

	
}

 二 队列

1.特点:先进先出

2.定义:队列是一种先进先出的线性表结构。和栈一样,通常用于限制线性表中元素的插入和删除,其中只允许插入的一端叫做“队尾”,只允许删除的一端叫做“队头”。

3.实现一个队列

1)基本思路:

        使用数组作为载体,定义两个指针分别指向“队头”、“队尾”。

        入队操作(插入数据):队尾++,数据放入

        出队操作(删除数据):队头++,数据出队

        队空:队头 == 队尾

        队满:队尾 == maxSize - 1

2)搭建框架:

    private int[] arr; // 定义一个数据作为队列
	private int maxSize; // 队列的最大容量
	private int front = -1; // 定义队头
	private int rear = -1; // 定义队尾
	
	public Queue(int maxSize) {
		this.maxSize = maxSize;
		arr = new int[maxSize];
	}

3)判断队列为空/已满 :

    // 判断队列是否已满
	public boolean isFull() {
		return rear == maxSize - 1;
	}
	
	// 判断队列是否为空
	public boolean isEmpty() {
		return rear == front;
	}

 4)入队:

    public void add(int value) {
		if(isFull()) {
			System.out.print("队列已满,请扩容");
			return;
		}
		rear++;
		arr[rear] = value;
	}

 5)出队:

    public int remove() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空,没有数据");
		}
		front++;
		return arr[front];
	}

6)打印输出: 

public void list() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空,没有数据");
		}
		for(int i = front + 1; i < rear; i++){
			System.out.println("Quene[" + i + "] = " + arr[i]);
		}
	}

 完整代码如下:

public class Queue {
	private int[] arr; // 定义一个数据作为队列
	private int maxSize; // 队列的最大容量
	private int front = -1; // 定义队头
	private int rear = -1; // 定义队尾
	
	public Queue(int maxSize) {
		this.maxSize = maxSize;
		arr = new int[maxSize];
	}
	
	// 入队
	public void add(int value) {
		if(isFull()) {
			System.out.print("队列已满,请扩容");
			return;
		}
		rear++;
		arr[rear] = value;
	}
	

	// 出队
	public int remove() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空,没有数据");
		}
		front++;
		return arr[front];
	}
	
	// 判断队列是否已满
	public boolean isFull() {
		return rear == maxSize - 1;
	}
	
	// 判断队列是否为空
	public boolean isEmpty() {
		return rear == front;
	}
	
	// 打印输出
	public void list() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空,没有数据");
		}
		for(int i = front + 1; i < rear; i++){
			System.out.println("Quene[" + i + "] = " + arr[i]);
		}
	}
	
}

三 Java Stack 类常用方法

序号方法描述
1boolean empty() 
测试堆栈是否为空。
2Object peek( )
查看堆栈顶部的对象,但不从堆栈中移除它。
3Object pop( )
移除堆栈顶部的对象,并作为此函数的值返回该对象。
4Object push(Object element)
把项压入堆栈顶部。
5int search(Object element)
返回对象在堆栈中的位置,以 1 为基数。

代码示例:

import java.util.*;
 
public class StackDemo {
 
    static void showpush(Stack<Integer> st, int a) {
        st.push(new Integer(a));
        System.out.println("push(" + a + ")");
        System.out.println("stack: " + st);
    }
 
    static void showpop(Stack<Integer> st) {
        System.out.print("pop -> ");
        Integer a = (Integer) st.pop();
        System.out.println(a);
        System.out.println("stack: " + st);
    }
 
    public static void main(String args[]) {
        Stack<Integer> st = new Stack<Integer>();
        System.out.println("stack: " + st);
        showpush(st, 42);
        showpush(st, 66);
        showpush(st, 99);
        showpop(st);
        showpop(st);
        showpop(st);
        try {
            showpop(st);
        } catch (EmptyStackException e) {
            System.out.println("empty stack");
        }
    }
}

 四 Java Queue常用方法

序号方法方法描述
1add增加一个元索
2remove 移除并返回队列头部的元素
3element返回队列头部的元素
4offer 添加一个元素并返回true
5poll 移除并返问队列头部的元素
6peek返回队列头部的元素
7put 添加一个元素
8take移除并返回队列头部的元素

代码示例:

import java.util.LinkedList;
import java.util.Queue;
 
public class Main {
    public static void main(String[] args) {
        //add()和remove()方法在失败的时候会抛出异常(不推荐)
        Queue<String> queue = new LinkedList<String>();
        //添加元素
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        queue.offer("d");
        queue.offer("e");
        for(String q : queue){
            System.out.println(q);
        }
        System.out.println("===");
        System.out.println("poll="+queue.poll()); //返回第一个元素,并在队列中删除
        for(String q : queue){
            System.out.println(q);
        }
        System.out.println("===");
        System.out.println("element="+queue.element()); //返回第一个元素 
        for(String q : queue){
            System.out.println(q);
        }
        System.out.println("===");
        System.out.println("peek="+queue.peek()); //返回第一个元素 
        for(String q : queue){
            System.out.println(q);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值