- C++中棧有順序棧和鏈棧之分。在順序棧中,定義了棧的棧底指針(存儲空間首地址base)、棧頂指針top以及順序存儲空間的大小stacksize(個人感覺這個數據成員是能夠不用定義的)
//順序棧數據結構C++類聲明(基類)
template <typename ElemType>
class SqStack
{
public:
void clear(); //把順序棧置空
int getLength(); //求順序棧中元素個數
int getstackSize(); //返回當前已分配的存儲空間的大小
Status getTop(ElemType & e); //讀棧頂的元素
bool isEmpty(); //推斷順序棧是否為空
SqStack<ElemType> operator =(SqStack<ElemType> rightS); //重載賦值運算符的定義
Status pop(ElemType & e); //彈出棧頂元素到e
void push(ElemType & e ); //在棧頂壓入元素e
//*****************************以下為系統自己主動調用構造函數及析構函數聲明******************************//
SqStack(); //順序棧構造函數
virtual ~SqStack();//順序棧析構函數
SqStack (const SqStack<ElemType>& otherS);//順序棧拷貝初始換構造函數
protected:
ElemType *base;
ElemType *top;
int stackSize;//順序存儲空間的大小
};
- 而對於鏈棧來說,它僅僅定義棧頂指針。
template<typename ElemType>
class Linkstack
{
private:
class LinkNode
{
public:
ElemType data;
LinkNode *next;
};
typedef LinkNode * NodePointer;
public:
void clear();
int getlength();
void display();
void randLinkStack();
Linkstack <ElemType> operator = (Linkstack <ElemType> rightS);
protected:
NodePointer top;
};
事實上這二者的差別是由順序表和鏈表的存儲結構決定的,在空間上,順序表是靜態分配的,而鏈表則是動態分配的;就存儲密度來說:順序表等於1,而鏈式表小於1。可是鏈式表能夠將非常多零碎的空間利用起來;順序表查找方便。鏈式表插入和刪除時非常方便。
順序表的這樣的靜態存儲的方式,決定了必須至少得有首地址和末地址來決定一個空間。否則,不知道查找到哪了。鏈式表每一個節點存儲了下一個節點的指針信息,故,對於鏈棧來說,僅僅須要一個top指針就可以查找到整個棧。
另外,順序棧和鏈棧的top指針有差別,順序棧的top指針指向棧定的空元素處,top-1才指向棧定元素,而鏈棧的top指針相當於鏈表的head指針一樣,指向實實在在的元素。
另外附自己寫的順序棧和鏈棧的隨機產生函數:
//順序棧:
template<typename ElemType>
void MyStack<ElemType>::RandStack()
{
int *p;
ElemType n;
ElemType Elem[11];
srand(time(NULL));
n=rand()%10+1;
cout<<"產生的隨機棧的深度為:"<<n<<endl;
cout<<"產生的隨機棧元素為:"<<endl;
for (int i = 0; i < n; i++)
{
Elem[i]=rand()%100+1;
cout<<Elem[i]<<" ";
}
cout<<endl;
base=new ElemType[n];
assert(base!=0);
top=base;
stackSize=n;
for (int j = 0; j < n; j++)
*(base+j)=Elem[j];
top=base+n;
cout<<"隨機產生的棧為:"<<endl;
for (int i = 0; i < stackSize; i++)
cout<<" "<<*(base+i);
cout<<endl;
cout<<" ♂";
for (int i = 1; i <stackSize ; i++)
{
setw(4);
cout<<" ";
}
cout<<" ♂"<<endl;
cout<<" base";
for (int i = 1; i <stackSize ; i++)
{
setw(4);
cout<<" ";
}
cout<<" top"<<endl;
}
template<typename ElemType>
void MyStack<ElemType>::display()
{
int n=top-base;
cout<<"當前棧為:"<<endl;
for (int i = 0; i < n; i++)
cout<<" "<<*(base+i);
cout<<endl;
cout<<" ♂";
for (int i = 1; i <n ; i++)
{
setw(4);
cout<<" ";
}
cout<<" ♂"<<endl;
cout<<" base";
for (int i = 1; i <n ; i++)
{
setw(4);
cout<<" ";
}
cout<<" top"<<endl;
}
//鏈棧
template<typename ElemType>
void Linkstack<ElemType>::display()
{
NodePointer r;
int num=0;
r=top;
while (r)
{
cout<<r->data<<" ";
r=r->next;
num++;
}
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"↑" ;
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"top"<<endl;
}
template <typename ElemType>
void Linkstack<ElemType>::randLinkStack()
{
ElemType elem[11];
srand(unsigned(time(NULL)));
int n;
n=rand()%10+1;
cout<<"the number of the stack is:"<<n<<endl;
cout<<"the elements here are:";
for (int i = 0; i < n; i++)
{
elem[i]=rand()%100+1;
cout<<elem[i]<<" ";
}
cout<<endl;
NodePointer p,s;
p=NULL;
for (int i = 0; i < n; i++)
{
s=new(LinkNode);
assert(s!=NULL);
s->data=elem[i];
s->next=p;
p=s;
}
top=p;
cout<<"the stack produced is:"<<endl;
NodePointer r;
int num=0;
r=top;
while (r)
{
cout<<r->data<<" ";
r=r->next;
num++;
}
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"↑" ;
cout<<endl;
for(int i=0;i<num-1;i++)
cout<<setw(4)<<" ";
cout<<"top"<<endl;
}