所以我的头衔可能有点令人困惑,但这是我的问题。假设我有一个数字,1083419。我需要做两件事,首先 - 我想将这些值添加到链表中,这样每个节点只有三位数,所以数字1083419将是:
节点= 001,节点= 083,节点= 419,
我不确定如何制作它,以便在它达到三位数时创建一个新节点,我也不确定如何填充左边节点0' s如果它不完全填充则使其成为三位数节点。
到目前为止,我已附上我的代码。从长远来看,我最终希望能够将两个列表一起添加到第三个列表中,并打印出结果。
提前感谢任何人的帮助。
Main.ccp文件
#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;
}
}