Arrays, stacks, and queues of the eight largest data structures in Java

A:

Data structure is the way a computer stores and organizes data, and refers to the collection of one or more specific data elements that exist among each other



Array Array Array Array Array Array

An array is a collection of data types used to hold the same data type. Note that only one data type can be held (except for arrays of type Object).

The array declaration

A:

ArrayName =new ArrayType [array length];

Int [] num=new int[5]; int[] num=new int[5]; Num [1]=12; num[1]=12; Println (num[0]); println(num[0]); println(num[0]); System.out.println(num[1]); System.out: 0 System.out: 12 String[] ss=new String[4]; ss[0]="Rocky"; System.out.println(ss[0]); System.out.println(ss[1]); // result system. out: Rocky system. out: null // You can see: int: default is 0, String: default is null, Boolean: default is flase

[] array name = {array element 1, array element 2,…… }

,4,3,5,7,3 int [] ss = {1}; for (int i=0; i<ss.length; i++){ System.out.println(ss[i]); }

Customize the realization of an array, the realization of the array to add, delete and change check

Public class myArray {private int[] intArray; Private int elems; private int elems; Private int length (); private int length (); Public myArray () {elems = 0; public myArray () {elems = 0; length = 50; intArray = new int[length]; Public myArray (int length) {elems = 0;} public myArray (int length) {elems = 0; this.length = length; intArray = new int[length]; } public int getSize() {return elems; Public Boolean add(int value) {public Boolean add(int value) {public Boolean add(int value) {public Boolean add(int value) {public Boolean add(int value) {public Boolean add(int value) { If (elems == length) {return false; } else { intArray[elems] = value; elems++; } return true; } // Returns the element represented by an array index, returns the element represented by an array index, returns the element represented by an array index, returns the element represented by an array index, returns the element represented by an array index, and returns the element represented by an array index. Prompt access to subscript cross-border public int the get (int I) {if (I < 0 | | I > elems) {System. Out. Println (" access subscript crossing the line "); } return intArray[i]; } public int find(int searchValue) {int I;} public int find(int searchValue) {int I; for (i = 0; i < elems; i++) { if (intArray[i] == searchValue) { break; } } if (i == elems) { return -1; } return i; } // Delete the element. If the desired value does not exist, return false; Public Boolean delete(int value) {int k = find(value); if (k == -1) { return false; } else { if (k == elems - 1) { elems--; } else {for (int I = k; int I = k; i < elems - 1; i++) { intArray[i] = intArray[i + 1]; } elems--; } return true; } public Boolean modify(int oldValue, int newValue) {int I = find(oldValue); If (I == -1) {System.out.println(" The data you need to modify does not exist "); return false; } else { intArray[i] = newValue; return true; Public void display() {for (int I = 0; i < elems; i++) { System.out.print(intArray[i] + " "); } System.out.println(); }}

call

MyArray =new myArray (4); myArray =new myArray (4); // Add four elements to Array.add (3); array.add(5); array.add(6); array.add(7); // display data Array.display (); Int I = Array.get (0); System.out.println(i); // Delete element array. Delete (7); // Modify element 6 to 12 Array.modify (6, 12); array.display(); // result System. Out: 3 5 6 7 System. Out: 3 5 12

Once an array is created, the size is fixed and the number of elements in the array cannot be dynamically expanded. If you initialize an array with a large size, it will waste memory space. If you initialize it with a small size, the number of data will increase and you will not be able to add it.

Definition of a 2-D Array: A 2-D array is an array whose elements are a 1-D array.

Int a[][] = new int[3][4]; int a[][] = new int[3][4]; int[][] b = new int[3][4]; Int [] c[] = new int[3][3]; b[0][0]=1; b[0][1]=5; System.out.print(b[0][0]+","); System.out.print(b[0][1]+","); System.out.print(b[3][6]+","); //java.lang.ArrayIndexOutOfBoundsException: length=3; Index System. = 3 / / results out: 1, 5, / / way two int [] [] d = {{1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23}}; For (int x=0; x<d.length; x++){ for(int y=0; y<d[x].length; y++){ System.out.print(d[x][y]+","); } System.out.println(); } / / results System. Out: 1 hc-positie System. Out: 9,11,13,15 System. Out: 17,19,21,23, / / way three int [] [] e = new int [3] []. e[0] = new int[2]; e[1] = new int[2]; e[2] = new int[2];

Stack (English: Stack) also known as a Stack or Stack, as a data structure, the Stack is a special linear table that can only be inserted and deleted at one end. It stores data according to the principle of first in last out, the first data is pushed to the bottom of the stack, the last data is at the top of the stack, when the need to read the data from the top of the stack to start the data (the last data is read first). Stack memory function, insert and delete the stack operation, do not need to change the bottom pointer of the stack. A stack is a special linear table that allows inserts and deletes on the same side. One end of the stack where inserts and deletes are allowed is called the top, and the other end is called the bottom. The bottom of the stack is fixed, while the top of the stack is floating; A stack with zero elements is called an empty stack. Ininserts are commonly called PUSH and deletes are called POP. Since stacked data structures only allow operations at one end, they operate on a Last In First Out (LIFO) principle. Stacks are also called LIFO lists.

Custom implementation of a stack, can be out of the stack, stack, can expand capacity, can access the top of the stack data

Public class myStack {private class myStack []; private class myStack []; private class myStack []; private class myStack []; Private int maxSize; Private int top; private int top; Public myStack () {this.elementData = new Object[10]; public myStack () {this.elementData = new Object[10]; this.top = -1; this.maxSize = 10; } public myStack (int InitialCapacity) {if (InitialCapacity < 0) {throw new IllegalArgumentException(" Stack < 0) : " + initialCapacity); } this.elementData = new Object[initialCapacity]; this.maxSize = initialCapacity; top = -1; } public Object push(Object value) {// IsGrow (top + 1); elementData[++top] = value; return value; } public Object pop() {Object obj = peek(); remove(top); return obj; } public Object Peek () {if(top == -1){throw new EmptyStackException(); } return elementData[top]; } public Boolean isEmpty() {return (top == -1);} public Boolean isEmpty() {return (top == -1); } public void remove(int top){// Set top to null elementData[top] = null; this.top--; } public Boolean isFull() {return (top == maxSize-1); } /** * If it needs to be expanded, then double the size and return true, Return false * * @param minCapacity * @return */ public Boolean isGrow(int minCapacity) {int oldCapacity = maxSize; Int newCapacity = 0; int newCapacity = 0; int newCapacity = 0; int newCapacity = 0; If ((oldCapacity << 1) -Integer.max_value > 0) {newCapacity = Integer.max_value; } else { newCapacity = (oldCapacity << 1); *2} this.maxSize = newCapacity; int[] newArray = new int[maxSize]; elementData = Arrays.copyOf(elementData, maxSize); return true; } else { return false; }}}

call

MyStack stack = new MyStack(3); stack.push(2); System.out.println(stack.peek()); stack.push("Rocky"); stack.push(true); stack.push("fight"); System.out.println(stack.peek()); System.out.println(stack.peek()); stack.pop(); stack.pop(); stack.pop(); System.out.println(stack.peek()); Boolean aa=stack.isEmpty(); System.out.println(aa); MyStack = new myStack (); myStack = new myStack (); myStack = new myStack (); myStack = new myStack (); String str = "HelloWorld"; char[] cha = str.toCharArray(); for(char c : cha){ myStack.push(c); } while(! myStack.isEmpty()){ System.out.println(myStack.pop()); }

Queue is a special kind of linear table, special in that it only allows delete operations at the front of the table, and insert operations at the back of the table. Like the stack, Queue is a linear table with limited operation. The end that does the insert operation is called the end of the queue and the end that does the delete operation is called the head of the queue. When there are no elements in the queue, it is called an empty queue. The data elements of a queue are also known as queue elements. Inserting a queue element into a queue is called enqueuing, and deleting an element from a queue is called dequeuing. Because the queue is only allowed to be inserted at one end and deleted at the other end, only the earliest elements to enter the queue can be deleted from the queue first, so the queue is also known as FIFO — first in first out (FIFO) linear table.

1. Unlike the stack, the data in the queue does not always start at the index 0 of the array. After removing some data from the front of the queue, the pointer to the front of the queue will point to a higher index position



2. When we redesign, the pointer at the back of the queue will move up when a new item is added to the queue (i.e. in the direction of a higher index). When a data item is removed, the front pointer of the queue moves up. So the design seems to be the opposite of what happens in real life. For example, the person at the head of the line buys the ticket and then leaves, and the whole line moves forward. It is also possible in a computer to remove a number from the queue and move the queue forward as a whole, but doing so is inefficient. What we chose to do was move the pointer at the head and back of the queue.

Customize the queue to achieve add and delete

3. If you move the pointer in step ②, you believe that the pointer at the end of the queue will soon move to the end of the data. At this time, you may have removed the data, and then there will be an empty position in the queue.



4. In order to avoid the queue being dissatisfied and not being able to insert new data, we can wind the tail pointer back to the beginning of the array. This is also called “circular queue”.

To realize after understanding the principle:

public class MyQueue { private Object[] queArray; Private int maxSize; private int maxSize; Private int front; private int front; Private int new (); private int new (); Private int (); private int (); private int (); public MyQueue(int size){ maxSize=size; queArray=new Object[maxSize]; front=0; rear=-1; nItems=0; } public void insert(int value){if (isFull()){System.out.println(" The queue isFull! ");} public void insert(int value){if (isFull()){System.out.println(" The queue isFull! "); ); }else {if (rear== maxsize-1){rear== maxsize-1; } // quearArray [++rear] = value; nItems++; }} public Object remove(){Object removeValue = null; if (! isEmpty()){ removeValue=queArray[front]; QuearArray [front]=null; // line header pointer +1 front++; If (front==maxSize){// Front =0; } nItems--; return removeValue; } return removeValue; } public int getSize(){return getSize(); } public Object PeekFront (){return QuearRay [front]; } public Boolean isEmpty(){return (nItems ==0);} public Boolean isEmpty(){return (nItems ==0); Public Boolean isFull(){return (nItems == maxSize);} public Boolean isFull(){return (nItems == maxSize); }}

call

MyQueue queue = new MyQueue(3); queue.insert(1); queue.insert(2); queue.insert(3); [1,2,3] System.out.println(Queue. PeekFront ()); //1 queue.remove(); [null,2,3] System.out.println(Queue. PeekFront ()); //2 queue.insert(4); / / queArray array data of [4, 2, 3] / / why is [4, 2, 3] not [4] 2 / / should be after the queue of the full circulation to front / * if tails pointing to the top, then loop back, If (rear==maxSize-1){rear==maxSize-1; }*/ system.out. println(queue.peekFront());}*/ system.out. println(queue.peekFront()); //2, if the queue header pointer does not change queue.remove(); [4, NULL,3] queue.remove(); [4, NULL, NULL] Queue. Insert (5); [4,5, NULL] queue.insert(6); [4,5,6] Queue. Insert (7); [4,5,6] System.out.println(Queue. PeekFront ()); System. Out: 1 System. Out: 2 System. Out: 2 System. Out: queue full!! System.out: 4

Prefix: The operator precedes the operand. For example, the +-543 (the computer scans from right to left) infix: The operator precedes the operand. The operator is in the middle of the operand, which is also the most recognizable arithmetic expression for humans: 3+4-5 postfix expressions: The operator follows the operand, such as 34+5- (the computer scans from left to right)

End: We’re all worms in the gutter, but someone has to look at the stars.