| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1255 人关注过本帖
标题:编写一个STACK的类
只看楼主 加入收藏
linzhaoshen
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-11-26
收藏
 问题点数:0 回复次数:4 
编写一个STACK的类
栈的ADT(至少)具有以下特点:
?    可建立空堆栈;
?    可从栈顶删除数据项(弹出);
?    可将数据项添加到栈顶(压入);
?    可查看堆栈是否为空;
编写一个STACK的类,该类要符合上述ADT要求。
搜索更多相关主题的帖子: STACK 堆栈 ADT 编写 数据 
2007-12-09 09:38
zbqf109
Rank: 1
等 级:新手上路
帖 子:289
专家分:0
注 册:2006-12-31
收藏
得分:0 
最好去C++版

坚决不跟用TC的人打交道!
2007-12-09 09:58
mfkpgfsbihc
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-11-25
收藏
得分:0 
template<class type>class stack;
template<class type>class node{    //结点类定义
    friend class stack<type>;
    private:
        type data;
        node<type> *next;
    public:
        node(type d,node<type> *ls=NULL):data(d),next(ls){}
};

template<class type>class stack{  //栈类定义
    private:
        node<type> *top;    //栈顶指针
    public:
        stack():top(NULL){}  //建立空栈
        void push(const type &item);//进栈
        type pop(void);            //出栈,返回栈顶元素
        int IsEmpty()const{return top==NULL;}//判断栈是否为空
};

template<class type>
void stack<type>::push(const type &item){
    top=new node<type>(item,top);
}

template<class type>
type stack<type>::pop(){
    if(pop==NULL){
        cout<<"NULL STACK.\n";
        exit(1);
    }
    node<type> *p=top;
    type item=top->data;
    top=p->next;
    delete p;
    return item;
}
2007-12-09 10:27
linzhaoshen
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-11-26
收藏
得分:0 
非常感谢!
2007-12-09 14:40
VxWorks
Rank: 3Rank: 3
来 自:WindRiver
等 级:论坛游民
威 望:6
帖 子:859
专家分:68
注 册:2007-11-24
收藏
得分:0 
以前学数据结构时写的。
程序代码:
SeqStack.h文件
//SeqStack.h   SeqStack模板类的定义

template<class EleType>
class SeqStack
{
protected:
    EleType * Array;
    int iTop,iMaxSize;

public:
    SeqStack(int i);
    ~SeqStack();
    int Pop(EleType &);
    int Push(EleType &);
    int IsEmpty() const;
    int IsFull() const;
    int ShowElement() const;
};

SeqStack.cpp文件
//SeqStack.cpp  SeqStack模板类的实现



#include "SeqStack.h"
#include "iostream"
using namespace std;

template<class EleType>
SeqStack<EleType>:: SeqStack(int i)
{
    iMaxSize=i;
    iTop=-1;
    Array=new EleType[iMaxSize];
}
template<class EleType>
SeqStack<EleType>:: ~SeqStack()
{
    delete [] Array;
}
template<class EleType>
int SeqStack<EleType>:: Pop(EleType &TopElement)
{
    if(iTop==-1)
    {
        std::cout<<"\n栈已空!";
        return 1;
    }
    else 
    {
        TopElement=Array[iTop];
        iTop--;
        return 0;
    }
}

template<class EleType>
int SeqStack<EleType>:: Push(EleType &TopElement)
{
    if(iTop==iMaxSize-1)
    {
        std::cout<<"\n栈已满!";
        return 1;
    }
    else 
    {
        iTop++;
        Array[iTop]=TopElement;        
        return 0;
    }
}

template<class EleType>
inline int SeqStack<EleType>:: IsEmpty() const
{
    return iTop==-1;
}

template<class EleType>
inline int SeqStack<EleType>:: IsFull() const
{
    return iTop==iMaxSize-1;
}

template<class EleType>
int SeqStack<EleType>:: ShowElement() const
{
    if(iTop==-1)
    {
        std::cout<<"\n栈已空!";
        return 1;
    }
    else
    {
        cout<<'\n';
        for(int i=iTop;i>=0;i--)
            cout<<Array[i]<<'\t';
        return 0;
    }
}

Test_SeqStack.cpp文件
//Test_SeqStack.cpp  程序入口,对SeqStack模板类的检验
#include "SeqStack.h"
#include "SeqStack.cpp"
#include "iostream"
int GetChoice();

using namespace std;
int main()
{
    SeqStack<int> MySeqStack(5);
    int iChoice=0;

    while(iChoice=GetChoice())
    {
        switch(iChoice)
        {
            case 1:    {
                    if(MySeqStack.IsEmpty())
                        cout<<"栈已空!\n";
                    else
                        cout<<"栈未空!\n";
                    break;
                }
            case 2:    {
                    int a;
                    if(!MySeqStack.IsFull())
                    {
                        cout<<"你要入栈的整数是:";
                        cin>>a;
                        MySeqStack.Push(a);
                        MySeqStack.ShowElement();
                    }
                    else
                        cout<<"栈已满\n";
                    break;
                }
            case 3:    {
                    int a;        
                    if(!MySeqStack.IsEmpty())
                    {
                        MySeqStack.Pop(a);
                        cout<<"你出栈的整数是:" <<a<<endl;
                        MySeqStack.ShowElement();
                    }
                    else
                        cout<<"栈已空\n";
                    break;
                }
            default: cout<<"错误的输入\n";
        }
    }
    return 0;
}
int GetChoice()
{
    int iChoice=0;
    cout<<"\n\t\t1 判栈空\n\t\t2 入栈\n\t\t3 出栈\n\t\t0 退出\n\t";
    cin>>iChoice;
    return iChoice;
}

链栈的实现:建立新的工程Test_LinkStack.h,由stdafx.h,LinkStack.h,LinkStack.cpp,Test_LinkStack.cpp组成,内容如下:

(1)    stdafx.h文件:
// stdafx.h : 标准系统包含文件的包含文件,
#pragma once
#include <iostream>
(2)    LinkStack.h文件:
// LinkStack模板类的定义
template<class ValueType>
class Node
{
protected:
    ValueType Value;
    Node *Next;
public:
    Node()
    {Next=NULL;}
    ~Node()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    {}
    void SetValue(ValueType a)
    {Value=a;} 
    void SetNext(Node * a)
    {Next=a;}
    ValueType &GetValue()
    {return  Value;    }
    Node * GetNext()
    {return Next;}
};

template<class ValueType>
class LinkStack
{
protected:
    Node<ValueType> * Head;
    
public:
    LinkStack();
    ~LinkStack();
    int Pop(ValueType &);
    int Push(const ValueType );
    bool IsEmpty() const;
    int ShowElement() const;
};

(3)LinkStack.cpp文件 
//LinkStack.cpp    LinkStack模板类成员函数的实现
#include "stdafx.h"
#include "LinkStack.h"

template<class ValueType>
LinkStack<ValueType>:: LinkStack()
{
    Head=new Node<ValueType>;
    Head->SetNext(NULL);
}

template<class ValueType>
LinkStack<ValueType>:: ~LinkStack()
{
    Node<ValueType> *pNode;
    if(!IsEmpty())
    {
        pNode=Head->GetNext();
        Head->SetNext(pNode->GetNext());
        delete pNode;
    }
    delete Head;
}

template<class ValueType>
int LinkStack<ValueType>:: Pop(ValueType &TopElement)
{
    if(this->IsEmpty())
    {
        std::cout<<"栈已空!";
        return 1;
    }
    else 
    {
        Node<ValueType> *pNode;
        pNode=Head->GetNext();

        Head->SetNext(pNode->GetNext());
        TopElement=pNode->GetValue();
        delete pNode;
        return 0;
    }
}

template<class ValueType>
int LinkStack<ValueType>:: Push(const ValueType TopElement)
{
    Node<ValueType> *pNode;
    pNode=new Node<ValueType>;

    pNode->SetValue(TopElement);
    pNode->SetNext(Head->GetNext());
    Head->SetNext(pNode);
    return 0;
    
}

template<class ValueType>
inline bool LinkStack<ValueType>:: IsEmpty() const
{
    return Head->GetNext()==NULL;
}

template<class  ValueType>
int LinkStack<ValueType>:: ShowElement() const
{
    if(IsEmpty())
    {
        std::cout<<"\n栈已空!";
        return 1;
    }
    else
    {
        cout<<'\n';
        Node<ValueType> *pNode,*temp;
        for(pNode=Head->GetNext();pNode->GetNext()!=NULL;pNode=pNode->GetNext())
        {
            cout<<pNode->GetValue()<<'\t';
            temp=pNode->GetNext();
            pNode=temp;
        }
        return 0;
    }
}
(4)Test_LinkStack.cpp文件
//Test_LinkStack.cpp    程序入口,对LinkStack模板类的检验

#include "stdafx.h"
#include "LinkStack.cpp"

int GetChoice();
using namespace std;
int main()
{
    LinkStack<int>  MyLinkStack;

    int iChoice=0;

    while(iChoice=GetChoice())
    {
        switch(iChoice)
        {
            case 1:    {
                    if( MyLinkStack.IsEmpty())
                        cout<<"栈已空!\n";
                    else
                        cout<<"栈未空!\n";
                    break;
                }
            case 2:    {
                    int a;
                    
                    cout<<"你要入栈的整数是:";
                    cin>>a;
                    MyLinkStack.Push(a);
                    MyLinkStack.ShowElement();
                    
                    break;
                }
            case 3:    {
                    int a;
        
                    if(!MyLinkStack.IsEmpty())
                    {
                        MyLinkStack.Pop(a);
                        cout<<"你出栈的整数是:" <<a<<endl;
                        MyLinkStack.ShowElement();
                    }
                    else
                        cout<<"栈已空\n";
                    break;
                }
            default: cout<<"错误的输入\n";
        }
    }
    return 0;
}

// 获取用户选择
int GetChoice()
{
    int iChoice=0;
    cout<<"\n\t\t1 判栈空\n\t\t2 入栈\n\t\t3 出栈\n\t\t0 退出\n\t";
    cin>>iChoice;
    return iChoice;
}

为了防止世界被破坏,为了守护世界的和平,贯彻爱与真实的邪恶,可爱又迷人的反派角色,VxWorks!
Don't ask me any question.I'm just here to buy soy sauce.
2007-12-09 19:29
快速回复:编写一个STACK的类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020938 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved