1.栈(Stack)
概念:
一种特殊的线性表只允许在固定的一端进行操作。进行操作的一端称为栈顶,另一称为栈底。
元素操作遵循后进先出原则(LIFO)。
(PUSH) 压栈=入栈=进栈=插入操作
( POP )出栈=删除操作
模型:
栈的实现
顺序栈:数组实现
入栈:存入数据
出栈:输出最后一个存入的数据并删除
peek:输出最后一个存入的数据
isfull:判断栈是否满了
import java.util.Arrays;
class AStack {//定义栈的结构是数组
public int[] array;//数组内元素类型
public int size = 0;
//new一个int[10]的数组作为栈
public AStack() {
this.array = new int[10];
}
//入栈
public void push(int v) {
if (this.size >= this.array.length){
System.out.println("栈满了");
return ;
}
this.array[this.size] = v;
size++;
}
//出栈
public int pop() {
int ret =array[size-1];
this.size--;
return ret;
}
//输出最后一个存入的数据
public int peek() {
return array[size-1];
}
//判断栈是否满了
public boolean isFull() {
if (this.size >= this.array.length) {
return true;
}else {
return false;
}
}
}
public class stackTest {
public static void main(String[] args) {
AStack stk = new AStack();
stk.push(0);
stk.push(1);
stk.push(2);
stk.push(3);
stk.push(4);
stk.push(5);
stk.push(6);
stk.push(7);
stk.push(8);
stk.push(9);
System.out.println(stk.isFull());
System.out.println(Arrays.toString(stk.array));
System.out.println(stk.peek());
System.out.println(stk.pop());
System.out.println(stk.pop());
System.out.println(Arrays.toString(stk.array));
}
}
链式栈:链表实现
//定义链表节点
class Node{
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
//定义栈
class Stack {
public Node head;
public int size = 0;
public Stack() {
this.size = size;
}
public void push(int v) {
Node node = new Node(v);
if (size >= 10){
System.out.println("栈满了");
return ;
}
if (this.head == null) {
this.head = node;
}else {
node.next = this.head;
this.head = node;
}
this.size++;
}
public int pop() {
int ret =this.head.val;
this.head = this.head.next;
this.size--;
return ret;
}
public int peek() {
return this.head.val;
}
public boolean isFull() {
if (this.size >= 10) {
return true;
}else {
return false;
}
}
public void show(){
Node cur = this.head;
System.out.print("[");
for (int i = 0; i < size-1; i++) {
System.out.print(cur.val+",");
cur = cur.next;
}
System.out.println(cur.val+"]");
}
}
public class test {
public static void main(String[] args) {
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
stack.push(0);
System.out.println(stack.isFull());
System.out.println(stack.peek());
stack.show();
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.show();
}
}
二、队
1.概念
只允许在一端(队尾Rear)进行插入数据操作,在另一端(队头Front)进行删除数据操作的特殊线性表。遵循先进先出原则。
一般采用链表实现;
实现:
尾插表示入队,删除表头表出队。
class Node{
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
class MyQueue{
public Node head =null;
public Node tail =null;
public int size = 0 ;
public void offer(int v) {
Node node = new Node(v);
if (this.size == 10){
System.out.println("队列已满");
return;
}
if (head == null) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = this.tail.next;
}
this.size++;
}
public int poll(){
int ret = this.head.val;
this.head = this.head.next;
this.size--;
return ret;
}
public int peek(){
return this.head.val;
}
public boolean isEmpty(){
return this.size == 0;
}
}
public class test {//测试
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
System.out.println(myQueue.isEmpty());
myQueue.offer(1);
myQueue.offer(2);
myQueue.offer(3);
myQueue.offer(4);
myQueue.offer(5);
System.out.println(myQueue.peek());
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
}
}
循环队列
class MyCircleQueue {
public int[] elem;
public int front;
public int rear;
public MyCircleQueue(int size) {
this.elem = new int[size+1];
}
public void enQueue(int val) {
if ((rear + 1) % elem.length == front) {
System.out.println("已满");
return;
}
elem[rear] = val;
rear = (rear+1)%elem.length;
}
public int deQueue() {
if (rear == front) {
System.out.println("空队列");
return -1;
}
front = (front+1)%elem.length;
return elem[(front+elem.length-1)%elem.length];
}
public int front() {
if (rear == front) {
System.out.println("空队列");
return -1;
}
return elem[front];
}
public int rear() {
if (rear == front) {
System.out.println("空队列");
return -1;
}
return elem[(rear + elem.length - 1) % elem.length];
}
public boolean isFull() {
if ((rear + 1) % elem.length == front) {
return true;
}else {
return false;
}
}
}
public class test {
public static void main(String[] args) {
MyCircleQueue myCircleQueue = new MyCircleQueue(4);
myCircleQueue.isFull();
System.out.println(myCircleQueue.front());
myCircleQueue.enQueue(11);
myCircleQueue.enQueue(23);
myCircleQueue.enQueue(34);
myCircleQueue.enQueue(46);
System.out.println(myCircleQueue.front());
System.out.println(myCircleQueue.rear());
System.out.println(myCircleQueue.deQueue());
myCircleQueue.enQueue(26);
System.out.println(myCircleQueue.front());
System.out.println(myCircleQueue.rear());
}
}