I've implemented both as follows. Could someone be kind enough to just play around with various inputs and let me know if there are any bugs?
#include <iostream>
using namespace std;
/***************************************************************************/
class AStack {
public:
AStack(int);
~AStack();
void push(int);
int pop();
int top();
bool isEmpty();
void Flush();
private:
int capacity ;
int* a;
int index = -1; // Index of the top most element
};
AStack::AStack(int size) {
a = new int[size];
capacity = size;
}
AStack::~AStack() {
delete[] a;
}
void AStack::push(int x) {
if (index == capacity - 1) {
cout << "\n\nThe stack is full. Couldn't insert " << x << "\n\n";
return;
}
a[++index] = x;
}
int AStack::pop() {
if (index == -1) {
cout << "\n\nNo elements to pop\n\n";
return -1;
}
return a[index--];
}
int AStack::top() {
if (index == -1) {
cout << "\n\nNo elements in the Stack\n\n";
return -1;
}
return a[index];
}
bool AStack::isEmpty() {
return (index == -1);
}
void AStack::Flush() {
if (index == -1) {
cout << "\n\nNo elements in the Stack to flush\n\n";
return;
}
cout << "\n\nFlushing the Stack: ";
while (index != -1) {
cout << a[index--] << " ";
}
cout << endl << endl;
}
/***************************************************************************/
class LLStack {
public:
struct Node {
int data;
Node* next;
Node(int n) {
data = n;
next = 0;
}
Node(int n, Node* node) {
data = n;
next = node;
}
};
LLStack();
~LLStack();
void push(int);
int pop();
int top();
bool isEmpty();
void Flush();
private:
Node* head;
};
LLStack::LLStack() {
head = 0;
}
LLStack::~LLStack() {
this->Flush();
}
void LLStack::push(int x) {
if (head == 0) head = new Node(x);
else {
Node* temp = new Node(x, head);
head = temp;
}
}
int LLStack::pop() {
if (head == 0) {
cout << "\n\nNo elements to pop\n\n";
return -1;
}
else {
Node* temp = head;
int n = temp->data;
head = temp->next;
delete temp;
return n;
}
}
int LLStack::top() {
if (head == 0) {
cout << "\n\nNo elements in the stack\n\n";
return -1;
}
else {
return head->data;
}
}
bool LLStack::isEmpty() {
return (head == 0);
}
void LLStack::Flush() {
while (head != 0) {
Node* temp = head;
head = head->next;
delete temp;
}
}
/***************************************************************************/
int main() {
// Insert code here to play around
return 0;
}