In: Computer Science
this code is about implementing stacks , I want for someone to rewrite it totally in different way
#include <iostream>
#include <stdexcept>
#include <vector>
#include <cstdlib>
using namespace std;
class Stack {
public:
bool isEmpty();
// returns true if stack
has no elements stored
int top();
// returns element from
top of the stack
// throws
runtime_error("stack is empty")
int pop();
// returns element from
top of the stack and removes it
// throws
runtime_error("stack is empty")
void push(int);
// puts a new element on
top of the stack
private:
vector<int>
elements;
};
//member function definitions
bool Stack::isEmpty()
{
return elements.empty();
}
int Stack::top()
{
if(isEmpty())
throw
runtime_error("stack is empty");
return elements.back();
}
int Stack::pop()
{
if(isEmpty())
throw
runtime_error("stack is empty");
int item = elements.back();
elements.pop_back();
return item;
}
void Stack::push(int item)
{
elements.push_back(item);
}
//member functions definitions end
int main()
{
string line, cmd;
int val;
Stack stack;
cout << "stack> ";
//read line by line
while(getline(cin, line))
{
//get the command
//trim leading
whitespace
if(line.find_first_not_of(" \t") == string::npos)
{
// empty line
cout << "stack> ";
continue;
}
line =
line.substr(line.find_first_not_of(" \t"));
cmd = line.substr(0,
line.find(" "));
if(cmd == "push")
{
// remaining string
line = line.substr(line.find(" ") + 1);
if(line.size() == 0
|| ( (line[0] == '-' || line[0] == '+') &&
line.find_first_of("0123456789") > 1)
|| line.find_first_not_of("0123456789+-") == 0)
{
cout << "error: not a number" << endl;
}
else
{
val = atoi(line.c_str());
stack.push(val);
}
}
else if(cmd ==
"pop")
{
try
{
val = stack.pop();
cout << val << endl;
}
catch(runtime_error& e)
{
cout << "error: " << e.what() << endl;
}
}
else if(cmd ==
"top")
{
try
{
val = stack.top();
cout << val << endl;
}
catch(runtime_error& e)
{
cout << "error: " << e.what() << endl;
}
}
else if(cmd ==
"list")
{
Stack s2;
bool first = true;
cout << "[";
/* pop repeatedly and store in another stack */
while(! stack.isEmpty())
{
val = stack.pop();
s2.push(val);
if(!first)
cout << ",";
cout << val;
first = false;
}
cout << "]" << endl;
/* restore original stack */
while(! s2.isEmpty())
{
stack.push(s2.pop());
}
}
else if(cmd ==
"end")
{
break;
}
else
{
cout << "error: invalid command" << endl;
}
cout <<
"stack> ";
}
cout << endl;
}
In given code, there is class named as "Stack" declared and all operations of stack data structure are
implemented by using vector.
Refer following different code is about implementing stacks:
#include <iostream>
using namespace std;
int stack[5];
int n=5;
int top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top]
<<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"All elements of stack are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty"<<endl;
}
int main() {
int ch, val;
do {
cout<<"1) Push element in stack"<<endl;
cout<<"2) Pop element from stack"<<endl;
cout<<"3) Display stack elements"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to push into stack:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
In this code, there is no any class declaration and all operations of stack data structure is implemented by using Array.
This is menu driven program. I hope, you will understand.