函数调用栈,出栈
1个回答
展开全部
#ifndef Node_
#define Node_
template <class T> class LinkedStack;
template <class T> class LinkedQueue;
template <class T>
class Node {
friend LinkedStack<T>;
friend LinkedQueue<T>;
private:
T data;
Node<T> *link;
};
#endif
// file lstack.h
// linked stack
#include "node.h"
#include "xcept.h"
template<class T>
class LinkedStack {
public:
LinkedStack() {top = 0;}
~LinkedStack();
bool IsEmpty() const {return top == 0;}
bool IsFull() const;
T Top() const;
LinkedStack<T>& Add(const T& x);
LinkedStack<T>& Delete(T& x);
private:
Node<T> *top; // pointer to top node
};
template<class T>
LinkedStack<T>::~LinkedStack()
{// Stack destructor..
Node<T> *next;
while (top) {
next = top->link;
delete top;
top = next;
}
}
template<class T>
bool LinkedStack<T>::IsFull() const
{// Is the stack full?
try {Node<T> *p = new Node<T>;
delete p;
return false;}
catch (NoMem) {return true;}
}
template<class T>
T LinkedStack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds();
return top->data;
}
template<class T>
LinkedStack<T>& LinkedStack<T>::Add(const T& x)
{// Add x to stack.
Node<T> *p = new Node<T>;
p->data = x;
p->link = top;
top = p;
return *this;
}
template<class T>
LinkedStack<T>& LinkedStack<T>::Delete(T& x)
{// Delete top element and put it in x.
if (IsEmpty()) throw OutOfBounds();
x = top->data;
Node<T> *p = top;
top = top->link;
delete p;
return *this;
}
#define Node_
template <class T> class LinkedStack;
template <class T> class LinkedQueue;
template <class T>
class Node {
friend LinkedStack<T>;
friend LinkedQueue<T>;
private:
T data;
Node<T> *link;
};
#endif
// file lstack.h
// linked stack
#include "node.h"
#include "xcept.h"
template<class T>
class LinkedStack {
public:
LinkedStack() {top = 0;}
~LinkedStack();
bool IsEmpty() const {return top == 0;}
bool IsFull() const;
T Top() const;
LinkedStack<T>& Add(const T& x);
LinkedStack<T>& Delete(T& x);
private:
Node<T> *top; // pointer to top node
};
template<class T>
LinkedStack<T>::~LinkedStack()
{// Stack destructor..
Node<T> *next;
while (top) {
next = top->link;
delete top;
top = next;
}
}
template<class T>
bool LinkedStack<T>::IsFull() const
{// Is the stack full?
try {Node<T> *p = new Node<T>;
delete p;
return false;}
catch (NoMem) {return true;}
}
template<class T>
T LinkedStack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds();
return top->data;
}
template<class T>
LinkedStack<T>& LinkedStack<T>::Add(const T& x)
{// Add x to stack.
Node<T> *p = new Node<T>;
p->data = x;
p->link = top;
top = p;
return *this;
}
template<class T>
LinkedStack<T>& LinkedStack<T>::Delete(T& x)
{// Delete top element and put it in x.
if (IsEmpty()) throw OutOfBounds();
x = top->data;
Node<T> *p = top;
top = top->link;
delete p;
return *this;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询