When using stacks and queues of standard libraries, first include the relevant header files
#include <stack>
#include <queue>
The definition stack is as follows:
Stack<int> Stk;
The definition queue is as follows:
Queue<int> Q;
The stack provides the following actions
S.empty () returns true if the stack is empty, otherwise returns Falses.size () returns the number of elements in the stack s.pop () removes the top element of the stack without returning its value s.top () returns the element at the top of the stack, But does not delete the element S.push () pushes a new element at the top of the stack
The queue provides the following actions
Q.empty () returns true if the queue is empty, otherwise returns Falseq.size () returns the number of elements in the queue Q.pop () deletes the first element of the queue but does not return its value Q.front () returns the value of the first element of the team, But do not delete the element Q.push () at the end of the team press into the new element Q.back () returns the value of the tail element of the queue, but does not delete the element
c++stack (Stack)
It is an adaptation of a container that implements an advanced post-out data structure (FILO)
You need to include the #include<stack> header file when using this container;
The sample code that defines the stack object is as follows:
stack<int>s1;
stack<string>s2;
The basic operations of stack are:
1. Into the stack: such as S.push (x);
2. Out of stack: such as S.pop (). Note: The stack operation simply removes the element at the top of the stack and does not return the element.
3. Access the top of the stack: such as s.top ();
4. Determine the empty stack: such as S.empty (). Returns True when the stack is empty.
5. Access the number of elements in the stack, such as s.size ();
Here's a simple example:
#include <iostream> #include <stack> using namespace std; int main (void) { stack<double>s;//defines a stack for (int i=0;i<10;i++) s.push (i); while (!s.empty ()) { printf ("%lf\n", S.top ()); S.pop (); } cout<< "The number of elements in the stack is:" <<s.size () <<endl; return 0; }
the definition of the stack:a stack is a linear table that restricts inserts or deletions only at the end of the table, so the end of the table becomes the top of the stack, and accordingly, the header end becomes the bottom of the stack, and the stack without any elements is called an empty stack. stack modification follows the principle of LIFO, so the stack is called the LIFO linear table, or the LIFO structure. The stack generally uses an array as its storage structure, which avoids using pointers, simplifies the program, and of course the array needs to pre-declare the size of the static data area, but this is not a problem, because even if it is frequently in and out of the stack operation, the actual number of stack elements at any time is not much, Reserving a stack that is large enough but not taking up too much space is not very difficult, and if you cannot do that, then the memory-saving approach is to use the list storage stack.
Basic operation of stack with linear table implementation
#include <iostream> #include <cstdio> using namespace std; typedef struct STACKNODE//Defines the structure of the chain stack {int data;//data field Stacknode *next;//the next node's pointer field}stacknode,*st Ack Initializes a chained stack (returns the head node of a chained stack) stack initstack () {stack stack= (stack) malloc (sizeof (Stacknode)); stack->next=null; return stack; }//Enter stack void Push (Stack stack,int newdata) {//Determine if NULL if (Stack==null) { printf ("stack uninitialized, use \ n after initialization"); Return }//Find the last node Stacknode *lastnode=stack; while (Lastnode->next) {lastnode=lastnode->next; } lastnode->next= (stacknode*) malloc (sizeof (stacknode*)); lastnode->next->data=newdata; lastnode->next->next=null; printf ("Into the stack success!") \ n "); }//out stack int Pop (stack stack) {//Determine if stack is empty if (!stack->next) {printf ("stack is empty, cannot stack \ n"); Return-1;//-1 is just a custom error code}//Find the last node of the money one node//tempnode: The last node of the previous node Stacknode *TEMPN Ode=stack; while (Tempnode->next->next) {tempnode=tempnode->next; } int data=tempnode->next->data; Free (tempnode->next); tempnode->next=null; return data; } int main () {Stack stack=initstack (); Push (stack,3),//3 push (stack,4),//4 into stack push (stack,5),//5 into the stack printf ("%d\n", Pop (stack)); printf ("%d\n", Pop (stack)); printf ("%d\n", Pop (stack)); printf ("%d\n", Pop (stack));//4th Time out stack, should be error return 0; }
C + + queue (queue)
The definition of the queue template class is in the <queue> header file.
The queue is very similar to the stack template, and the queue template also needs to define two template parameters, one element type, one container type, the element type is necessary, the container type is optional, and the default is the Dqueue type .
The sample code that defines the queue object is as follows:
queue<int>q1;
queue<double>q2;
The basic operations of the queue are:
1. Queue: As Q.push (x): The x element is connected to the end of the line;
2. Out of line: The first element of the popup queue, such as Q.pop (), does not return the value of the element;
3, access to the first element of the team: such as Q.front ()
4, access to the tail elements, such as q.back ();
5, the number of elements in the access team, such as Q.size ();
Two. Priority queue
In the <queue> header file, a very useful template class Priority_queue (priority queue) is also defined, the difference between the priority queue and the queue is that the priority queue is not queued in the queued order, Instead, the queue is prioritized by the priority order of the elements in the queues (the default is large, or you can specify your own precedence by specifying operators), which is a large heap by default .
The Priority_queue template class has three template parameters, element types, container types, and comparison operators. The latter two can be omitted, the default container is a vector, the default operator is less, that is, a small forward row, large back row (out of the queue when the tail of the elements out of the team).
The sample code that defines the Priority_queue object is as follows:
Priority_queue<int >q1;
Priority_queue<pair<int,int> >q2;
Priority_queue<int,vector<int>,greater<int> >q3;//Define a small first-out team
Priority_queue basic operations are the same as the queue
The most difficult thing for beginners when using priority_queue is how to define a comparison operator. If it is a basic data type, or a class that has a comparison operator defined, you can directly use the less operator and the greater operator of the STL-the default is to use the less operator, a small forward row, a large first-out team. If you want to define your own comparison operator, there are several ways to do this: the overloaded comparison operator. The priority queue attempts to take two elements x and y into the comparison operator (for less operators, call X<y, the greater operator, call X>y), if the result is true, X is in front of y, Y will precede X, and vice versa, then Y will be in front of X, and X will first be out of the team.
Let's look at this simple example:
#include <iostream> #include <queue> #include <stdlib.h> using namespace std; Class T {public : int x, y, Z; T (int a,int b,int C): X (a), Y (b), Z (c) { } }; BOOL operator< (const t&t1,const t&t2) { return t1.z<t2.z; } int main (void) { priority_queue<t>q; Q.push (T (4,4,3)); Q.push (T (2,2,5)); Q.push (T (1,5,4)); Q.push (T (3,3,6)); while (!q.empty ()) { T t=q.top (); Q.pop (); cout<<t.x<< "" <<t.y<< "" <<t.z<<endl; } System ("Pause"); return 1; }
The output result is
Note that this is in the order of Z from the big to the small team.
If we change the comparison operator overload in the above example to:
BOOL operator< (const T &t1,const t &t2)
{
Return t1.z>t2.z;
}
Then the resulting output will be in the order of z from small to large team.
Introduction and use of C + + stacks and queues