Код: Выделить всё
#include
#include"List_Node.h"
using namespace std;
int main() {
Num_List *list, *list2;
cout displayList();
cout displayList();
list->addTwoNumbers((ListNode*)list->val, (ListNode*)list2->val);
return 0;
}
Код: Выделить всё
#pragma once
#ifndef LIST_NODE_H
#define LIST_NODE_H
struct ListNode {
int val;
struct ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
ListNode* head;
class Num_List: public ListNode {
private:
public:
//constructor
Num_List() {
head = nullptr;
}
//Destructor
~Num_List();
void appendNode(int);
void insertNode(int);
void deleteNode(int);
void displayList()const;
int addTwoNumbers(ListNode*, ListNode*);
};
#endif
Код: Выделить всё
#include"List_Node.h"
#include
Num_List::~Num_List()
{
ListNode* nodePtr;// pointer to traverse list
ListNode* nextNode;//pointer to next node
//Position nodePtr at the head of the list
nodePtr = head;
//while nodeptr is no 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 a the next node for next loop
nodePtr = nextNode;
}
}
void Num_List::appendNode(int num)
{
ListNode* newNode;
ListNode* nodePtr;
//Allocate a new node and store data there;
newNode = new ListNode;
newNode->val = num;
newNode->next = nullptr;
//If there are no nodes in list
//then make a newNode the head node.
if (!head) {
head = newNode;
}
else {
//Initealize nodePtr to head of the list.
nodePtr = head;
//Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
//Insert a newNode as the last node ie already points to nullPtr
nodePtr->next = newNode;
}
}
void Num_List::insertNode(int num)
{
ListNode* newNode; // a new node
ListNode* nodePtr;// a pointer to nodes used to traverse list
ListNode* previousNode = nullptr;// the previous node
//Allocate a new node and sotre the num there.
newNode = new ListNode;
newNode->val = num;
//if there are no nodes in the list make newNode the head node;
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else {//otherwise insert a newNode
//Position nodePtr at the ahead of the list
nodePtr = head;
//Initialize previousNode to nullptr
previousNode = nullptr;
//skip all nodes whose value is less than input value
while (nodePtr != nullptr && nodePtr->val < 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;
}
}
}void Num_List::deleteNode(int num)
{
ListNode* nodePtr;// a pointer to nodes used to traverse list
ListNode* previousNode = nullptr;// the previous node
//determine if the first node contains the data
if (!head)return;
if (head->val == num) {
nodePtr = head->next;
delete head;
head = nodePtr;
}
else {
//Initialize nodePtr to the head of list
nodePtr = head;
//skip all nodes whose value is not the input value
while (nodePtr != nullptr && nodePtr->val != 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;
}
}
}
using namespace std;
void Num_List::displayList() const
{
ListNode* nodePtr;//pointer to a node int ListNode class
//position nodePtr at the head of the list.
nodePtr = head;
//while nodePtr points to a node, traverse the list
while (nodePtr) {
//display value contained in node
cout val next;
}
}
int Num_List::addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(0);
ListNode* tail = head;
int carry = 0;
while (l1 != nullptr || l2 != nullptr || carry != 0) {
int dig1 = (l1 != nullptr) ? l1->val : 0;
int dig2 = (l1 != nullptr) ? l1->val : 0;
int sum = dig1 + dig2 + carry;
int digit = sum % 10;
carry = sum / 10;
ListNode* newNode = new ListNode(digit);
tail->next = newNode;
tail = tail->next;
l1 = (l1 != nullptr) ? l1->next : nullptr;
l2 = (l2 != nullptr) ? l2->next : nullptr;
}
ListNode* result = head->next;
delete head;
return (int) result;
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... add2number