链表实现的栈

实现了主要函数

编译环境 visual stdio2005 

#include<iostream>
#include<assert.h>
using namespace std;
template <class T>
class stack;

template <class T>
class node
{
 friend class stack<T>;
private:
 T data;
 node<T> *link;
public:
 node(T d=0,node<T> *l=NULL):data(d),link(l){}
};

template<class T>
class stack
{
private:
 node<T> *top;
public:
 stack(T d){top=new node<T>(d);}
 void push(const T& x)
 {
  top=new node<T>(x,top);
 }
 T *pop()
 {
  assert(!IsEmpty ( ));
  node<T>*p;T *q;
  p=top;q=&p->data;
  top=top->link;
  delete p;
  return q;
 }
 T GetTop ( )const
 {
  assert(!IsEmpty ( ));
  return top->data;
 }
    int IsEmpty ( ) const
 {
  return top==NULL;
 }
 void makeempty()
 {node<T> *p;
   while(top!=NULL)
  { p=top;
   top=top->link;
   delete p;
  }
 }
 ~stack()
 {
   while(top!=NULL)
  { node<T> *p=top;
   top=top->link;
   delete p;
  }
 }
};


void main()
{
 stack<float> s(2);
 s.push(3);
 cout<<s.GetTop();
 s.pop();
 cout<<s.GetTop();
 cout<<s.IsEmpty();
 s.makeempty();
 cout<<s.IsEmpty();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值