Sicily 2286. Stack Implementation

2286. Stack Implementation

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB , Framework Judge

Description

Implement the following Stack:

  typedef int Stack_entry;

class Stack {
public:
// Standard Stack methods
Stack();
bool empty() const;
/* Returns true if the stack is empty, otherwise, returns false.
*/
int size() const;
/* Returns the number of elements in the stack.
*/
void push(const Stack_entry &item);
/*item is pushed into the stack and it becomes the new top element.
*/
void pop();
/*The top item is removed if the stack is not empty.
Otherwise, nothing happens.
*/

 Stack_entry & top() const;
/* The top element is returned by item if the stack is not empty,
and the stack remains unchanged.
Nothing happens if the stack is empty.
*/
// Safety features
~Stack();
Stack(const Stack &original);
void operator =(const Stack &original);
};


typedef Stack MyStack;

//or if your are using templates

typedef Stack<int> MyStack;

 

Hint

Submit your implementations only.

 

Problem Source

ADTs: Implementations and Applications

// Problem#: 2286
// Submission#: 3279937
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
typedef int Stack_entry;

class Stack {
public:
    Stack_entry s[10000];
    int pos;
    Stack() {
        pos = 0;
    }
    bool empty() const {
        return pos == 0;
    }
    int size() const {
        return pos;
    }
    void push(const Stack_entry &item) {
        s[pos++] = item;
    }
    void pop() {
        if (pos == 0) return;
        pos--;
    }
    Stack_entry top() const {
        if (pos == 0) return -1;
        return s[pos - 1];
    }
    ~Stack() {}
    Stack(const Stack & original) {
        *this = original;
    }
    void operator = (const Stack & original) {
        for (int i = 0; i < original.pos; i++) {
            s[i] = original.s[i];
        }
        pos = original.pos;
    }
};

typedef Stack MyStack;                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值