Stacks and queues are operated linear tables, as if the number of data structures is said to be said. Some books have been defined and implemented in accordance with this idea; but unfortunately, this book does not do this, so the practice in the original book is repeated construction, which may be used to freely use such a reason to write.
The stack and queue represented in the order must be pre-allocated, and the space size is limited, and the use is limited. Moreover, due to the defined access location, the random access to the random access is not, so, the chain structure should be preferred.
Stack definition and implementation
#ifndef stack_h
#define stack_h
#include "list.h"
Template
{
PUBLIC:
Void Push (Type Value)
{
INSERT (VALUE);
}
TYPE POP ()
{
TYPE P = * getNext ();
REMOVEAFTER ();
Return P;
}
TYPE GETTOP ()
{
Return * getNext ();
}
List
List
}
#ENDIF
Queue definition and implementation
#ifndef queue_h
#define queue_h
#include "list.h"
Template
{
PUBLIC:
Void enqueue (const type & value)
{
LastInsert (Value);
}
TYPE Dequeue ()
{
TYPE P = * getNext ();
REMOVEAFTER ();
ISempty ();
Return P;
}
TYPE GETFRONT ()
{
Return * getNext ();
}
List
List
}
#ENDIF
test program
#ifndef stackte_h
#define stacktest_h
#include "stack.h"
Void stacktest_int ()
{
Cout << Endl << "Integer Stack Test" << Endl;
Cout << Endl << "Constructs a Empty Stack" << ENDL;
Stack
Cout << "will be 1 ~ 20 into the stack, then put the stack" << ENDL;
For (int i = 1; i <= 20; i ) a.push (i);
While (! a.isempty ()) cout << a.pop () << '';
Cout << Endl;
}
#ENDIF
#ifndef queetest_h
#define queuetest_h
#include "queue.h"
Void queuetest_int ()
{
COUT << Endl << "Integer Queue Test" << Endl;
COUT << Endl << "Constructs an air queue" << endl; queue
Cout << "will join 1 ~ 20, then queue" << Endl;
For (int i = 1; i <= 20; i ) a.enqueue (i);
While (! a.isempty ()) cout << a.dequeue () << '';
Cout << Endl;
}
#ENDIF
[Postscript] Nothing to say, you can clearly see that on the basis of single-chain list, the implementation of stacks and queues is so simple, this is also the biggest reason why I have dissatisfied with the original book.