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\n", sta.pop());
sta.print();
sta.pop();
printf("Stack empty? %d\n", sta.isEmpty());
sta.pop();
printf("Stack empty? %d\n", 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 <stdio.h>
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
//node.cpp
#include "node.h"
#include <stdio.h>
// 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) {
next=n;
}
// Method for obtaining the data property of a node
int node::get_data() {
return data;
}
// Method for obtaining the next property of a node
node * node::get_next() {
return next;
}
//LL.cpp
#include "LL.h"
#include <stdio.h>
//constructor
LL::LL(){
//initializing head and tail to NULL
head=NULL;
tail=NULL;
}
LL::~LL(){
//deleting all nodes to de allocate memory
while(head!=NULL){
node *temp=head->get_next();
delete head;
head=temp;
}
}
void LL::prepend(int value){
//adding a new node before head
node *n=new node(value, head);
//updating head and tail as necessary
if(head==NULL){
head=n;
tail=n;
}else{
head=n;
}
}
int LL::removeHead(){
if(head!=NULL){
//getting data of node to be removed
int data=head->get_data();
//updating head
head=head->get_next();
//updating tail if necessary
if(head==NULL){
tail=NULL;
}
//returning data of removed node
return data;
}
return -1; //indicating empty list
}
void LL::print(){
node *temp=head;
//looping and printing all data
while(temp!=NULL){
printf("%d",temp->get_data());
temp=temp->get_next();
//appending a => if there is a next node
if(temp!=NULL){
printf(" => ");
}else{
//end of the loop, printing a line break
printf("\n");
}
}
}
node * LL::get_head(){
//returning the head
return head;
}
//Stack.h file
#include "LL.h"
class Stack {
private:
//pointer to the LL
LL *top;
public:
// Constructor
Stack();
// Destructor
~Stack();
// method to push an item to the top
void push(int);
//remove and return an item from the top
int pop();
//returns 1 if stack is empty, else 0
int isEmpty();
//prints the stack
void print();
};
//Stack.cpp file
#include "Stack.h"
#include <stdio.h>
//implementation of all methods
Stack::Stack(){
//initializing linked list
top=new LL();
}
Stack::~Stack(){
//de allocating the memory
delete top;
}
void Stack::push(int value){
//appending before top node of the LL
top->prepend(value);
}
int Stack::pop(){
//removing and returning the head/top value
return top->removeHead();
}
int Stack::isEmpty(){
//returns true if LL head is NULL
return top->get_head()==NULL;
}
void Stack::print(){
//calling print() of LL class
top->print();
}
/*OUTPUT using given test program */
5 => 4 => 3
4 => 3
10 => 4 => 3
Popping 10
4 => 3
Stack empty? 0
Stack empty? 1
PART 1) Implement a node class and linked list class. The completed header files node.h and...