数据结构--栈

一、用数组实现

头文件stack.h:

#ifndef STACK_H
#define STACK_H
 
 
class Stack
{
public:
    Stack(int maxSize);
    ~Stack();
    bool isEmpty();
    bool isFull();
    bool Top(int &x);
    bool push(int x);
    bool pop();
    void clear();
 
private:
    int maxTop;
    int top;
    int *s;
};
 
#endif // STACK_H
 

stack.cpp

#include "stack.h"
#include <iostream>
using namespace std;
 
Stack::Stack(int maxSize)
{
    maxTop = maxSize;
    top = -1;
    s = new int[maxSize];
}
 
Stack::~Stack()
{
    delete []s;
}
 
bool Stack::isEmpty()
{
    return top == -1;
}
 
bool Stack::isFull()
{
    return top == maxTop;
}
 
bool Stack::Top(int &x)
{
    if(isEmpty()){
        cout<<"Empty"<<endl;
        return false;
    }
    x = s[top];
    return true;
}
 
bool Stack::push(int x)
{
    if(isEmpty()){
        cout<<"Empty"<<endl;
        return false;
    }
    s[++top] = x;
    return true;
}
 
bool Stack::pop()
{
    if(isEmpty()){
        cout<<"Empty"<<endl;
        return false;
    }
    cout<<s[top]<<" poped!"<<endl;
    top--;
    return true;
}
 
 

测试:

#include <QCoreApplication>
#include <iostream>
using namespace std;
#include "stack.h"
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Stack s(5);
    s.push(3);
    s.push(2);
    s.push(1);
    s.push(4);
 
    return a.exec();
}
 

二、用链表实现

头文件stack.h

#ifndef PTSTACK_H
#define PTSTACK_H
#include <iostream>
using namespace std;
 
class Node
{
public:
    Node();
    Node(int e);
 
private:
    int element;
    Node *link;
    friend class ptStack;
};
 
 
class ptStack
{
public:
    ptStack(int maxSize);
    ~ptStack();
    bool isEmpty();
    bool isFull();
    bool push(int e);
    bool pop();
    bool getTop(int &x);
    int getLength();
 
private:
    int maxTop;
    int count;
    Node *Top;
};
 
#endif // PTSTACK_H
 

stack.cpp:

#include "ptstack.h"
#include <iostream>
using namespace std;
 
ptStack::ptStack(int maxSize)
{
    maxTop = maxSize;
    Top = NULL;
    count = 0;
}
 
ptStack::~ptStack()
{
    for(int i = 0;i < count;i++){
        Node *p = Top;
        Top = p->link;
        delete p;
    }
}
 
bool ptStack::isEmpty()
{
    return count == 0;
}
 
bool ptStack::isFull()
{
    return count == maxTop;
}
 
bool ptStack::push(int e)
{
    if(isFull()){
        cout<<"The stack if full!"<<endl;
        return false;
    }
    Node *newNode = new Node(e);
    if(isEmpty()){
        Top = newNode;
    }
    else{
        newNode->link = Top;
        Top = newNode;
    }
    cout<<e<<" has been pushed!"<<endl;
    count++;
    return true;
}
 
bool ptStack::pop()
{
    if(isEmpty()){
        cout<<"The stack is empty!"<<endl;
        return false;
    }
    Node *p = Top;
    Top = p->link;
    cout<<p->element<<" has been poped!"<<endl;
    delete p;
    count--;
    return true;
}
 
bool ptStack::getTop(int &x)
{
    if(isEmpty()){
        cout<<"The stack is empty!"<<endl;
        return false;
    }
    Node *temp = Top;
    x = temp->element;
    return true;
}
 
int ptStack::getLength()
{
    return count;
}
 
 
Node::Node()
{
    link = NULL;
    element = 0;
}
 
Node::Node(int e)
{
    link = NULL;
    element = e;
}
 
 

测试:

#include <QCoreApplication>
#include <iostream>
using namespace std;
#include "ptstack.h"
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    ptStack s(5);
    s.push(3);
    s.push(6);
    s.push(1);
    s.pop();
 
    return a.exec();
}
 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值