Question: PART 1) Implement a node class and linked list class. The completed header files node.h and LL.h are provided, with comments specifying what each method
PART 1) Implement a node class and linked list class. The completed header files node.h and LL.h are provided, with comments specifying what each method does. Modify the incomplete node.cpp and LL.cpp files. Specifically, implement the following methods for the node class in node.cpp:
constructor
destructor
set_data
set_next
get_data
get_next
To get you started, the constructor, destructor, and set_data methods are already implemented.
Also, implement the following methods for the LL class in LL.cpp:
constructor
destructor
prepend
removeHead
get_head
Make sure to recall the naming convention for constructors and destructors
//node.h:
class node { // node class used in the LL (linked list) class private: node * next; // Pointer to next node of an LL int data; // integer data stored in this node public: node(int x, node * n); // Constructor ~node(); // Destructor void set_data(int x); // Change the data of this node void set_next(node * n);// Change the next pointer of this node int get_data(); // Access the data of this node node * get_next(); // Access the next pointer of this node };
//node.cpp: #include "node.h" #include
// Constructor node::node(int x, node * n) { next = n; data = x; }
// Destructor node::~node() { // Nothing to do here }
// Method for changing the data property of a node void node::set_data(int x) { data = x; }
// Method for changing the next property of a node void node::set_next(node * n) { }
// Method for obtaining the data property of a node int node::get_data() { }
// Method for obtaining the next property of a node node * node::get_next() { }
//LL.h: #include "node.h"
// Linked list class, used in the Stack class class LL { private: node * head; // pointer to first node node * tail; // pointer to last node public: LL(); // Constructor ~LL(); // Destructor void prepend(int value); // add a node to the beginning of the LL int removeHead(); // remove the first node of the LL void print(); // print the elements of the LL node * get_head(); // access the pointer to the first node of the LL };
// test with: main.cpp #include "Stack.h" #include
int main() { LL a; a.prepend(3); a.prepend(4); a.prepend(5); a.print(); a.removeHead(); a.print(); Stack sta; sta.pop(); sta.push(3); sta.push(4); sta.push(10); sta.print(); printf("Popping %d ", sta.pop()); sta.print(); sta.pop(); printf("Stack empty? %d ", sta.isEmpty()); sta.pop(); printf("Stack empty? %d ", sta.isEmpty()); return 0; }
//complete LL.cpp:
// LL.cpp #include "LL.h" #include
// Implement methods given in LL.here
PART 2) Implement a Stack class using the linked list implementation in the first question. This time, you will need to complete both Stack.h and Stack.cpp. Incomplete versions. When designing the Stack class, make sure to hide information appropriately by using private members. The methods in Stack class should include the following: constructor destructor push pop isEmpty print The push and pop methods should add and remove elements to the stack in last-infirst-out order, the isEmpty method should return 1 if the stack is empty and 0 otherwise, and the print method should print elements of the stack in the order that they would be popped.
// Stack.h #include "LL.h"
class Stack { private:
public: // Constructor // Destructor // Other methods };
// Stack.cpp #include "Stack.h" #include
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
