In: Computer Science
the purpose of the code is to implement a stack
how can i convert this code in a way that these changes apply to it?
// simplify the logic of the main
// getline(cin, line) needs to be changed to a normal cin >> line;
//make a function for 'list'
// not allowed temporary stack (stack s2)
//merge the catches (errors)
// introduce another function for main
// avoid repetitive code
here is the code:
#include
#include
#include
#include
#include
using namespace std;
class Stack {
public:
bool isEmpty();
int top();
int pop();
void push(int);
private:
vector elements;
};
bool Stack::isEmpty() {
return elements.empty();
}
int Stack::top() {
if(isEmpty()) {
throw runtime_error("error: stack is empty");
}
else {
return elements.back();
}
}
int Stack::pop() {
if(isEmpty()) {
throw runtime_error("error: stack is empty");
}
else {
int item = elements.back();
elements.pop_back();
return item;
}
}
void Stack::push(int item) {
elements.push_back(item);
}
int main() {
string line, cmd;
int val;
Stack stack;
cout << "stack> " << endl;
while(getline(cin, line)) {
if(line.find_first_not_of(" \t") == string:: npos) {
cout << "stack> ";
continue;
}
line = line.substr(line.find_first_not_of(" \t"));
cmd = line.substr(0, line.find(" "));
if(cmd == "push") {
try {
line = line.substr(line.find(" ") + 1);
if(line.size() == 0 || line.at(0) == '-' || line.at(0) == '+' && line.find_first_of("0123456789") > 1 || line.find_first_not_of("0123456789+-") == 0) {
throw runtime_error("error: not a number");
}
else {
val = atoi(line.c_str());
stack.push(val);
}
}
catch(runtime_error& excpt) {
cout << excpt.what() << endl;
}
}
else if(cmd == "pop") {
try {
val = stack.pop();
cout << val << endl;
}
catch(runtime_error& excpt) {
cout << excpt.what() << endl;
}
}
else if(cmd == "top") {
try {
val = stack.top();
cout << val << endl;
}
catch(runtime_error& excpt) {
cout << excpt.what() << endl;
}
}
else if(cmd == "list") {
Stack s2;
bool first = true;
cout << "[";
while(!stack.isEmpty()) {
val = stack.pop();
s2.push(val);
if(!first) {
cout << ",";
}
cout << val;
first = false;
}
cout << "]" << endl;
while(!s2.isEmpty()) {
stack.push(s2.pop());
}
}
else if(cmd == "end") {
break;
}
else {
cin.ignore(numeric_limits::max(), '\n');
cout << "error: invalid command" << endl;
}
cout << "stack> " << endl;
}
}
#include <bits/stdc++.h>
using namespace std;
// Declare linked list node
struct Node {
int data;
struct Node* link;
};
struct Node* top;
// Utility function to add an element data in the stack
// insert at the beginning
void push(int data)
{
// create new node temp and allocate memory
struct Node* temp;
temp = new Node();
// check if stack (heap) is full. Then inserting an
element would
// lead to stack overflow
if (!temp) {
cout << "\nHeap
Overflow";
exit(1);
}
// initialize data into temp data field
temp->data = data;
// put top pointer reference into temp link
temp->link = top;
// make temp as top of Stack
top = temp;
}
// Utility function to check if the stack is empty or not
int isEmpty()
{
return top == NULL;
}
// Utility function to return top element in a stack
int peek()
{
// check for empty stack
if (!isEmpty())
return top->data;
else
exit(1);
}
// Utility function to pop top
// element from the stack
void pop()
{
struct Node* temp;
// check for stack underflow
if (top == NULL) {
cout << "\nStack Underflow"
<< endl;
exit(1);
}
else {
// top assign into temp
temp = top;
// assign second node to
top
top = top->link;
// destroy connection between
first and second
temp->link = NULL;
// release memory of top
node
free(temp);
}
}
// Function to print all the
// elements of the stack
void display()
{
struct Node* temp;
// check for stack underflow
if (top == NULL) {
cout << "\nStack
Underflow";
exit(1);
}
else {
temp = top;
while (temp != NULL) {
// print node
data
cout <<
temp->data << " ";
// assign
temp link to temp
temp =
temp->link;
}
}
}
int main()
{
// push the elements of stack
push(11);
push(22);
push(33);
push(44);
// display stack elements
display();
// print top elementof stack
cout << "\nTop element is\n" <<
peek();
pop();
cout<<"\n";
cout<<"After poping stack elements are\n";
display();
cout << "\nTop element is\n" <<
peek();
return 0;
}