基础:栈和队列
栈
顺序存储结构:使用数组实现栈
public class StackArray {
//需要一个数组来保存栈的元素
private int[] stack;
//需要一个指针指向栈顶元素
private int point;
public StackArray(int size){
this.stack = new int[size];
//-1表示栈空
this.point = -1;
}
public void push(int elem){
stack[++this.point] = elem;
}
public int pop(){
if(!empty()){
return stack[this.point--];
}else{
return -1;
}
}
public int peek(){
if(!empty()){
return stack[this.point];
}else{
return -1;
}
}
public boolean empty(){
if(this.point == -1){
return true;
}
return false;
}
public static void main(String[] args) {
StackArray stackArray = new StackArray(10);
stackArray.push(1);
stackArray.push(2);
stackArray.push(3);
System.out.println(stackArray.peek());//3
System.out.println(stackArray.pop());//3
System.out.println(stackArray.peek());//2
System.out.println(stackArray.pop());//2
System.out.println(stackArray.pop());//1
System.out.println(stackArray.empty());
System.out.println(stackArray.pop());//-1
}
}
链式存储结构(推荐)
package StackAndQueue;
public class StackList {
//定义栈顶元素
private Node top;
//初始化
public StackList(){
this.top = null;
}
public void push(int i){
//注意:栈的指针是从上向下的
this.top = new Node(i, top);
}
public int pop(){
if(!empty()){
int val = this.top.element;
this.top = this.top.next;
return val;
}
return -1;
}
public int peak(){
if(!empty()){
int val = this.top.element;
return val;
}
return -1;
}
public boolean empty(){
if(this.top == null){
return true;
}
else{
return false;
}
}
public static void main(String[] args) {
StackList stackList = new StackList();
stackList.push(1);
stackList.push(2);
stackList.push(3);
System.out.println(stackList.peak());//3
System.out.println(stackList.pop());//3
System.out.println(stackList.peak());//2
System.out.println(stackList.pop());//2
System.out.println(stackList.pop());//1
System.out.println(stackList.empty());
System.out.println(stackList.pop());//-1
}
}
队列
顺序存储结构
/*
用数组实现的队列是循环队列,这样可以节省空间
为了区分队列是满还是空,数组中需要有一个位置为空 -- 一般来说数组0位置为空
首先让front = rear = 0,进队 (rear + 1) % length,出队front++
这样就出现了数组0位置为空(刚开始),以后不一定空出什么位置
满:(rear+1) % length == front
空:rear = front
*/
public class QueueArray {
int[] queue;
int size;
int front,rear;
public QueueArray(int i){
size = i;
queue = new int[i];
front = 0;
rear = 0;
}
public void enter(int i){
if(!full()){
//先确定rear新位置,在更新
rear = (rear + 1) % size;
queue[rear] = i;
}
}
public int leave(){
if(!empty()){
//指向下一个位置
front = (front + 1) % size;
//理解为当前指向的为空
return queue[front];
}
return -1;
}
public int peek(){
if(!empty()){
return queue[(front + 1) % size];
}
return -1;
}
public boolean empty(){
if(rear == front){
return true;
}
return false;
}
public boolean full(){
if((rear + 1) % size == front){
return true;
}
return false;
}
public static void main(String[] args) {
QueueArray queueArray = new QueueArray(5);
queueArray.enter(1);
queueArray.enter(2);
queueArray.enter(3);
queueArray.enter(4);
System.out.println(queueArray.full());//true
System.out.println(queueArray.peek());//1
System.out.println(queueArray.leave());//1
System.out.println(queueArray.leave());//2
System.out.println(queueArray.peek());//3
System.out.println(queueArray.empty());//false
System.out.println(queueArray.leave());//3
System.out.println(queueArray.leave());//4
System.out.println(queueArray.empty());//true
}
}
链式存储结构
package StackAndQueue;
public class QueueList {
//定义头节点、尾节点
Node front;
Node rear;
//队列为空时,front和rear都为null
public QueueList(){
this.front = null;
this.rear = null;
}
public void enter(int i){
Node newNode = new Node(i,null);
//判断队列是否为空
if(isEmpty()){
this.front = newNode;
this.rear = newNode;
}else{
this.rear.next = newNode;
this.rear = newNode;
}
}
public int leave(){
if(isEmpty()){
return -1;
}
int val = this.front.element;
this.front = this.front.next;
if(this.front == null){
this.rear = null;
}
return val;
}
public int peek(){
if(isEmpty()){
return -1;
}
int val = this.front.element;
return val;
}
public boolean isEmpty(){
if(this.front == null){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
QueueList queueList = new QueueList();
queueList.enter(1);
queueList.enter(2);
queueList.enter(3);
queueList.enter(4);
System.out.println(queueList.peek());//1
System.out.println(queueList.leave());//1
System.out.println(queueList.leave());//2
System.out.println(queueList.peek());//3
System.out.println(queueList.isEmpty());//false
System.out.println(queueList.leave());//3
System.out.println(queueList.leave());//4
System.out.println(queueList.isEmpty());//true
}
}
java Deque使用
Java双端队列Deque使用详解
可以当作是队列以及堆栈使用
Queue方法 | 等效的Deque方法 | 说明 |
---|---|---|
add(e) | addLast(e) | 抛出异常 |
offer(e) | offerLast(e) | 返回特殊值 |
remove() | removeFirst() | 抛出异常 |
poll() | pollFirst() | 返回特殊值 |
element() | getFirst() | 抛出异常 |
peek() | peekFirst() | 返回特殊值 |
Stack方法 | 等效的Deque方法 | 说明 |
---|---|---|
push(e) | addFirst(e) | 抛出异常 |
pop() | removeFirst() | 抛出异常 |
peek() | peekFirst() | 返回特殊值 |