栈和队列
栈的实现
class stackOfArray{
public int[] elem;
public int usedsize;
public stackOfArray() {
this.elem = new int[5]; //初始设置为5
}
public void push(int value) { //往栈中压入元素
if(isFull()) {
//栈满时也可以扩容 this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
System.out.println("栈满,无法push");
return;
}
this.elem[this.usedsize] = value;
this.usedsize++;
}
public void pop() { //获取栈顶元素,并删除
if (isEmpty()) {
System.out.println("栈空,不能pop");
return;
}
System.out.println(this.elem[this.usedsize - 1]);
this.usedsize--;
}
public void peek() { //获取栈顶元素,不删除
if (isEmpty()) {
System.out.println("栈空,不能peek");
return;
}
System.out.println(this.elem[this.usedsize - 1]);
}
public boolean isEmpty() { //判空
if(this.usedsize == 0) {
return true;
}
return false;
}
public boolean isFull() { //判满
if (this.elem.length == this.usedsize) {
return true;
}
return false;
}
@Override
public String toString() {
return "stackOfArray{" +
"elem=" + Arrays.toString(elem) +
'}';
}
}
-
泛型实现顺序栈
//泛型实现顺序栈,主要将类改为泛型类 //上述顺序栈代码只需更改以下几处 class stackOfArray<T>{ //添加泛型 public T[] elem; //泛型 public int usedsize; public stackOfArray() { this.elem = (T[]) new Object[5]; } public void push(T value) { //往栈中压入元素 //参数也为泛型 if(isFull()) { this.elem = Arrays.copyOf(this.elem,this.elem.length * 2); // System.out.println("栈满,无法push"); // return; } this.elem[this.usedsize] = value; this.usedsize++; } }
-
单链表实现栈
单链表实现栈,使用头插、头删方法效率最高,时间复杂度为O(1).
class myNode{
public int val;
public myNode next;
public myNode(int value) {
this.val = value;
}
}
class stackOfListNode{
public myNode head; //头节点
public void push(int value) {
myNode node = new myNode(value);
if (isEmpty()) {
this.head = node;
}else {
node.next = this.head; //头插
this.head = node;
}
}
public void pop() {
if (isEmpty()) {
System.out.println("栈空,无法pop");
}else {
System.out.println(this.head.val);
this.head = this.head.next; //头删
}
}
public void peek() {
if (isEmpty()) {
System.out.println("栈空,无法peek");
}else {
System.out.println(this.head.val);
}
}
public boolean isEmpty() {
if(this.head == null) {
return true;
}
return false;
}
public void display() {
myNode cur = this.head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
队列的实现
- 链表实现队列
class myQueueNode{
public int val;
public myQueueNode next;
public myQueueNode(int value) {
this.val = value;
}
}
class queueOfListNode{
//使用带尾节点的单链表
public myQueueNode head;
public myQueueNode tail;
public void offer(int val) { //入队
myQueueNode node = new myQueueNode(val);
if (isEmpty()) {
this.head = node;
this.tail = node;
}else {
this.tail.next = node;
this.tail = node;
}
}
public void poll() { //出队,并删除
if (isEmpty()) {
System.out.println("队空,不能poll");
}else {
int temp = this.head.val;
this.head = this.head.next;
System.out.println(temp);
}
}
public void peek() { //出队,不删除
if (isEmpty()) {
System.out.println("队空,不能peek");
}else {
System.out.println(this.head.val);
}
}
public boolean isEmpty() { //判空
if(this.head == null) {
return true;
}
return false;
}
public void display() {
myQueueNode cur = this.head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
- 数组实现循环队列
class circularQueue{
public int[] elem;
public int front;
public int rear;
public circularQueue(int cap) {
this.elem = new int[cap + 1]; //浪费一个空间,以便于判断队满队空
}
public void enQueue(int value) {
if (isFull()) {
System.out.println("队满,不能入队");
}else {
this.elem[this.rear] = value;
this.rear = (this.rear + 1) % this.elem.length;
}
}
public void deQueue() {
if (isEmpty()) {
System.out.println("队空,不能出队");
}else {
System.out.println(this.elem[this.front]);
this.front = (this.front + 1) % this.elem.length;
}
}
public void getRear() {
if (isEmpty()) {
System.out.println("队空,不能getRear");
}else {
int index = this.rear == 0 ? this.elem.length - 1 : this.rear - 1;
System.out.println(this.elem[index]);
}
}
public void getFront() {
if (isEmpty()) {
System.out.println("队空,不能getFront");
}else {
System.out.println(this.elem[this.front]);
}
}
public boolean isEmpty() { //rear == front时队空
if (this.rear == this.front) {
return true;
}
return false;
}
public boolean isFull() { //当(rear + 1) % this.elem.length == front时队满
if ((this.rear + 1) % this.elem.length == this.front) {
return true;
}
return false;
}
@Override
public String toString() {
return "circularQueue{" +
"elem=" + Arrays.toString(elem) +
'}';
}
}
-
数组双端循环队列
类似于循环队列,区别在于,双端队列支持头部删除插入,尾部也可以删除插入。
class double_queue { public int[] elem; public int front; public int rear; public double_queue(int cap) { this.elem = new int[cap + 1]; //浪费一个空间,以便于判断队满队空 } public void addRear(int value) { //队尾添加元素 if (isFull()) { System.out.println("队满,不能addRear"); }else { this.elem[this.rear] = value; this.rear = (this.rear + 1) % this.elem.length; } } public void addFront(int value) { //队头添加元素 if (isFull()) { System.out.println("队满,不能addFront"); }else { this.front = this.front == 0 ? this.elem.length - 1 : this.front - 1; this.elem[this.front] = value; } } public void deleteRear() { //从队尾删除元素,并输出 if (isEmpty()) { System.out.println("队空,无法deleteRear"); }else { this.rear = this.rear == 0 ? this.elem.length - 1 : this.rear - 1; System.out.println(this.elem[this.rear]); } } public void deleteFront() { //从队头删除元素,并输出删除了哪个元素 if (isEmpty()) { System.out.println("队空,不能deleteFront"); }else { int temp = this.elem[this.front]; this.front = (this.front + 1 ) % this.elem.length; System.out.println(temp); } } public int getFront() { //获取队头元素 if (isEmpty()) { System.out.println("队空,不能getFront"); return -1; } return this.elem[this.front]; } public int getRear() { //获取队尾元素 if (isEmpty()) { System.out.println("队空,不能getRear"); return -1; } int index = this.rear == 0 ? this.elem.length - 1 : this.rear - 1; return this.elem[index]; } public boolean isEmpty() { //判空 if (this.front == this.rear) { return true; } return false; } public boolean isFull() { //判满 if ((this.rear + 1) % this.elem.length == this.front) { return true; } return false; } @Override public String toString() { return "double_queue{" + "elem=" + Arrays.toString(elem) + '}'; } }
*队列实现栈
class stack_with_queue{
public Queue<Integer> que1;
public Queue<Integer> que2;
public int usedsize;
public stack_with_queue(){
this.que1 = new LinkedList<>();
this.que2 = new LinkedList<>();
}
public void push(int val) { //队不为空就放,两个队都为空随机选一个放
if (!this.que1.isEmpty()) {
this.que1.offer(val);
}else if(!this.que2.isEmpty()){
this.que2.offer(val);
}else {
this.que1.offer(val);
}
this.usedsize++;
}
public void pop() { //队不为空,把队里前size-1个移到空队中,然后poll最后一个
if (isEmpty()) {
System.out.println("队空,无法pop");
return;
}
int i = this.usedsize - 1;
if (!this.que1.isEmpty()) {
while(i > 0) {
this.que2.offer(this.que1.poll());
i--;
}
this.usedsize--;
System.out.println(this.que1.poll());
}else if (!this.que2.isEmpty()) {
while(i > 0) {
this.que1.offer(this.que2.poll());
i--;
}
this.usedsize--;
System.out.println(this.que2.poll());
}
}
public void peek() { //队不为空,就把所有元素移到空的里面,temp标记的就是栈的顶端节点
if (isEmpty()) {
System.out.println("队空,无法peek");
return;
}
int index = this.usedsize;
int temp = -1;
if(!this.que1.isEmpty()) {
while(index > 0) {
temp = this.que1.poll();
this.que2.offer(temp);
index--;
}
}else if(!this.que2.isEmpty()) {
while(index > 0) {
temp = this.que2.poll();
this.que1.offer(temp);
index--;
}
}
System.out.println(temp);
}
public boolean isEmpty() {
if (this.que2.isEmpty() && this.que1.isEmpty()) {
return true;
}
return false;
}
}
*栈实现队列
class queue_with_stack{
Stack<Integer> s1; //只入队
Stack<Integer> s2; //只出队
public queue_with_stack() {
this.s1 = new Stack<>();
this.s2 = new Stack<>();
}
public void offer(int value) {
this.s1.push(value);
}
public void poll() { //全部移到s2,然后pop s2的顶端数据
if (this.s2.isEmpty()) {
while(!this.s1.isEmpty()) {
this.s2.push(this.s1.pop());
}
}
if (!this.s2.isEmpty()) {
int temp = this.s2.pop();
System.out.println(temp);
}
}
public void peek() { //同poll一样,全部移到s2,peek顶端数据
if(this.s2.isEmpty()) {
while(!this.s1.isEmpty()) {
this.s2.push(this.s1.pop());
}
}
if(!this.s2.isEmpty()) {
int temp = this.s2.peek();
System.out.println(temp);
}
}
public boolean isEmpty() {
if(this.s1.isEmpty() && this.s2.isEmpty()) {
return true;
}
return false;
}
}