Please answer this question in C++: Node.cpp #include "Node.h" struct Node; Node.h #ifndef LINKEDLIST_NODE_H #define LIN
Posted: Fri May 20, 2022 1:19 pm
Please answer this question in C++:
Node.cpp
#include "Node.h"
struct Node;
Node.h
#ifndef LINKEDLIST_NODE_H
#define LINKEDLIST_NODE_H
struct Node {
public:
int data;
Node* next;
};
#endif
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
int numNodes(Node* head);
Node* searchNode(Node* head, int element);
bool deleteNode(Node** head, Node* toDelete);
#endif
Helper.cpp
#include <iostream>
#include "Helper.h"
#include "Node.h"
using namespace std;
// only for the 1st Node
void initNode(struct Node* head, int n) {
head->data = n;
head->next = NULL;
}
// appending
void addNode(struct Node* head, int n) {
Node* newNode = new Node;
newNode->data = n;
newNode->next = NULL;
Node* cur = head;
while (cur) {
if (cur->next == NULL) {
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void insertFront(struct Node** head, int n) {
Node* newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
/* Creating a copy of a linked list */
void copyLinkedList(struct Node* node, struct Node** pNew) {
if (node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}
/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node* node1, struct Node* node2)
{
static int flag;
/* both lists are NULL */
if (node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if (node1 == NULL || node2 == NULL)
flag = 0;
else if (node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}
return flag;
}
void deleteLinkedList(struct Node** node) {
struct Node* tmpNode;
while (*node) {
tmpNode = *node;
*node = tmpNode->next;
delete tmpNode;
}
}
void display(struct Node* head) {
Node* list = head;
while (list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}
Helper.h
#ifndef LINKEDLIST_HELPER_H
#define LINKEDLIST_HELPER_H
void initNode(struct Node* head, int n);
void addNode(struct Node* head, int n);
void insertFront(struct Node** head, int n);
void copyLinkedList(struct Node* node, struct Node** pNew);
int compareLinkedList(struct Node* node1, struct Node*
node2);
void deleteLinkedList(struct Node** node);
void display(struct Node* head);
#endif
Main.cpp
#include <iostream>
#include "Node.h"
#include "LinkedList.h"
#include "Helper.h"
using namespace std;
int main() {
struct Node* newHead;
struct Node* head = new Node;
initNode(head, 10);
display(head);
addNode(head, 20);
display(head);
addNode(head, 30);
display(head);
addNode(head, 35);
display(head);
addNode(head, 40);
display(head);
insertFront(&head, 5);
display(head);
int numDel = 5;
Node* ptrDelete = searchNode(head, numDel);
if (deleteNode(&head, ptrDelete))
cout << "Node " << numDel << " deleted!\n";
display(head);
cout << "The list is copied\n";
copyLinkedList(head, &newHead);
display(newHead);
cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if (compareLinkedList(head, newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
numDel = 35;
ptrDelete = searchNode(newHead, numDel);
if (deleteNode(&newHead, ptrDelete)) {
cout << "Node " << numDel << " deleted!\n";
cout << "The new list after the delete is\n";
display(newHead);
}
cout << "Comparing the two lists again...\n";
cout << "Are the two lists same?\n";
if (compareLinkedList(head, newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
cout << "Deleting the copied list\n";
deleteLinkedList(&newHead);
display(newHead);
return 0;
}
LinkedList.cpp
// TO-DO: Add the deleteNode function to LinkedList.cpp.
The function prototype is shown in LinkedList.h. Make this function
accept a double pointer to the list (**head) and a node to delete.
Find the provided node and remove it from the list.
Node.cpp
#include "Node.h"
struct Node;
Node.h
#ifndef LINKEDLIST_NODE_H
#define LINKEDLIST_NODE_H
struct Node {
public:
int data;
Node* next;
};
#endif
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
int numNodes(Node* head);
Node* searchNode(Node* head, int element);
bool deleteNode(Node** head, Node* toDelete);
#endif
Helper.cpp
#include <iostream>
#include "Helper.h"
#include "Node.h"
using namespace std;
// only for the 1st Node
void initNode(struct Node* head, int n) {
head->data = n;
head->next = NULL;
}
// appending
void addNode(struct Node* head, int n) {
Node* newNode = new Node;
newNode->data = n;
newNode->next = NULL;
Node* cur = head;
while (cur) {
if (cur->next == NULL) {
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void insertFront(struct Node** head, int n) {
Node* newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
/* Creating a copy of a linked list */
void copyLinkedList(struct Node* node, struct Node** pNew) {
if (node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}
/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node* node1, struct Node* node2)
{
static int flag;
/* both lists are NULL */
if (node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if (node1 == NULL || node2 == NULL)
flag = 0;
else if (node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}
return flag;
}
void deleteLinkedList(struct Node** node) {
struct Node* tmpNode;
while (*node) {
tmpNode = *node;
*node = tmpNode->next;
delete tmpNode;
}
}
void display(struct Node* head) {
Node* list = head;
while (list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}
Helper.h
#ifndef LINKEDLIST_HELPER_H
#define LINKEDLIST_HELPER_H
void initNode(struct Node* head, int n);
void addNode(struct Node* head, int n);
void insertFront(struct Node** head, int n);
void copyLinkedList(struct Node* node, struct Node** pNew);
int compareLinkedList(struct Node* node1, struct Node*
node2);
void deleteLinkedList(struct Node** node);
void display(struct Node* head);
#endif
Main.cpp
#include <iostream>
#include "Node.h"
#include "LinkedList.h"
#include "Helper.h"
using namespace std;
int main() {
struct Node* newHead;
struct Node* head = new Node;
initNode(head, 10);
display(head);
addNode(head, 20);
display(head);
addNode(head, 30);
display(head);
addNode(head, 35);
display(head);
addNode(head, 40);
display(head);
insertFront(&head, 5);
display(head);
int numDel = 5;
Node* ptrDelete = searchNode(head, numDel);
if (deleteNode(&head, ptrDelete))
cout << "Node " << numDel << " deleted!\n";
display(head);
cout << "The list is copied\n";
copyLinkedList(head, &newHead);
display(newHead);
cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if (compareLinkedList(head, newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
numDel = 35;
ptrDelete = searchNode(newHead, numDel);
if (deleteNode(&newHead, ptrDelete)) {
cout << "Node " << numDel << " deleted!\n";
cout << "The new list after the delete is\n";
display(newHead);
}
cout << "Comparing the two lists again...\n";
cout << "Are the two lists same?\n";
if (compareLinkedList(head, newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
cout << "Deleting the copied list\n";
deleteLinkedList(&newHead);
display(newHead);
return 0;
}
LinkedList.cpp
// TO-DO: Add the deleteNode function to LinkedList.cpp.
The function prototype is shown in LinkedList.h. Make this function
accept a double pointer to the list (**head) and a node to delete.
Find the provided node and remove it from the list.