Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Queue.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include "Header1.h"
- using namespace std;
- template<class T>
- class node
- {
- public:
- T num;
- node<T> *next = NULL;
- };
- template<class T>
- class queue
- {
- public:
- queue<T>(){ cout << "***head= " << head << endl; }
- void push(T);
- void pop();
- private:
- node<T> *head = new(node<T>);
- };
- template<class T>
- void queue<T>::push(T n)
- {
- node<T> *pos = head;
- while (pos->next != NULL)
- {
- pos = pos->next;
- }
- pos->next = new(node<T>);
- pos->next->num = n;
- cout << "***push " << n << " to " << pos->next << endl;
- }
- template<class T>
- void queue<T>::pop()
- {
- if (head->next == NULL)
- {
- cout << "empty queue!\n";
- return;
- }
- node<T> *del = head->next;
- head->next = head->next->next;
- cout << "pop " << del->num << " frome " << del << endl;
- delete(del);
- }
- void main()
- {
- cout << "new qu1\n";
- queue<int> qu1;
- cout << "start int push\n";
- qu1.push(1);
- qu1.push(2);
- qu1.push(3);
- qu1.push(4);
- cout << "start int pop\n";
- qu1.pop();
- qu1.pop();
- qu1.pop();
- qu1.pop();
- qu1.pop();
- queue<char> qu2;
- cout << "start char push\n";
- qu2.push('f');
- qu2.push('u');
- qu2.push('c');
- qu2.push('k');
- cout << "start char pop\n";
- qu2.pop();
- qu2.pop();
- qu2.pop();
- qu2.pop();
- qu2.pop();
- queue<float> qu3;
- cout << "start float push\n";
- qu3.push(1.1);
- qu3.push(2.2);
- qu3.push(3.3);
- qu3.push(4.4);
- cout << "start float pop\n";
- qu3.pop();
- qu3.pop();
- qu3.pop();
- qu3.pop();
- qu3.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement