Generic Queue : generic queue « template « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » template » generic queue 
13.14.1.Generic Queue
#include<iostream.h>
#include<iomanip.h>
#include<ctype.h>

template<class Type>
class queue
{
private:
   Type size;
   Type start;
   Type end;
   Type *data;
public:
    queue()
    {
      size=20;
      start=end=0;
      data=new Type[size];
    }

    queue(Type n)
    {
      size=n;
      start=end=0;
      data=new Type[size];
    }

       ~queue()
       {
         delete data;
       }
    void put(Type value)
    {
      if((start+1)size==end)
      {
       cout<<"\n***Queue is full!***\n";
       return ;
       }
       data[start]=value;
       start=(start+1)size;
       cout<<"You have put a data into the queue!\n";
       return;
    }

    Type get()
    {
      Type value;
      if(start==end)
      {
        cout<<"\n***Queue is empty!***\n";
        return(0);
      }
      value=data[end];
      end=(end+1)size;
      cout<<"\n Get "<<value<<" from the queue!\n";
      return(value);
    }

    void clear()
    {
      start=end;
      cout<<"\n ***Queue is empty!***\n";
    }

    void ShowQueue()
    {
       if(start==end)
       {
        cout<<"\n The queue has no data!\n";
        return;
       }
       Type i;
       cout<<"\n The content of queue:\n";
       for(i=end;i!=start;i=((i+1)size))
          cout<<setw(5)<<data[i];
       cout<<"\n\n";
    }

};

main()
{
  queue<char> ss(5);
  char value = 'a';
  ss.put(value);
  ss.put('b');
  value=ss.get();
  ss.clear();
  ss.ShowQueue();
}
You have put a data into the queue!
You have put a data into the queue!

 Get a from the queue!

 ***Queue is empty!***

 The queue has no data!
13.14.generic queue
13.14.1.Generic Queue
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.