patterncppMinor
Stack without use of STL - version 1.0
Viewed 0 times
withoutstlstackversionuse
Problem
This is a basic implementation of a stack. Can anyone please review it and let me know how I can improve this code and overall coding practice?
#include"iostream"
#define MAX_SIZE 5
using namespace std;
class Mystack
{
private:
int A[MAX_SIZE];
int top;
public:
Mystack();
void push(int x);
void pop();
void topElement();
void print();
void isEmpty();
};
Mystack::Mystack()
{
top = -1;
}
void Mystack::push(int x)
{
if(top == MAX_SIZE-1)
{
cout 0)
{
cout > ch;
switch (ch)
{
case 1:
cout > num;
s1.push(num);
break;
case 2:
cout << "Get the TOP Element" << endl;
s1.topElement();
break;
case 3:
cout << "Check Empty" << endl;
s1.isEmpty();
break;
case 4:
cout << "POP the element" << endl;
s1.pop();
break;
case 5: exit(0);
case 6:
s1.print();
break;
}
}
system("pause");
}Solution
This review is in response to the request for help on "overall coding practice." It does not delve into the syntax or semantics of the C++ language. There are three levels: Computer Science, Architecture, Variable Names.
Computer Science
Technically the implementation is not a stack because:
does not return
In other words,
Architecture
Strictly speaking
The current implementation of
A more modular approach to implementing stack might be:
and placing - from the viewpoint of the stack - all the user interface code in
For example, breaking out the user interface and its logic and its content into modules allows handling nasty issues like multiple languages and the quirks of UniCode to be handled more gracefully. String output is the one of the places Your-Not-Gonna'-Need-It [YNGNI] fails and why it is a baked in module for so many development frameworks.
Variable Names
is more than 50 lines from:
and that distance is only likely to grow as a useful program adds features.
Final Thought
I cannot help but recommend McConnell's Code Complete as a guide to deepening one's understanding of the ways in which software can be organized.
Computer Science
Technically the implementation is not a stack because:
myStack.push(4);
myStack.pop();does not return
4. To be a proper stack, rather than returning void, pop should be:int pop();In other words,
pop() must return something that was pushed onto the stack (or an error, but that's an architectural issue not a principle of computer science).Architecture
Strictly speaking
topElement() could return a void and write a message to the console, but it is conventional for it to return something of the same type as the top element of the stack - in the case of reference types, the question of whether it should be a copy [and what type of copy] or a reference to the object itself muddies the waters.The current implementation of
void pop(); points to a larger architectural issue, a low degree of modularity. Currently, user interface is spread across the code and the stack data structure even writes messages to the console. The effect:- The implementation of stack is not broadly reusable.
- The user interface does not have a clear design and would be hard to debug at a larger scale. There's a bit of the "spaghetti design pattern."
A more modular approach to implementing stack might be:
public:
Mystack();
void push(int x);
int pop();
int topElement();
bool isEmpty();
bool isFull(); // added to allow bounds checking
void print(); // could be handled elsewhere but that's pure opinion.and placing - from the viewpoint of the stack - all the user interface code in
Main. Whether the user interface logic all lives there or in it's own class/module is another question. But reusability and maintenance certainly offer suggestions.For example, breaking out the user interface and its logic and its content into modules allows handling nasty issues like multiple languages and the quirks of UniCode to be handled more gracefully. String output is the one of the places Your-Not-Gonna'-Need-It [YNGNI] fails and why it is a baked in module for so many development frameworks.
Variable Names
A, num, ch, and s1 though perhaps conventional for C++ example code, don't really scale well because they lack information. Even in the original code:cout << "top: " << A[i]<< endl;is more than 50 lines from:
private:
int A[MAX_SIZE];and that distance is only likely to grow as a useful program adds features.
Final Thought
I cannot help but recommend McConnell's Code Complete as a guide to deepening one's understanding of the ways in which software can be organized.
Code Snippets
myStack.push(4);
myStack.pop();public:
Mystack();
void push(int x);
int pop();
int topElement();
bool isEmpty();
bool isFull(); // added to allow bounds checking
void print(); // could be handled elsewhere but that's pure opinion.cout << "top: " << A[i]<< endl;private:
int A[MAX_SIZE];Context
StackExchange Code Review Q#75223, answer score: 9
Revisions (0)
No revisions yet.