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...
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...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H #define   BINGOBALL_H #include <iostream> class BingoBall { public:    BingoBall():letter{'a'}, number{0} {}    BingoBall(char let, int num) :        letter{ let }, number{ num } {}    char getChar() const { return letter; }    int getNumber() const { return number; }    //overload == operator    bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); } private:   ...
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public:...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public: /** * MyVector * * This is the default constructor that sets size equal * to 0 and capacity to 10. * * Parameters: none * * Output: * return: none * reference parameters: none * stream: none */ MyVector(); /** * ~MyVector * * This is the destructor that deletes memory * * Parameters: none * * Output: * return: none * reference...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT