So my title may be a little confusing, here is my question though. Say I have a number, 1083419 for arguments sake. I need to do two things with it, First - I would like to add these values into a linked list so that only three digits are in each node, so the number 1083419 would be:
Node = 001, Node = 083, Node = 419,
I am unsure how to make it so a new node is created when it reaches three digits, and I am also unsure how to pad the left node with 0's to make it three digits if it does not totally fill the node.
I have attached my code so far. In the long run, I eventually want to be able to add the two lists together into a third list, and print out the results of that.
Thanks in advance for anyones help.
Main.ccp file
#include <iostream>
#include "List.h"
using namespace std;
int main()
{
List listA; // Create object
List listB;
// New list to store the new total
List listC;
listA.addNode(90);
listA.addNode(8);
listA.addNode(100);
listB.addNode(10);
listB.addNode(8);
int sum = listA.SumOfNodes (listA) + listB.SumOfNodes (listB);
cout << sum << endl;
listC.addNode(sum);
cout << "The new list C is ";
listC.printList();
cout << endl;
return 0;
}
List.h
#include <cstdlib>
#include <iostream>
using namespace std;
class List // Create Linked List class
{
private:
// List structure
typedef struct node
{
int data;
node* next; // Create node pointer
}*nodePtr;
//typedef struct node* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
List(); // Constructor
void addNode(int addData); // Adds node
void deleteNode(int delData); // Deletes Node
int SumOfNodes(const List list1); // Sum of Nodes
void printList(); // Prints list
}; // End class header
List.cpp
#include <iostream>
#include "List.h"
using namespace std;
List::List() // Constructor
{
head = NULL; // Set all variables initially to NULL
curr = NULL;
temp = NULL;
}
//-----------------------------------------------------
// ADD NODE FUNCTION
void List::addNode(int addData)
{
nodePtr n = new node;
n->next = NULL; // Node n is pointing to, access its next element and make it point to NULL
n->data = addData;
if (head != NULL) {
curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = n;
}
else {
head = n; // If list is not empty, n is the front
}
}
//----------------------------------------------------
// DELETE NODE FUNCTION
void List::deleteNode(int delData) // Pass in data and traverse list
{ // until delData matches the node value
nodePtr delPtr = NULL; // Then delete
temp = head;
curr = head;
while (curr != NULL && curr->data != delData) {
temp = curr; // Code to traverse the list
curr = curr->next;
}
// If data is not in list, cout statement
if (curr == NULL) {
cout << delData << " was not in the list" << endl;
delete delPtr;
}
else {
delPtr = curr;
curr = curr->next;
temp->next = curr; // Patches list after deletion
if (delPtr == head) {
head = head->next;
temp = NULL;
}
delete delPtr; // Deletes
cout << "The value " << delData << " has been deleted" << endl;
}
}
//----------------------------------------------------
// SUM OF NODES FUNCTION
// This function sums all the nodes together
int List::SumOfNodes(const List list1)
{
//find the length of the list
node *temp = head;
int count = 0;
while (temp != NULL)
{
count++;
temp = temp->next;
}
//re-assign temp to head
temp = head;
//calculate the sum
unsigned int sum = 0;
for (unsigned int i = 1; i < pow(10, count); i = i * 10)
{
sum = sum + temp->data * i;
temp = temp->next;
}
return sum;
}
//----------------------------------------------------
// PRINT LIST FUNCTION
void List::printList()
{
curr = head;
while (curr != NULL) {
cout << curr->data << endl;
curr = curr->next;
}
}
Aucun commentaire:
Enregistrer un commentaire