Definition of stacks
a stack is a linear table that can only be inserted or deleted at one end.
The one end of the table that allows insertions and deletions is called the top of the stack . The current position of the top of the stack is dynamic and is indicated by the stack-top pointer. The other end of the table is called the bottom of the stack . When there is no data element in the stack, it is called an empty stack . The insert operation of the stack is often referred to as a stack or stack, and the delete operation of the stack is often referred to as a fallback or stack.
Stack ofParticularity lies inrestricting the insertion and deletion of data elements can only be done at one end of the linear table, so the features of the stackis a LIFO (last in first out ), and the stack is abbreviated as a LIFO linear table.
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7F/33/wKiom1cWQWSiIYBRAABOCEz1WE0075.jpg "title=" Stack "alt= "Wkiom1cwqwsiiybraabocez1we0075.jpg"/>
Stack.h
#pragma oncetemplate <class t>class stack{public:stack () //constructor: _arr (NULL), _top (0), _capacity (0) {}~stack () //destructor {if (_arr) {Delete[] _arr;}} Public:void push (const t& x) //Insert {_ Checkcapacity (); _arr[_top++] = x;} Void pop () //Delete {assert (_top > 0); --_top;} Size_t size () //size {return _top;} Bool empty () //determine if the stack is empty {//return _top == 0;if (_top <= 0) {return true;} Elsereturn false;} T& top () //get stack top element {return _arr[_top - 1] ;} Protected:void _checkcapacity () {IF&NBSP; (_arr == null) {_capacity = 5;_arr = new t[_capacity];return;} else if (_top == _capacity) {_capacity *= 2; t* tmp = new t[_capacity];for (Size_t i = 0;i < _top;++i) {tmp[i] = _arr[i];} Delete[] _arr;_arr = tmp;}} protected:t* _arr;size_t _top;size_t _capacity;}; Void teststack () //test Case {stack<char> s;s.push (' a '); S.Push (' B '); s. Push (' C '); S.push (' d ');while (!s.empty ()) {cout << s.top () << " "; s. Pop ();} Cout << endl;}
Definition of a queue
a queue , which is also a linear table with limited operations, which restricts the insertion at one end of the table and deletes at the other end of the table.
One end of the insert operation is called the tail of the queue (rear), and the one end of the delete operation is called the first or the team head (front).
Inserting new elements into the queue is called entering or queuing, and the new element is queued and becomes a new tail element; Removing elements from a queue is called a team or a drop, and after the element is out of the team, the successor element becomes the first element of the team.
The special feature of the team is that the insertion can only be done at one end of the table (not in), and the deletion can only be done at the other end of the table (only out), so the operation principle of the queue is first-in, or FIFO , in the second-in-a-out form.
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7F/31/wKioL1cWRdKh1pLzAAA_O__jOD4871.jpg "title=" queue "alt = "Wkiol1cwrdkh1plzaaa_o__jod4871.jpg"/>
Queue.h
#pragma oncetemplate <class T>struct Node{T _data; node<t>* _next; Node (const t& x): _data (x), _next (NULL) {}};template <class t>class queue{ Public:queue (): _head (null), _tail (null), _size (0) {}~queue () {if (_head) {delete _head;}} Public:void push (const t& x) //Insert Data {if at the end of the team (_head == null) //queue is empty condition {_head = _tail = new Node<T> (x);} Else{_tail->_next = new node<t> (x); _tail = _tail->_next;} ++_size;} Void pop () //delete data {assert (_head);if (_head == _tail) in team header //queue has only one data condition {delete _head;_head = _tail = null;} Else{node<t>* tmp = _head;_head = _head->_next;delete tmp;} --_size;} Bool empty () {return _head == null;} Size_t size () {return _size;} T& getfront () //Get team Header data {assert (_head); return _head->_data;} T& gettail () //Get the end of the queue data {assert (_tail); return _tail->_data;} protected:node<t>* _head; node<t>* _tail;size_t _size;}; Void testqueue () //test Case {Queue<int> q;q.push (1); Q.Push (2); Q. Push (3); Q.push (4);while (!q.empty ()) {Cout << q.getfront () << " "; Q. Pop ();} Cout << endl;}
If there is insufficient, please advise ~
This article is from the "clown" blog, make sure to keep this source http://clown5.blog.51cto.com/10730975/1765587
Stacks and teams