In: Computer Science
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor.
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <limits>
using namespace std;
class Stack {
public:
bool isEmpty();
int top();
int pop();
void push(int);
void printList();
private:
vector<int> elements;
};
bool Stack::isEmpty() {
return elements.empty();
}
int Stack::top() {
if(isEmpty()) {
throw runtime_error("error: stack is empty");
}
return elements.back();
}
int Stack::pop() {
if(isEmpty()) {
throw runtime_error("error: stack is empty");
}
int item = elements.back();
elements.pop_back();
return item;
}
void Stack::push(int item) {
elements.push_back(item);
}
void Push(Stack& stack, string line, int val) {
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;
}
}
void PopTop(Stack& stack, int val, string cmd) {
try {
if(cmd == "pop") {
val = stack.pop();
}
else {
val = stack.top();
}
cout << val << endl;
}
catch(runtime_error& excpt) {
cout << excpt.what() << endl;
}
}
void printList(Stack s2, int val) {
bool first = true;
cout << "[";
while(!s2.isEmpty()) {
val = s2.pop();
if(!first) {
cout << ",";
}
cout << val;
first = false;
}
cout << "]" << endl;
}
int main() {
Stack stack;
string line, cmd;
int val;
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") {
Push(stack, line, val);
}
else if(cmd == "pop" || cmd == "top") {
PopTop(stack, val, cmd);
}
else if(cmd == "list") {
printList(stack, val);
}
else if(cmd == "end") {
break;
}
else {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "error: invalid command" << endl;
}
cout << "stack> " << endl;
}
}
i am giving you the explanation of default constructor ,copy construction and destructor so i hope you will understand the behavier of the constructor and destructor.
Example of default constructor and copy constructor :-
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Example of destructor :-
class String
{
private:
char *s;
int size;
public:
String(char *); // constructor
~String(); // destructor
};
String::String(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
String::~String()
{
delete []s;
}