Question

In: Computer Science

using C++. edit this code down below so that it will implement stack with linked list...

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;
}   
}

Solutions

Expert Solution

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;

}


Related Solutions

How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
Could you check/edit my code? Design and implement your own linked list class to hold a...
Could you check/edit my code? Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to...
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT