棧和隊列是操作受限的線性表,好像每本講數(shù)據(jù)結(jié)構(gòu)的數(shù)都是這么說的。有些書按照這個思路給出了定義和實現(xiàn);但是很遺憾,這本書沒有這樣做,所以,原書中的做法是重復(fù)建設(shè),這或許可以用不是一個人寫的這樣的理由來開脫。
順序表示的棧和隊列,必須預(yù)先分配空間,并且空間大小受限,使用起來限制比較多。而且,由于限定存取位置,順序表示的隨機存取的優(yōu)點就沒有了,所以,鏈式結(jié)構(gòu)應(yīng)該是首選。
棧的定義和實現(xiàn)
#ifndef stack_h
#define stack_h
#include "list.h"
template class stack : list//棧類定義
{
public:
void push(type value)
{
insert(value);
}
type pop()
{
type p = *getnext();
removeafter();
return p;
}
type gettop()
{
return *getnext();
}
list ::makeempty;
list ::isempty;
};
#endif
隊列的定義和實現(xiàn)
#ifndef queue_h
#define queue_h
#include "list.h"
template class queue : list//隊列定義
{
public:
void enqueue(const type &value)
{
lastinsert(value);
}
type dequeue()
{
type p = *getnext();
removeafter();
isempty();
return p;
}
type getfront()
{
順序表示的棧和隊列,必須預(yù)先分配空間,并且空間大小受限,使用起來限制比較多。而且,由于限定存取位置,順序表示的隨機存取的優(yōu)點就沒有了,所以,鏈式結(jié)構(gòu)應(yīng)該是首選。
棧的定義和實現(xiàn)
#ifndef stack_h
#define stack_h
#include "list.h"
template class stack : list//棧類定義
{
public:
void push(type value)
{
insert(value);
}
type pop()
{
type p = *getnext();
removeafter();
return p;
}
type gettop()
{
return *getnext();
}
list ::makeempty;
list ::isempty;
};
#endif
隊列的定義和實現(xiàn)
#ifndef queue_h
#define queue_h
#include "list.h"
template class queue : list//隊列定義
{
public:
void enqueue(const type &value)
{
lastinsert(value);
}
type dequeue()
{
type p = *getnext();
removeafter();
isempty();
return p;
}
type getfront()
{

