Customize the class template to implement queue

Source: Internet
Author: User

# Include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; <br/> // incorrect example <br/> // template <class T> class listitem; <br/> // template <class T> class list {<br/> // public: <br/> // list <t> (); <br/> // list <t> (const list <t> &); <br/> // list <t> & operator = (const list <t> &); <br/> //~ List (); <br/> // void insert (listitem * PTR, T value _); <br/> // listitem * Find (T value _); <br/> // <br/>/PRIVATE: <br/> // listitem * front; <br/> // listitem * end; <br/> // }; <br/> template <class type> <br/> class queueitem {<br/> public: <br/> queueitem (const type & T): item (t), next (0) {}< br/> type item; <br/> queueitem * next; <br/>}; <br/> template <class type> <br/> class queue {<br/> Public: <br/> Qu EUE (): Head (0), tail (0) {}< br/> Queue (const queue & Q): Head (0), tail (0) {<br/> copy_elems (Q ); <br/>}< br/> template <class type2> // here, the object is declared to support type conversion <br/> queue <type> & operator = (const queue <type2> &); <br/> ~ Queue () {destroy () ;}< br/> type & Front () {return head-> item ;}< br/> const type & Front () const {return head-> item ;}< br/> void push (const type &); <br/> void POP (); <br/> bool empty () const {<br/> return head = 0; <br/>}< br/> PRIVATE: <br/> queueitem <type> * head; <br/> queueitem <type> * tail; <br/> void destroy (); <br/> void copy_elems (const queue &); <br/> }; <br/> template <class type> <br/> void queue <Type >:: push (const type & Val) <br/>{< br/> queueitem <type> * PT = new queueitem <type> (VAL ); <br/> If (empty () <br/> head = tail = pt; <br/> else {<br/> tail-> next = pt; <br/> tail = pt; <br/>}< br/> template <class type> <br/> void queue <type> :: pop () <br/>{< br/> queueitem <type> * P = head; <br/> head = head-> next; <br/> Delete P; <br/>}< br/> template <class type> <br/> void queue <type >:: destroy () <br/> {<Br/> while (! Empty () <br/> POP (); <br/>}< br/> template <class type> <br/> void queue <type> :: copy_elems (const queue & orig) <br/>{< br/> for (queueitem <type> * PT = orig. head; PT; Pt = Pt-> next) <br/> push (Pt-> item ); <br/>}< br/> template <class type> <br/> template <class type2> // The purpose of this statement is to support type conversion. <br/> queue <type> & queue <type>:: Operator = (const queue <type2> & orig) <br/> {<br/> If (void *) This = (void *) & orig) {<br/> * th Is; <br/>}< br/> queue <type2> TMP (orig); <br/> destroy (); // Delete </P> <p> while (! TMP. empty () <br/>{< br/> push (TMP. front (); <br/> TMP. pop (); <br/>}< br/> return * This; <br/>}< br/> int main () <br/>{ <br/> queue <int> qi; <br/> short S = 43; <br/> int I = 23; <br/> Qi. push (s); <br/> Qi. push (I); <br/> queue <float> AI; <br/> AI = qi; <br/> queue <int> Bi; <br/> return 0; <br/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.