Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29118
  • 博文数量: 16
  • 博客积分: 600
  • 博客等级: 上士
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-21 13:21
文章分类

全部博文(16)

文章存档

2011年(1)

2008年(15)

我的朋友
最近访客

分类: C/C++

2008-03-28 22:17:34

#ifndef HEADER_STACK
#define HEADER_STACK
#include<iostream.h>
#define stacksize 10

#include<iostream.h>

    
template<class T>
class Stack
{
private:
    T data[stacksize];
        int top;
public:
    Stack();
    int push(const T& x);
    T pop();
    T gettop();
    int Gettop();
    bool Empty();
    void clear();
};
template<class T>
int Stack<T>:: Gettop()
{
    return top;
}
template<class T>
Stack<T>::Stack()
{
  top=-1;
}//Stack::就没有必要

template<class T>//这里要明白这个模板的声明什么时候用到它.只要有变量有类型用到它,才要加,如果是像

int Stack<T>::push(const T& x)//由于这是形参,用到了它

{
    if (top==stacksize-1)
    {
        cout<<"表满"<<endl;
        return 0;
    }
else
{
    top++;
    data[top]=x;
    cout<<data[top];
    cout<<"ABC"<<endl;
    return 1;
}
}
template<class T>
T Stack<T>::pop()
{
    if (top==-1)
    {
    cout<<"栈空"<<endl;    return NULL;
    }
    else
    {
        T x;
     x=data[top];
         top--;
       cout<<x<<endl;
     return x;
    }
}
template<class T>
T Stack<T>::gettop()
{
    if (top==-1)
    {
        cout<<"栈空 "<<endl;
        return NULL;
    }
else {
     T n;
     n=data[top];
     return n;
}
}
template<class T>
bool Stack<T>::Empty()//作为外部的访问

{
    if(top==-1)
    {cout<<"栈空"<<endl;return true;}
    else return false;
}
template<class T>
void Stack<T>::clear()
{
    top=-1;
}
#endif

 

 

#include
#include
#include
#include"stack.h"
#define N 5
void main()
{
        Stack st;
       int a[N];
        for(int i=0;i           cin>>a[i];
      for(int last=0,j=0;j        {    
         for(int p=last+1;p<=a[j];p++)
             st.push(p);
            if(last            if(st.gettop()!=a[j])
   {cout<<"这组数据不可能组成一列车"<    st.pop();
           
        
        }
  cout<<"可以组成一列车"<}

阅读(481) | 评论(0) | 转发(0) |
0

上一篇:字符记数

下一篇:类实现广义表

给主人留下些什么吧!~~