Skip to content

Instantly share code, notes, and snippets.

@sudheesh001
Created October 18, 2013 07:44
Show Gist options
  • Save sudheesh001/7037878 to your computer and use it in GitHub Desktop.
Save sudheesh001/7037878 to your computer and use it in GitHub Desktop.
C++ implementation of queue
#include <iostream>
using namespace std;
template <class etype>
class queue
{
class qnode
{
public:
etype element;
qnode *next;
qnode( etype e = 0 ): element( e ), next( NULL )
{ }
};
qnode *first;
qnode *last;
public:
queue( ): first( NULL ), last( NULL )
{ }
inline bool isempty( ) const
{
return first == NULL;
}
void enqueue( const etype x );
etype dequeue( );
void makeempty( );
};
template <class etype>
void queue <etype> :: enqueue( const etype x )
{
if ( isempty( ) )
{
first = new qnode( x );
last = first;
}
else
{
qnode *p = new qnode( x );
last->next = p;
last = last->next;
}
}
template <class etype>
etype queue<etype>::dequeue( )
{
etype x;
qnode *p;
if ( !isempty( ) )
{
x = first->element;
p = first;
first = first->next;
delete p;
return x;
}
}
template <class etype>
void queue<etype>::makeempty( )
{
while ( !isempty( ) )
dequeue( );
}
/*
template <class etype>
void
queue<etype>::
display( )
{
if ( isempty( ) )
cout << "\n\nThe queue is empty.";
for ( qnode *p = first; p != NULL; p = p->next )
cout << p->element << " ";
}
int main( )
{
queue<int> q;
q.enqueue( 10 );
q.enqueue( 45 );
q.enqueue( 34 );
q.enqueue( 12 );
q.enqueue( 89 );
q.enqueue( 17 );
q.display( );
for ( int i = 0; i < 3; i++ )
q.dequeue( );
q.display( );
q.makeempty( );
q.display( );
cin.get( );
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment