This chapter covers the three types of data storage: stacks, queues, and priority queues.
Different types of structures
Programmer's Tools
Arrays are data storage structures that have been introduced, and other structures (linked lists, trees, and so on) are suitable for data logging in data applications.
However, this chapter is about data structures and algorithms that are more used as programmers ' tools. They are used as an aid to the idea algorithm, not as a complete data storage tool. These data structures have a much shorter life cycle than those of database types. They are created during the execution of a program operation, usually to perform a particular task, and when completed, they are destroyed.
Restricted access
In an array, you can access the data item as long as you know the subscript. or sequential access.
In the data structure of this chapter, access is restricted, that is, only one data item can be read or deleted at a particular time (unless "cheat");
These structural interfaces have been designed to enhance this limited access. Access to other data items (theoretically) is not allowed.
More abstract
Stacks, queues, and priority queues are more abstract structures than arrays and other data-storage structures. Stacks, queues, and priority queues are defined primarily through interfaces that indicate what they can do, and whose primary implementation mechanism is invisible to the user.
For example, the main mechanism of a stack can be implemented by an array, but it can also be implemented with a linked list. The internal implementation of the priority queue can be implemented using an array or a special book ——— heap.
Stack
The stack allows access to only one data item: The last inserted data item. This data entry is removed to access the second-to-last inserted data, and so on.
Java code for Stacks
public class stack { private int maxsize; private long[] stackArray; private int top; public stack (int s) { maxsize = s; stackArray = new long[maxSize]; top=-1; } public void push (LONG J) { stackarray[++top] = j; } public long pop () { return stackarray[top--]; } public long peek () { return stackarray[top]; } pUblic boolean isempty () { return (top==-1); } public boolean isfull () { return top==maxsize-1; }}
public static void Main (string[] args) {Stack thestack = new Stack (10); Thestack.push (10); Thestack.push (20); Thestack.push (30); Thestack.push (40); while (!thestack.isempty ()) {Long value = Thestack.pop (); System.out.print (value); System.out.print (""); }}//Output: 40302010
Efficiency of the Stack
The time spent on stack operations is not dependent on the number of data items in the stack, so the operation time is short. Stacks do not require comparison and move operations.
Queue
The word "queue" is the English "Platoon" (a way to wait for service).
A queue is a data structure that is a bit like a stack, except that the first inserted data item in the queue is first removed (first in, out, FIFO), and in the stack, the last inserted data item is removed first.
Java code for queues
public class queue { private int maxsize; private long[] queueArray; private int front; private int rear; private int nItems; public queue (int s) { maxsize = s; queuearray = new long[ maxsize]; front = 0; rear = -1; nItems = 0; } public void insert (LONG J) { if (rear == maxsize-1) rear = -1; queuearray[++rear]=j; nitems++; } public long remove () { long temp = queuearray[front++]; if (front==maxsize) front = 0; nItems--; return temp; } public long peekfront () { return queuearray[front]; } public boolean isempty () { return nitems ==0; } public boolean isfull () { return nitems == maxsize; } public int size () { return nitems; }}
Public static void main (String[] args) { queue thequeue = new queue (5); thequeue.insert (; ) thequeue.insert ( thequeue.insert); Thequeue.insert (+); thequeue.remove (); Thequeue.remove (); thequeue.remove (); Thequeue.insert ( thequeue.insert); thequeue.insert (70); thequeue.insert ( thequeue.insert); thequeue.insert (+); while (!thequeue.isempty ()) { long n = thequeue.remove (); system.ouT.print (n); system.out.print (" "); } system.out.println ("");}
Efficiency of the queue
As with the stack, the time complexity of inserting and removing data items in a queue is O (1).
Dual-ended queues
A double-ended queue is a queue with both ends at the end. Each end of the queue can insert data items and remove data items. These methods can be called Insertleft () and Insertright (), as well as Removeleft () and Removeright ().
A double-ended queue is a multipurpose data structure compared to a stack or a queue, and in a container class library, it is sometimes used to provide both stacks and queues with two-ended queues. However, double-ended queues are not as common as stacks and queues ....
Priority queue
Priority queues are data structures that are more specialized than stacks and queues. But it is useful in a lot of situations. Like a normal queue, the priority queue has a head and a tail, and it also removes data items from scratch. However, in the priority queue, the data items are ordered by the value of the keyword, so that the smallest item of the keyword (or the data item with the largest number of keywords in some implementations) is always on the team header. When data items are inserted, they are inserted in the proper order to ensure the order of the queues.
Priority Queue Java code
public class priorityqueue { private int maxsize; private long[] queueArray; private int nItems; public priorityqueue (int s) { maxsize = s; queuearray = new long[maxSize]; nItems = 0; } public void inser (Long item) { int i; if (nitems==0) queuearray[nitems++] = item; else { for (i = nitems-1; i>=0; i--) { if ( Item>queuearray[i]) queueArray[i+1] = queueArray[i]; else break; } queueArray[i+1] = item; nItems++; } } &nbSp; public long remove () { return queuearray [--nitems]; } public long Meekmin () { return queuearray[nitems-1]; } public boolean isempty () { return nItems ==0; } public boolean isfull () { return nitems==maxsize; }}
public static void Main (string[] args) {priorityqueue queue = new Priorityqueue (5); Queue.inser (30); Queue.inser (50); Queue.inser (10); Queue.inser (40); Queue.inser (20); while (!queue.isempty ()) {Long item = Queue.remove (); System.out.print (item); System.out.print (""); } System.out.println ("");} Output: 10 20 30 40 50
Randomly insert 5 data items in the main () method, and then remove and display them. The smallest data item is always removed first, so the output is:
10 20 30) 40 50
Efficiency of Priority queues
In the priority queue implemented in this chapter, an O (N) time is required for an insert operation, and an O (1) time is required for deletion.
Parsing an arithmetic expression
Suffix expression method
The daily arithmetic expression is to place the operator (operator) (+,-,*,/) between two operands (operands) (a number or a letter representing a number). Because the operator is written in the middle of the operand, it is the expression of infix notation.
In the suffix expression "also known as the Polish reverse expression (Reverse Polish natation), or RPN", it was invented by a Polish mathematician, with the operator following the two operands. In this way, A+b becomes ab+,a/b become ab/. The more complex tables are:
| Infix expression |
Suffix expression |
| A+b-c |
ab+c- |
| A*b/c |
ab*c/ |
| A+b*c |
abc*+ |
| A*b+c |
ab*c+ |
| A * (B+C) |
abc+* |
| A*b+c*d |
ab*cd*+ |
| (a+b) * (c-d) |
ab+cd-* |
| ((a+b) *c)-D |
ab+c*d- |
| A+b (c-d/(e+f)) |
abcdef+/-*+ |
Suffix expression job search .....
Summary
Stacks, queues, and priority queues are data structures that are often used to simplify some program operations.
In these data structures, only one item of data can be accessed.
The stack allows access to the last inserted data item.
An important operation in the stack is inserting (pressing) a data item at the top of the stack and removing (ejecting) a data item from the top of the stack.
The queue only allows access to the first inserted data item.
The important operation of the queue is to insert the data item at the end of the team and remove the data item in the team header.
A queue can be implemented as a circular queue, which is based on an array, and the array subscript wraps around the end of the array to the beginning of the array.
The priority queue allows access to the smallest (or sometimes largest) data item.
The important operation of the priority queue is to insert new data items in an orderly manner and to remove the data items with the smallest keywords.
These data structures can be implemented in arrays or by other mechanisms, such as linked lists.
Ordinary arithmetic expressions are expressed in infix notation, and the reason for this designation is that the operator is written in the middle of two operations.
In postfix notation, the operator follows the two operands.
Arithmetic expression evaluation is usually converted to a suffix expression before the value of the suffix expression is evaluated.
Stacks are useful tools in the process of converting infix expressions to postfix expressions and for the value of suffix expressions.
Java Data structures and algorithms (fourth stacks and queues)