Title: Two buyers implement a queue, please implement its two functions Appendtail/deletehead
Algorithm Ideas:
queue: Press the element into buyers S1;//appendtail
out Team: First determine whether the S2 is empty, if not empty, directly pop up the top of the stack element; otherwise, the elements in the S1 are popped up and pressed into the buyers S2, the last element ejected and out of the team;//deletehead
Determines whether the queue is empty (empty ()): Returns True if there are no elements in the S1/S2, otherwise returns false;//empty
take the team head element (Top ()): first determine whether the S2 is empty, if not empty, take the top element of the S2 stack, otherwise, the elements in the S1 are popped up and pressed into buyers S2, and then take s2 of the top element of the stack;//top
Two buyers are empty, that is, when the queue is empty;//Special Case
in the end, if the queue is not NULL, the queue will be printed from the team head to the end of the team, verifying that the result is as you want it to be.//print
test Case: 1, data not all deleted;//test-----> Head to Tail 1->2->3->4->10->99->65
2. Delete all data;//test1-----> Head to Tail 1->2->3->4->10->99->65
Description: I use the template to write, so you can instantiate various types;
The
code is as follows:
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > #include <iostream> #include <stack>using namespace std;template <class t>class queue{public: Queue (); T Top (); bool Empty (); ~queue (); void Appendtail (const t& _data); void Deletehead (); void Print ();p rivate:stack<t > s1;//Insert node buyers stack<t> s2;//Delete node buyers/Fetch stack top element buyers/print queue element buyers};template <class T>queue<t>::queue () {} Template <class T>queue<t>::~queue () {while (! Empty ()) {Deletehead ();}} Template <class t>t queue<t>::top () {if (Empty ()) {cout << "Top:queue is null!" << Endl;return (T)- 1;} if (S2.empty ()) {while (!s1.empty ()) {S2.push (S1.top ()); S1.pop ();}} return S2.top ();} Template <class T>bool Queue<t>::empty () {return (S1.empty () && s2.empty ());} Template <class t>void queue<t>::appendtail (const t& _data) {S1.push (_data);} Template <class t>void queue<t>::d Eletehead () {if (Empty ()) {cout << "Deletehead:queue is null!" < < Endl;reTurn;} if (!s2.empty ()) {S2.pop ();} Else{while (!s1.empty ()) {T tmp = S1.top (); S1.pop (); if (!s1.empty ()) {S2.push (TMP);}}} Template <class t>void queue<t>::P rint () {if (Empty ()) {cout << "Print:queue is null!" << Endl; return;} while (! Empty ()) {while (!s2.empty ()) {cout << s2.top () << "," S2.pop ();} while (!s1.empty ()) {S2.push (S1.top ()); S1.pop ();}} cout << Endl;} void Test () {queue<int> q1;q1.appendtail (1); Q1.appendtail (2); Q1.appendtail (3); Q1.appendtail (4); Q1.deletehead (); Q1.appendtail (Q1.appendtail); Q1.appendtail (+); Q1.deletehead (); Q1. Print ();} void Test1 () {queue<int> q1;q1.appendtail (1); Q1.appendtail (2); Q1.appendtail (3); Q1.appendtail (4); Q1.deletehead (); Q1.appendtail (Q1.appendtail); Q1.appendtail (+); Q1.deletehead (); Q1.deletehead (); Q1.deletehead (); Q1.deletehead (); Q1.deletehead (); Q1. Print (); Q1.deletehead (); Q1. Print (); Q1.deletehead (); Q1. Top ();} int main () {cout << "Test:" << Endl; Test (); cout << "Test1:" << endL Test1 (); return 0;} </span>
The results are as follows:
Two buyers implementation of a queue