The queue stack and the java

A stack

Stack is a last-out data structure. When the order is not what esoteric concept, a lot of things when you want to put in a container, when you want to take out, according to what is put in the last first taken out, we should think of the stack.

The analogy of a stack box analogy book data stack. Now the empty boxes to put the book, the first book to be placed in the bottom of the box, this is then put the second, third .... present When removing a book from the box, first remove the top must be the book (the last release of the book, rather than put the beginning of the book). The figure is shown in the process schematic of an element onto the stack.

This stack will be achieved by a data structure java code (implemented using arrays).

Write your MyStack.java categories:

public class MyStack {
       //使用数组来实现栈,声明一个数组
	  int[] Arr;
	   //构造方法
	  public MyStack() {
		  Arr = new int[0];
	  }
	  
	    //压入元素的方法push
	  public void push(int element) {
	      //创建一个新数组
		  int[] newArr = new int[Arr.length+1];
		  //将原数组的元素赋值到新数组
		  for(int i=0; i<Arr.length; i++) {
			  newArr[i] = Arr[i];
		  }
		  //将新增加的元素放入新数组中
		  newArr[Arr.length] = element;
		  //使用新数组替换原数组
		  Arr = newArr;
	  }
	  
	  //取出栈顶元素
	  public int pop() {
		  //若栈中没有元素,则取出的元素的下标为0-1=-1,超出下标界限。因此要抛出异常
		  if(Arr.length == 0) {
			  throw new RuntimeException("下标越界");
		  }
		  //取出数组中最后一个元素
		  int element = Arr[Arr.length-1];
		  //创建一个新的数组
		  int[] newArr = new int[Arr.length-1];
		  //将原数组中除最后一个元素以外的其他元素复制到新数组
		  for(int i=0; i<Arr.length-1; i++) {
			  newArr[i] = Arr[i];
		  }
		  //将新数组替换原数组
		  Arr = newArr;
		  //返回栈顶元素
		  return element;
	  }
	  
	  //查看栈顶元素
	  public int look() {
		  //将数组中最后一个元素返回
		  return Arr[Arr.length-1];
	  }
	  
	  //判断栈是否为空
	  public boolean isEmpty() {
		  return Arr.length==0;
	  }
}

Write test classes TestStack:

public class TestStack {
	public static void main(String[] args) {
     //创建一个栈
	  MyStack ms = new MyStack();
	  //向栈中压入数据
	  ms.push(1);
	  ms.push(2);
	  ms.push(3);
	  //查看栈顶元素
	  System.out.println(ms.look()); 
	  //取出栈顶元素
	  System.out.println(ms.pop()); 
	  System.out.println(ms.pop()); 
	  //判断栈是否为空
	  System.out.println(ms.isEmpty()); 
	}
}

Second, the queue

Queues and the opposite stack, the stack is a last-out data structure, and the queue is a FIFO data structure. The reason why this data structure called a queue, because this structure is the same as the queue, if the queue is now past a door, must be at the top of people (to insert data queue) go first. The following figure shows a schematic configuration of the queue:

Let's queue (implemented using arrays) using java code.

Write your MyQueue.java categories:

public class MyQueue {
      int[] Arr;
      public MyQueue() {
    	  Arr = new int[0];
      }
      //入队
	  public void add(int element) {
	      //创建一个新数组
		  int[] newArr = new int[Arr.length+1];
		  //将原数组的元素赋值到新数组
		  for(int i=0; i<Arr.length; i++) {
			  newArr[i] = Arr[i];
		  }
		  //将新增加的元素放入新数组中
		  newArr[Arr.length] = element;
		  //使用新数组替换原数组
		  Arr = newArr;
	  }
	  
	  //取出队列元素
	  public int poll() {
		  //取出数组中第1个元素
		  int element = Arr[0];
		  //创建一个新的数组
		  int[] newArr = new int[Arr.length-1];
		  //将原数组中除第一个元素以外的其他元素复制到新数组
		  for(int i=0; i<newArr.length; i++) {
			  newArr[i] = Arr[i+1];
		  }
		  //将新数组替换原数组
		  Arr = newArr;
		  //返回队列中的元素
		  return element;
	  }
	  
	  //判断栈是否为空
	  public boolean isEmpty() {
		  return Arr.length==0;
	  }
}

Then write test classes TestQueue.java:

public class TestQueue {
	public static void main(String[] args) {
     //创建一个队列
	  MyQueue mq = new MyQueue();
	  //将元素入队
	  mq.add(1);
	  mq.add(2);
	  mq.add(3);
	  mq.add(4);
	  //出队
	  System.out.println(mq.poll());
	  //判断队列是否为空
	  System.out.println(mq.isEmpty());  
	}
}

 

 

 

 

 

Published 20 original articles · won praise 2 · Views 1590

Guess you like

Origin blog.csdn.net/weixin_42132733/article/details/105152706