I. Stack
Generally, a stack is a linear table. All its inserts and deletes are limited at one end of the table. The end that allows insertion and deletion is called the top of the stack ), the end of insertion or deletion is not allowed to be called a bottom)
If a stack S = (A1, A2, A3 ,..., An), that is, A1 is the bottom element of the stack, an is the top element of the stack, element AI is located above the element ai-1. The elements in the stack Are A1, A2, A3 ,..., If all elements are extracted from the stack, the order of the output stack is an, An-1 ,..., A1. That is to say, the inbound and outbound elements in the stack follow the principle of forward and backward, which is an important feature of the stack structure. Therefore, the stack is also called the LIFO-last in first out table.
Generally, stack operations include the following types:
(1) Add a new element to the top of the stack, called Stack;
(2) Delete the top element of the stack, which is called the rollback;
(3) view the current top element of the stack, called the read stack.
Public class Stack {// Stack Array long stackarr []; // stack size int maxsize; // The top int top of the stack; // initialize a public stack (INT size) {maxsize = size; stackarr = new long [size]; Top =-1 ;} // public long POP () {return stackarr [top --];} // public void push (long value) {stackarr [++ top] = value;} // determines whether the stack is empty. Public Boolean isempty () {return Top =-1 ;} // determine whether the stack is full public Boolean isfull () {retur N Top = maxsize-1;} // get the top element of the stack public long PEEK () {return stackarr [Top];} public static void main (string [] ARGs) {stack = new stack (10); While (! Stack. isfull () {long v = (long) (math. random () * 100); stack. push (V); system. out. print (V + "");} system. out. println (); While (! Stack. isempty () {long topvalue = stack. Pop (); system. Out. Print (topvalue + "") ;}system. Out. println ();}}
Output:
94 33 49 96 25 2 66 95 86 39
39 86 95 66 2 25 96 49 33 94
Ii. Queue
A queue is also a special case of a linear table. However, unlike a stack, all the inserts are limited at one end of the table, all deletions are performed at the other end of the table.
The end that can be inserted is called a rear, and the end that can be deleted is called a front ).
The structure of the queue is characterized by the first release of the elements of the advanced team. Assume that the queue q = (A1, A2, A3 ,..., An), then the elements in queue Q are according to A1, A2, A3 ,..., An order to enter the team, and the first team should be A1, the second team should be A2, only after the ai-1 team, AI can leave the team (1 ≤ I ≤ n ). Therefore, queues are usually called the Fifi-first in first out table.
Queue operations include the following:
(1) enter the queue (insert) and add a new element to the end of the queue;
(2) Remove (delete) the element from the team header;
(3) view (read HEAD) view the current head element.
Public class queue {// queue array private long queuearr []; // The frontend subscript of the queue private int front; // The end subscript of the queue private int rear; // queue size private int maxsize; // number of elements in the queue private int nitems; // initialize a queue with the size public Queue (INT size) {queuearr = new long [size]; maxsize = size; front = 0; Rear =-1; nitems = 0 ;}// insert public void insert (long value) {// The queue is full if (Rear = maxSize-1) rear =-1; queuearr [+ rear] = value; nitem S ++;} // Delete public long remove () {long temp = queuearr [Front ++]; If (front = maxsize) Front = 0; nitems --; return temp;} // return the first element of the queue public long peakfront () {return queuearr [Front];} // determines whether it is empty public Boolean isempty () {return nitems = 0 ;}// determine whether the public Boolean isfull () {return nitems = maxsize ;}// return the number of elements in the queue public int size () {return nitems;} public void print () {for (INT I = front; I <fr ONT + nitems; I ++) {system. out. print (queuearr [I] + "");} system. out. println ();} public static void main (string [] ARGs) {queue q = new Queue (10); While (! Q. isfull () {long value = (long) (math. Random () * 100); q. insert (value) ;}q. Print (); While (! Q. isempty () {q. Remove (); q. Print ();} system. Out. println (Q. isempty ());}}
Output:
10 15 76 88 13 16 52 37 80 39
15 76 88 13 16 52 37 80 39
76 88 13 16 52 37 80 39
88 13 16 52 37 80 39
13 16 52 37 80 39
16 52 37 80 39
52 37 80 39
37 80 39
80 39
39
True
Iii. priority queue
A priority queue is a set of 0 or more elements. Each element has a priority. operations performed on the priority queue are (1) searched (2) insert a new element (3) delete an element. Generally, the search operation is used to search for the element with the highest priority, and the delete operation is used to delete the element. Elements with the same priority can be processed in the FIFO order or any priority.
A priority queue is another queue different from a first-in-first-out queue. Each time an element is retrieved from the queue, it has the highest priority.
public class PriorityQueue { private int nItems; private long pqArr[]; private int maxSize; public PriorityQueue(int size){ maxSize = size; pqArr = new long[size]; nItems = 0; } public void insert(long value){ int i; if(nItems == 0) pqArr[nItems++] = value; else{ for(i = nItems-1;i >= 0;i--){ if(value < pqArr[i]){ pqArr[i+1] = pqArr[i]; } else break; } pqArr[i+1] = value; nItems++; } } public long remove(){ return pqArr[--nItems]; } public boolean isEmpty(){ return nItems == 0; } public boolean isFull(){ return nItems == maxSize; } public void print(){ for(int i = 0;i < nItems;i++) System.out.print(pqArr[i]+" "); System.out.println(); } public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(10); while(!pq.isFull()){ long value = (long)(Math.random()*100); pq.insert(value); } pq.print(); }}
Output:
9 12 17 17 43 45 50 73 85 89
Summary:
1. stacks, queues, and priority queues are data structures that are often used to simplify certain program operations.
2. In these data structures, only one data item can be accessed.
3. The stack allows access to the last inserted data item.
4. An important operation in the stack is to insert (Press) A data item at the top of the stack and remove (pop up) A data item from the top of the stack.
5. the queue only allows access to the first inserted data item.
6. An important operation of the queue is to insert data items at the end of the queue and remove data items from the queue header.
7. The queue can be implemented as a cyclic queue. It is based on arrays, And the array subscript can be retrieved from the end of the array to the starting position of the array.
8. The priority queue allows the smallest (or sometimes the largest) data items to be accessed.
9. An important operation of priority queue is to insert new data items in sequence and remove the data items with the smallest keyword.
11. These data structures can be implemented using arrays or other mechanisms (such as linked lists.