对顶栈的设计

我是初级选手,没学过C++,只接触C语言和数据
是“对顶栈”

第1个回答  2007-12-19
这一是段用链式结构实现栈功能的代码,不知是不是你要的:
#include "StackInChain.h"
#include <stdlib.h>
//构造一个空栈 S。
void InitStack(sNode *&HS) { HS=NULL; }
//清空 所有数据,使S为空栈。
void ClearStack(sNode *&HS)
{ sNode *Head,*del; Head=HS;
while(Head){ del=Head; Head=Head->next; delete del; }
HS=NULL;
}
//若栈 S 为空栈,则返回TRUE,否则返回FALSE。
bool StackEmpty(sNode *HS)
{ return HS==NULL; }
// 返回S的栈顶元素。
ElemType Peek(sNode *HS)
{ if(HS==NULL)
{ cerr<<"栈已经没有数据!"<<endl; exit(1); }
//返回栈顶元素的值
return HS->data;
}
//入栈操作,在当前栈顶元素之后插入新的栈顶元素。
void Push(sNode *&HS, ElemType item)
{ sNode *p=new sNode;
if(p==NULL){
cerr<<"动态存储分配失败"<<endl;
exit(1);
}
p->data=item; p->next=HS; HS=p;
}
//出栈操作,删除 S 的栈顶元素,并其值。
ElemType Pop(sNode *&HS)
{ if(HS==NULL){
cerr<<"栈已经没有数据!"<<endl;
exit(1);
}
//返回栈顶元素的值
sNode *del;
del=HS;
HS=HS->next;
ElemType temp=del->data;
delete del;
return temp;
}
第2个回答  2007-12-19
#include<iostream.h>
const int maxstack=10;
enum Error_code{success,fail,overflow,underflow};
template < class entry_stack>
class stack
{private:
int count;
entry_stack entry[maxstack];
public:
stack();
bool empty()const;
Error_code push(const entry_stack &item);
Error_code pop();
entry_stack top();
};
template <class entry_stack>
stack<entry_stack>::stack()
{count=0;}
template <class entry_stack>
bool stack<entry_stack>::empty()const
{
return(count==0);
}
template<class entry_stack>
Error_code stack<entry_stack>::pop()
{if(empty()) return underflow;
count--;
return success;
}
template <class entry_stack>
Error_code stack<entry_stack>::push(const entry_stack &item)
{if(count==maxstack)return overflow;
entry[count++]=item;
return success;
}
template <class entry_stack>
entry_stack stack<entry_stack>::top()
{
for(int i=count;i>=0;i--)
return entry[i-1];
}
void main()
{stack<char>mystack;
char item;
for(int i=0;i<maxstack;i++)
{cin>>item;
mystack.push(item);
}cout<<endl<<endl;
while(!mystack.empty())
{
cout<<mystack.top()<<endl;
mystack.pop();
}cout<<endl;}本回答被提问者和网友采纳
第3个回答  2007-12-20
对顶栈?好像没有这种栈?难道你说的是队列?
相似回答