Code:
linkqueue.hpp//Queue class #pragma once#include "linklist.hpp" template <typename t>class linkqueue{public:linkqueue ( ); ~linkqueue ();p ublic:int Clear (); int append (t &t); int Retieve (t &t); int header (t &t); int length (); Protected:linklist<t> *m_list;}; Template <typename T>linkqueue<t>::linkqueue () {m_list = new linklist < T >;} Template <typename T>linkqueue<t>::~linkqueue () {clear ();d elete m_list;m_list = NULL;} Template <typename t>int linkqueue<t>::clear () {T t;while (M_list->getlen () > 0) {m_list->del (0, T) ;} return 0;} Template <typename t>int linkqueue<t>::append (t &t) {return M_list->insert (T, M_list->getlen ()) ;} Template <typename t>int linkqueue<t>::retieve (t &t) {return M_list->del (M_list->getlen ()-1, T) ;} Template <typename t>int linkqueue<t>::header (t &t) {return m_list->get (0, t);} Template <typename t>int linkqueue<t>::length () {return M_LIST->getlen ();}
main.cpp//Queue Class Test program # include <iostream> #include <cstdio> #include "linkqueue.hpp" using namespace std; struct Student{char name[32];int age;}; void Play () {Student S1, s2, s3;s1.age = 21;s2.age = 22;s3.age = 23; Linkqueue<student> LQ; Create Queue lq.append (S1); Into the queue lq.append (S2); Lq.append (S3); Student Tmp;lq.header (TMP); cout << "header of the queue:" << tmp.age << endl;cout << "Length of queue : "<< lq.length () << endl;while (lq.length () > 0) {lq.retieve (TMP); cout << tmp.age <<";} cout << endl;lq.clear ();} int main () {play (); return 0;}
for the design and implementation of the linked list class, see my other two articles:C + + linked list template classes,Linear chain-type storage design and implementation-API implementation
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Queue class-Chained storage