Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in C++. Thanks! 1 // Specification file for the DynIntStack class 2 #ifnde

ID: 3796337 • Letter: W

Question

Write in C++. Thanks!

1 // Specification file for the DynIntStack class
2 #ifndef DYNINTSTACK_H
3 #define DYNINTSTACK_H
4
5 class DynIntStack
6 {
7 private:
8 // Structure for stack nodes
9 struct StackNode
10 {
11 int value; // Value in the node
12 StackNode *next; // Pointer to the next node
13 };
14
15 StackNode *top; // Pointer to the stack top
16
17 public:
18 // Constructor
19 DynIntStack()
20 { top = nullptr; }
21
22 // Destructor
23 ~DynIntStack();
24
25 // Stack operations
26 void push(int);
27 void pop(int &);
28 bool isEmpty();
29 };
30 #endif

*************************************************

#ifndef DYNINTQUEUE_H
2 #define DYNINTQUEUE_H
3
4 class DynIntQueue
5 {
6 private:
7 // Structure for the queue nodes
8 struct QueueNode
9 {
10 int value; // Value in a node
11 QueueNode *next; // Pointer to the next node
12 };
13
14 QueueNode *front; // The front of the queue
15 QueueNode *rear; // The rear of the queue
16 int numItems; // Number of items in the queue
17 public:
18 // Constructor
19 DynIntQueue();
20
21 // Destructor
22 ~DynIntQueue();

23

24 // Queue operations
25 void enqueue(int);
26 void dequeue(int &);
27 bool isEmpty() const;
28 bool isFull() const;
29 void clear();
30 };
31 #endif

The DynlntStack and DynlntQueue classes shown in this chapter are abstract data types using a dynamic stack and dynamic queue, respectively. The classes do not cur­rently test for memory allocation errors. Modify the classes so they determine whether new nodes cannot be created by handling the bad_alloc exception.

Explanation / Answer

//Made changes to the given input

1 // Specification file for the DynIntStack class
2 #ifndef DYNINTSTACK_H
3 #define DYNINTSTACK_H
4
5 class DynIntStack
6 {
7 private:
8 // Structure for stack nodes
9 struct StackNode
10 {
11 int value; // Value in the node
12 StackNode *next; // Pointer to the next node
13 };
14
15 StackNode *top; // Pointer to the stack top
16
17 public:
18 // Constructor
19 DynIntStack()
20 {
21   try{ //adding a try catch statement to handle the bad allocation
22   top = nullptr; }
23   catch (std::bad_alloc& ba)
24   {
25 std::cerr << "bad_alloc caught: " << ba.what() << ' ';
26   }}
27
28 // Destructor
29 ~DynIntStack();
30
31 // Stack operations
32 void push(int);
33 void pop(int &);
34 bool isEmpty();
35 };
36 #endif
*************************************************
#ifndef DYNINTQUEUE_H
2 #define DYNINTQUEUE_H
3
4 class DynIntQueue
5 {
6 private:
7 // Structure for the queue nodes
8 struct QueueNode
9 {
10 int value; // Value in a node
11 QueueNode *next; // Pointer to the next node
12 };
13
14 QueueNode *front; // The front of the queue
15 QueueNode *rear; // The rear of the queue
16 int numItems; // Number of items in the queue
17 public:
18 // Constructor
19 DynIntQueue()
20   {
21   try{ //adding a try catch statement to handle the bad allocation
22   front = new QueueNode;
23   rear = new QueueNode;}
24   catch (std::bad_alloc& ba)
25   {
26 std::cerr << "bad_alloc caught: " << ba.what() << ' ';
27   }}
28
29 // Destructor
30 ~DynIntQueue();
31
32 // Queue operations
33 void enqueue(int);
34 void dequeue(int &);
35 bool isEmpty() const;
36 bool isFull() const;
37 void clear();
38 };
39 #endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote