How to use array method rewrite the insert data of part , Thx lot !

#include "SinglyList.h"
#include "Node.h"
#include <string>
#include <iostream>

using namespace std;

SinglyList::SinglyList() {
head = NULL;
}

SinglyList::~SinglyList() {
Node* currNode = head;
Node* nextNode = NULL;
while (currNode != NULL) {
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}

bool SinglyList::isEmpty() {
return (head==NULL);
}

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;
}
What is your current problem?
Where is your function main()?
Hello ! Thx for reply .

My teacher said that my code so heavy , just told me to use array method to rewrite the insert data of part ........below is Node.h. And MySinglyList.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




#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();

};

#endif
In other words, this is what your teacher gave you, and you have to rewrite the code according to the assignment?
Have you considered writing a function main() yourself? I still don't see it.
Thx for reply !


#include "Node.h"
#include "SinglyList.h"
#include "MySinglyList.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
cout << "=========================================" << endl;
cout << " Create a new list with intialized nodes " << endl;
cout << "=========================================" << endl;
MySinglyList list;

cout << "=========================================" << endl;
cout << " Add Value(10) " << endl;
cout << "=========================================" << endl;

list.addValue(10);
list.displayList();

cout << "=========================================" << endl;
cout << " Check Sorted Order " << endl;
cout << "=========================================" << endl;
cout << "The list is in " << (list.checkSortedOrder() ? "sorted order." : "unsorted order.") << "\n"<< endl;

cout << "=========================================" << endl;
cout << " Move Last Node To Begin() " << endl;
cout << "=========================================" << endl;
list.moveLastNodeToBegin();
list.displayList();

cout << "=========================================" << endl;
cout << " Insert Node with 888(14.6) " << endl;
cout << "=========================================" << endl;
list.insertNodewith888(14.6);
list.displayList();
system("pause");
return 0;
}
Topic archived. No new replies allowed.