stacks and queues are dynamic collections, and the entry and exit of elements is well-defined. The stack specifies that the element is advanced-out (FILO), and the queue specifies that the element is FIFO. Implementations of stacks and queues can be implemented using arrays and linked lists. In the standard module library STL has the specific application, may refer to http://www.cplusplus.com/reference/.
The basic operation of the stack consists of a stack push and a stack pop, a stack top pointer top, a pointer to the latest stack of elements, both the stack and the stack operation are from the top of the stack.
The basic operations of the queue include the queued enqueue and the dequeue, with the queue head head and the tail tail pointer. Elements are always out of the team, from the end of the team. In order to make reasonable use of space, we can use the array to realize the efficient utilization of the queue space.
Simple array implementation of the stack:
#include <iostream>using namespace std;struct stack{int *s;int stacksize;int top;};/ /init stackvoid init_stack (Stack *s) {s->stacksize = 100;s->s = (int*) malloc (sizeof (int) *s->stacksize);//s- >s = new int (); s->top =-1;} int Stack_empty (stack s) { return ((0 = = s.stacksize)? 1:0);} void Push_stack (Stack *s, int x) {if (s->top = = s->stacksize) cout << "Up to Overflow" << endl;else{s-> Top++;s->s[s->top] = x;s->stacksize++;}} void Pop_stack (Stack *s) {if (0 = = s->stacksize) cout << "down to Overflow" << endl;else{s->top--;s->s tacksize--;}} int Top_stack (stack s) {return s.s[s.top];} int main () {stack s;init_stack (&s); for (int i = 0; i <; i++) {Push_stack (&s, i);} for (int i = 0; i < 5; i++) {Cout<<top_stack (s) << "";p op_stack (&s);}}
The simple implementation of the queue is next written. Study for a night, very tired very tired.
Introduction to algorithm------------stack simple array implementation