Question

In: Computer Science

I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef>...

I create a h file, and I wrote

#ifndef MYSTACK_H
#define MYSTACK_H

#include <cstdlib>
#include <cstddef>
#include <iostream>

struct node
{
int value;
node* next;

node(int value, node* next = nullptr)
{
this->value = value;
this->next = next;
}
};

class mystack
{
private:
node* stack_top;
size_t stack_size;

public:
mystack();
mystack(const mystack& x);
~mystack();
mystack& operator=(const mystack& x);
size_t size() const;
bool empty() const;
void clear();
const int& top() const;
void push(int value);
void pop();
void clone(const mystack& x);
};

#endif

how do I write a default constructer for my node structure?

C++

Solutions

Expert Solution

Code

#ifndef MYSTACK_H
#define MYSTACK_H

#include <cstdlib>
#include <cstddef>
#include <iostream>

struct node
{
   int value;
   node* next;
   node()//this is your default constructor
   {
       this->value = 0;
       this->next = nullptr;
   }

   node(int value, node* next = nullptr)
   {
       this->value = value;
       this->next = next;
   }
};

class mystack
{
private:
   node * stack_top;
   size_t stack_size;

public:
   mystack();
   mystack(const mystack& x);
   ~mystack();
   mystack& operator=(const mystack& x);
   size_t size() const;
   bool empty() const;
   void clear();
   const int& top() const;
   void push(int value);
   void pop();
   void clone(const mystack& x);
};

#endif

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include...
//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include        // Provide list using namespace std; class Table { public:    // MEMBER CONSTANT    static const unsigned int TABLE_SIZE = 13;    Table() { total_records = 0; }    virtual ~Table() { }    unsigned int get_total() { return total_records; }    virtual void insert(string key) =0;    virtual void print() =0;    protected:    unsigned int total_records;    // HELPER...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start of the marco guard // Try not to change this file #include <iostream> #include <cmath> using std::cout; using std::endl; using std::istream; using std::ostream; class Point { private:    double x, y, z; public:    // Constructors    Point();    Point(double inX, double inY, double inZ = 0);    Point(const Point& inPt);    // Get Functions    double getX() const;    double getY() const;   ...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int main() {    const char* filename = "samples.coe"; const int N = 1024; FILE* file = fopen(filename, "w"); if (file == NULL) { perror("fopen"); } fprintf(file, "; These are 1024 sample values in range -1 to 1,\n"); fprintf(file, "; Sine Wave 0\n"); fprintf(file, "memory_initialization_radix = 10;\n"); fprintf(file, "memory_initialization_vector\n"); double values[N]; double delta = M_PI / (N - 1); for (int i = 0; i...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct ListNode *list_prepend(struct ListNode *list, long value); int list_length(struct ListNode *list); struct ListNode *list_remove(struct ListNode *list, long value); #endif /* LIST_H_ */ given.c file #include #include "list.h" struct ListNode *list_prepend(struct ListNode *list, long value) { struct ListNode *node = malloc(sizeof(struct ListNode)); node->value = value; node->next = list; return node; } list.c file #include #include "list.h" /* Counts and returns the number of nodes in the...
Convert this C++ to js/html and run in browser. ///////////////////////////////////////////////// #include <cstdlib> #include <ctime> #include <sstream>...
Convert this C++ to js/html and run in browser. ///////////////////////////////////////////////// #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For...
C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" //Converting hexadecimal to binary int main() {    char binarynum[65], hexa[17];    //Using long int because it has greater capacity    long int...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT