c语言stack,编写一个STACK的类

以前学数据结构时写的。

21c92f7342edc52acb5684b0b88bdcec.png程序代码:SeqStack.h文件

//SeqStack.h SeqStack模板类的定义

template

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

SeqStack:: SeqStack(int i)

{

iMaxSize=i;

iTop=-1;

Array=new EleType[iMaxSize];

}

template

SeqStack:: ~SeqStack()

{

delete [] Array;

}

template

int SeqStack:: Pop(EleType &TopElement)

{

if(iTop==-1)

{

std::cout<

return 1;

}

else

{

TopElement=Array[iTop];

iTop--;

return 0;

}

}

template

int SeqStack:: Push(EleType &TopElement)

{

if(iTop==iMaxSize-1)

{

std::cout<

return 1;

}

else

{

iTop++;

Array[iTop]=TopElement;

return 0;

}

}

template

inline int SeqStack:: IsEmpty() const

{

return iTop==-1;

}

template

inline int SeqStack:: IsFull() const

{

return iTop==iMaxSize-1;

}

template

int SeqStack:: ShowElement() const

{

if(iTop==-1)

{

std::cout<

return 1;

}

else

{

cout<

for(int i=iTop;i>=0;i--)

cout<

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 MySeqStack(5);

int iChoice=0;

while(iChoice=GetChoice())

{

switch(iChoice)

{

case 1: {

if(MySeqStack.IsEmpty())

cout<

else

cout<

break;

}

case 2: {

int a;

if(!MySeqStack.IsFull())

{

cout<

cin>>a;

MySeqStack.Push(a);

MySeqStack.ShowElement();

}

else

cout<

break;

}

case 3: {

int a;

if(!MySeqStack.IsEmpty())

{

MySeqStack.Pop(a);

cout<

MySeqStack.ShowElement();

}

else

cout<

break;

}

default: cout<

}

}

return 0;

}

int GetChoice()

{

int iChoice=0;

cout<

cin>>iChoice;

return iChoice;

}

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

(1) stdafx.h文件:

// stdafx.h : 标准系统包含文件的包含文件,

#pragma once

#include

(2) LinkStack.h文件:

// LinkStack模板类的定义

template

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 LinkStack

{

protected:

Node * 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

LinkStack:: LinkStack()

{

Head=new Node;

Head->SetNext(NULL);

}

template

LinkStack:: ~LinkStack()

{

Node *pNode;

if(!IsEmpty())

{

pNode=Head->GetNext();

Head->SetNext(pNode->GetNext());

delete pNode;

}

delete Head;

}

template

int LinkStack:: Pop(ValueType &TopElement)

{

if(this->IsEmpty())

{

std::cout<

return 1;

}

else

{

Node *pNode;

pNode=Head->GetNext();

Head->SetNext(pNode->GetNext());

TopElement=pNode->GetValue();

delete pNode;

return 0;

}

}

template

int LinkStack:: Push(const ValueType TopElement)

{

Node *pNode;

pNode=new Node;

pNode->SetValue(TopElement);

pNode->SetNext(Head->GetNext());

Head->SetNext(pNode);

return 0;

}

template

inline bool LinkStack:: IsEmpty() const

{

return Head->GetNext()==NULL;

}

template

int LinkStack:: ShowElement() const

{

if(IsEmpty())

{

std::cout<

return 1;

}

else

{

cout<

Node *pNode,*temp;

for(pNode=Head->GetNext();pNode->GetNext()!=NULL;pNode=pNode->GetNext())

{

cout<GetValue()<

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 MyLinkStack;

int iChoice=0;

while(iChoice=GetChoice())

{

switch(iChoice)

{

case 1: {

if( MyLinkStack.IsEmpty())

cout<

else

cout<

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<

MyLinkStack.ShowElement();

}

else

cout<

break;

}

default: cout<

}

}

return 0;

}

// 获取用户选择

int GetChoice()

{

int iChoice=0;

cout<

cin>>iChoice;

return iChoice;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值