1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#include <iostream> // For cout
using namespace std;
class NumberList
{
private:
// Declare a structure for the list
struct ListNode
{
double value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
NumberList()
{ head = nullptr; }
void mergeArray(double[],int);
// Destructor
~NumberList();
// Linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayList() const;
//Added for assignment
};
//**************************************************
// appendNode appends a node containing the *
// value pased into num, to the end of the list. *
//**************************************************
void NumberList::appendNode(double num)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
}
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
void NumberList::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//**************************************************
// The insertNode function inserts a node with *
// num copied to its value member. *
//**************************************************
void NumberList::insertNode(double num)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = nullptr; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to nullptr.
previousNode = nullptr;
// Skip all nodes whose value is less than num.
while (nodePtr != nullptr && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//**************************************************
// The deleteNode function searches for a node *
// with num as its value. The node, if found, is *
// deleted from the list and from memory. *
//**************************************************
void NumberList::deleteNode(double num)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != nullptr && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
NumberList::~NumberList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr is not at the end of the list...
while (nodePtr != nullptr)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
int main () //Here is the main...It will demonstrate the assignment
{
NumberList num; //Object of Nmberlist...(num)
//Now we must make a list of numbers
int sizeofarray = 7;
num.insertNode(1.3);
num.insertNode(8);
num.insertNode(4.5);
num.insertNode(6);
num.insertNode(8.8);
cout<<"The current list of values are: "<<endl;
num.displayList();
double myArray[7] ={1.0,2.3,5.8,9.7,8.5,4.2,19.0}; //myArray of doubles
cout<<"These are in the double array"<<endl;
//passing
for(int i =0; i <7; i++)
{
if(i<6)
cout<<myArray[i]<<" -- "<<endl;
else
cout <<myArray[i];
}
cout << "\n"<<endl;
num.mergeArray(myArray,7); //HERE is the error
cout<<"The passing results in : "<<endl;
num.displayList();
return 0;
}
|