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;