数据结构编程题
- 提问者网友:战皆罪
- 2021-04-11 15:20
- 五星知识达人网友:何以畏孤独
- 2021-04-11 15:42
"LinkStack.h"头文件
template<class Telem> class LinkStack;
template<class Telem>
class Node{
Telem data;
Node<Telem> *next;
friend class LinkStack<Telem>;
public :
Node<Telem>(Node<Telem>*n=NULL,Telem d=0):next(n),data(d){}
};
template<class Telem>
class LinkStack{
Node<Telem> * top;
public:
Telem gettop();
bool pop();
bool empt();
void push(Telem el);
void clear();
int leng();
};
template<class Telem>
bool LinkStack<Telem>::empt()
{
if(top==NULL)return true;
else return false;
}
template<class Telem>
void LinkStack<Telem>::push(Telem el)
{
Node<Telem>*p=new Node<Telem>(top,el);
top=p;
}
template<class Telem>
bool LinkStack<Telem>::pop()
{
if(top==NULL)
{
exit(0);
return false;
}
Node<Telem>*p=top;
//Telem data=top->data;
top=top->next;
delete p;
return true;
}
template<class Telem>
Telem LinkStack<Telem>::gettop()
{
if(top==NULL)return NULL;
return top->data;
}
template<class Telem>
void LinkStack<Telem>::clear()
{
while(!this->empt())
this->pop();
}
template<class Telem>
int LinkStack<Telem>::leng()
{
int i=0;Node<Telem>*p=top;
while(p)
{
p=p->next;
i++;
}
return i;
}
- 1楼网友:慢性怪人
- 2021-04-11 16:51
进栈算法 void push(int x) {if(top>=MAX-1) printf( " overflow " ); else {top++; stack[top]=x;} }
出栈算法
int pop( ) { int x; if(top==-1) printf( “栈下溢出!“ ) ; else { x=stack[top]; top -― ; } return x ; }
取栈顶元素
int gettop() {if(top==-1) printf( “栈下溢出!“ ); else return stack[top] ; }
清空栈 int clear_stack(sqstack *s)
{ s->base = NULL; s->top = NULL; s->stacksize = 0; return 0; }
销毁
void destroy_seqstack(struct seqstack *s) { free(s); } 你可以自己在想想怎么具体实现