栈和队列,严格意义上来说,也属于线性表,因为它们也都用于存储逻辑关系为 “一对一” 的数据,
使用栈结构存储数据,讲究“先进后出”,即最先进栈的数据,最后出栈;
使用队列存储数据,讲究 “先进先出”,即最先进队列的数据,也最先出队列。
既然栈和队列都属于线性表,根据线性表分为顺序表和链表的特点,栈也可分为顺序栈和链表,队列也分为顺序队列和链式队列,这里我目前只实现了顺序栈和链式队列。
可以根据注释理解代码:
栈
顺序表实现顺序栈
package Stack;
import java.util.Arrays;
/**
* ClassName: MyStack
* Description:
* date: 2021/4/28 11:01
* 栈的实现
* @author wt
* @since JDK 1.8
*/
public class MyStack {
public int[] elem;
public int top; //usedSize
public MyStack() {
this.elem = new int[10];
this.top = 0;
}
/**
* 1.push()
* 压栈,
* 又称进栈
*/
public void push(int val) {
if (this.isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
this.elem[this.top] = val;
top++;
}
//判断栈是否满了
public boolean isFull() {
if (this.elem.length == this.top) {
return true;
}
return false;
}
/**
* 2.pop()
* 出栈,
* 取出并返回出栈元素
*/
public int pop() {
if (isEmpty()) {
throw new UnsupportedOperationException("栈为空");
}
int old = this.elem[this.top-1];
this.top--;
return old;
}
//判断栈是否为空
public boolean isEmpty() {
return this.top == 0;
}
/**
* 3.peek()
* 获取栈顶元素
* 不取出
*/
public int peek() {
if (isEmpty()) {
throw new UnsupportedOperationException("栈为空");
}
//int old = this.elem[this.top-1];
return this.elem[this.top-1];
}
/**
* 打印栈
*/
public void display() {
for (int i = 0; i <this.top ; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
}
顺序栈测试截图
package Stack;
/**
* ClassName: TestMyStack
* Description:
* date: 2021/4/28 11:29
*
* @author wt
* @since JDK 1.8
*/
public class TestMyStack {
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
System.out.println("打印栈:");
myStack.display();
System.out.println("返回栈顶元素:");
System.out.println(myStack.peek());
System.out.println("出栈:");
myStack.pop();
myStack.display();
myStack.pop();
myStack.pop();
myStack.pop();
System.out.println("出完:");
myStack.display();
}
}
队列
单链表实现链式队列
package Queue;
/**
* ClassName: MyQueue
* Description:
* date: 2021/4/28 19:22
* 链式队列
* 1.队列都是从队尾进,对头出。
* 2.进队列使用链表尾插法,时间复杂度为O(1)。
* 3.出队列从对头出,时间复杂度为O(1)。
* @author wt
* @since JDK 1.8
*/
class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
public class MyQueue {
public Node front;//对头
public Node rear;//队尾
public int usedSize;//有效个数
/**
* 1.offer()入队
* 采用尾插法
* @param val
*/
public void offer(int val) {
Node node = new Node(val);
//第一次插入
if (this.rear == null) {
this.front = node;
this.rear = node;
} else {
//尾插法
this.rear.next = node;
this.rear = node;
}
usedSize++;
}
/**
* 2.poll() 出队
* 取出队头元素,返回其值
*/
public int poll() throws UnsupportedOperationException{
if (isempty()) {
throw new UnsupportedOperationException("队列为空");
}
//拿出队头,即链表的头的值返回,front指向下一位,有效个数减一
int old = this.front.val;
this.front = this.front.next;
this.usedSize--;
return old;
}
public boolean isempty() {
return this.usedSize == 0;
}
/**
* 3.peek()
* 获取队头元素 但是不删除
*/
public int peek() {
if (isempty()) {
throw new UnsupportedOperationException("队列为空");
}
// int old = this.front.val;
// return old;
return this.front.val;
}
/**
* 4.size()
*
*/
public int size(){
return this.usedSize;
}
/**
* 5.display
* 打印队列
*/
public void display() {
if (isempty()) {
return;
}
Node cur = this.front;
while (cur != null) {
System.out.println(cur.val +" ");
cur = cur.next;
}
}
}
链式队列测试截图
package Queue;
/**
* ClassName: TestMyQueue
* Description:
* date: 2021/4/28 19:50
* 链式队列测试
* @author wt
* @since JDK 1.8
*/
public class TestMyQueue {
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
System.out.println("入队打印:");
myQueue.offer(1);
myQueue.offer(2);
myQueue.offer(3);
myQueue.display();
System.out.print("出队:");
System.out.println(myQueue.poll());
System.out.print("获取现在的队头:");
System.out.println(myQueue.peek());
}
}
如果有代码错误,望多指点!!!