int SinglyList::findNode(double x) {
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x)
{
currNode = currNode->next;
currIndex++;
}
if (currNode)
return currIndex;
return 0;
}
Node* SinglyList::insertNode(int index, double x) {
if (index < 0)
return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL)
return NULL;
Node* newNode = new Node;
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
int SinglyList::deleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x)
{
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void SinglyList::displayList() {
int num = 0;
Node* currNode = head;
while (currNode != NULL) {
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
void SinglyList::setHead(Node* nodeIn) {
head = nodeIn;
}
Node* SinglyList::getHead() {
return head;
}
Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public: // Making all the data members
// data // as public is very exceptional.
double data; // Since this facilitates rapid
// pointer to next node // access of data members
Node* next;
};
#endif
MySinglyList.h
#ifndef MYSINGLYLIST_H
#define MYSINGLYLIST_H
#include "SinglyList.h"
#include "Node.h"
class MySinglyList {
private:
SinglyList sLL;
public:
MySinglyList(); // Initialize nodes and call displayList() to display all nodes
void displayList(); // Call displayList() in SinglyList class to display all attached nodes
bool checkSortedOrder(); // Check linked list order. Return true if in ascending order
void addValue(double); // Add value to all nodes
void insertNodewith888(double); // Insert a new node with value 888 after any node that has the same input value
Node* moveLastNodeToBegin();