C++ LinkedList,向量构造函数的内存溢出?
此代码使用linkedList添加两个和整数。 我在struct ListNode中添加了一个新的构造函数,以便将两个向量存储a和B作为输入C++ LinkedList,向量构造函数的内存溢出?,c++,C++,此代码使用linkedList添加两个和整数。 我在struct ListNode中添加了一个新的构造函数,以便将两个向量存储a和B作为输入 ListNode(vector<int> array) { vector<int>::iterator itr = array.begin(); ListNode *t = nullptr; for (; itr < array.end(); itr++) {
ListNode(vector<int> array)
{
vector<int>::iterator itr = array.begin();
ListNode *t = nullptr;
for (; itr < array.end(); itr++) {
t->val = *itr;
t = t->next;
}
}
在访问
t->val
之前,您的ListNode
构造函数没有为t
创建指向的新ListNode
。它甚至没有尝试初始化此的成员
请尝试类似以下内容:
struct ListNode{
int-val=0;
ListNode*next=nullptr;
ListNode(intx=0,ListNode*next=nullptr):val(x),next(next){
ListNode(常量向量和数组)
{
如果(!array.empty()){
auto-itr=array.begin();
val=*itr++;
ListNode**t=&next;
while(itr!=array.end()){
*t=新列表节点(*itr++);
t=&(*t)->next;
}
}
}
};
话虽如此,在ListNode
中拥有这样一个构造函数真的毫无意义。列表节点应该只关心自己的数据,而不关心周围节点的数据。这种列表构造实际上属于一个单独的list
类,它包装了一个ListNode
s链,例如:
#包括
#包括
#包括
/*
单链表的定义。
*/
结构列表节点{
int-val=0;
ListNode*next=nullptr;
ListNode(intx=0,ListNode*next=nullptr):val(x),next(next){
};
班级名单{
私人:
ListNode*head=nullptr;
公众:
List()=默认值;
列表(常量向量和向量)
{
ListNode**t=&head;
for(int val:vec){
*t=新列表节点(val);
t=&(*t)->next;
}
}
列表地址编号(常数列表和l)常数{
ListNode*p=头部;
ListNode*q=l.head;
列出dummyList;
列表节点**当前=&(dummyList.head);
整数进位=0;
while(p | | q){
int x=(p)?p->val:0;
int y=(q)?q->val:0;
整数和=进位+x+y;
进位=总和/10;
*当前=新的ListNode(总和%10);
当前=&(*当前)->下一步;
如果(p){
p=p->next;
}
if(q){
q=q->next;
}
}
如果(进位>0){
*当前=新列表节点(进位);
}
返回dummyList;
}
//以下是遵守“3/5/0规则”所需的内容:
// https://en.cppreference.com/w/cpp/language/rule_of_three
列表(常量列表和列表)
{
ListNode**t=&head;
对于(ListNode*n=list.head;n;n=n->next){
*t=新列表节点(n->val);
t=&(*t)->next;
}
}
列表(列表和列表)
{
std::swap(head,list.head);
}
~List()
{
ListNode*n;
对于(ListNode*t=head;t;t=n){
n=t->next;
删除t;
}
}
列表和运算符=(列表rhs){
列表温度(标准:移动(rhs));
标准:交换(压头、温度压头);
归还*这个;
}
};
类解决方案{
公众:
列表添加两个数字(常数列表和l1、常数列表和l2){
返回l1.addNumber(l2);
}
};
int main(int argc,const char*argv[]{
溶液溶胶;
向量A={2,4,3};
向量B={5,6,4};
清单1(A);
清单2(B);
列表结果=sol.addTwoNumber(列表1、列表2);
返回0;
}
在访问t->val
之前,您的ListNode
构造函数没有为t
创建指向的新ListNode
。它甚至没有尝试初始化此的成员
请尝试类似以下内容:
struct ListNode{
int-val=0;
ListNode*next=nullptr;
ListNode(intx=0,ListNode*next=nullptr):val(x),next(next){
ListNode(常量向量和数组)
{
如果(!array.empty()){
auto-itr=array.begin();
val=*itr++;
ListNode**t=&next;
while(itr!=array.end()){
*t=新列表节点(*itr++);
t=&(*t)->next;
}
}
}
};
话虽如此,在ListNode
中拥有这样一个构造函数真的毫无意义。列表节点应该只关心自己的数据,而不关心周围节点的数据。这种列表构造实际上属于一个单独的list
类,它包装了一个ListNode
s链,例如:
#包括
#包括
#包括
/*
单链表的定义。
*/
结构列表节点{
int-val=0;
ListNode*next=nullptr;
ListNode(intx=0,ListNode*next=nullptr):val(x),next(next){
};
班级名单{
私人:
ListNode*head=nullptr;
公众:
List()=默认值;
列表(常量向量和向量)
{
ListNode**t=&head;
for(int val:vec){
*t=新列表节点(val);
t=&(*t)->next;
}
}
列表地址编号(常数列表和l)常数{
ListNode*p=头部;
ListNode*q=l.head;
列出dummyList;
列表节点**当前=&(dummyList.head);
整数进位=0;
while(p | | q){
int x=(p)?p->val:0;
int y=(q)?q->val:0;
整数和=进位+x+y;
进位=总和/10;
*当前=新的ListNode(总和%10);
当前=&(*当前)->下一步;
如果(p){
p=p->next;
}
if(q){
q=q->next;
}
}
#include <iostream>
#include <vector>
using namespace std;
/*
Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
ListNode(vector<int> array)
{
vector<int>::iterator itr = array.begin();
ListNode *t = nullptr;
for (; itr < array.end(); itr++) {
t->val = *itr;
t = t->next;
}
}
};
class Solution {
public:
Solution(){};
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p = l1;
ListNode *q = l2;
ListNode *dummyHead = new ListNode(0);
ListNode *current = dummyHead;
int carry = 0;
while (p != NULL || q != NULL) {
int x = (p != NULL) ? p->val : 0;
int y = (q != NULL) ? q->val : 0;
int sum = carry + x + y;
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current -> next;
if (p != NULL) {
p = p -> next;
}
if (q != NULL) {
q = q -> next;
}
}
if (carry > 0) {
current -> next = new ListNode(carry);
}
return dummyHead->next;
}
};
int main(int argc, const char * argv[]) {
Solution *sol = nullptr;
vector<int> A = {2,4,3};
vector<int> B = {5,6,4};
ListNode *list1= new ListNode(A);
ListNode *list2= new ListNode(B);
sol->addTwoNumbers(list1,list2);
return 0;
}
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
class VectorList {
public:
ListNode *root;
VectorList() : root(nullptr){}
VectorList(vector<int> &array){
root = new ListNode();
ListNode *curr;
curr = root;
int _size = array.size();
for(int i = _size - 1; i >= 0; i--){
ListNode *t = new ListNode(array[i]);
curr->next = t;
curr = curr->next;
}
root = root->next;
}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* curr, *root = NULL;
int carry = 0;
while(l1 || l2){
if(l1 != NULL && l2 != NULL){
ListNode* t = new ListNode;
t->val = (l1->val + l2->val + carry) % 10;
t->next = NULL;
carry = (l1->val + l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
l2 = l2->next;
}
else if(l1 == NULL){
ListNode* t = new ListNode;
t->val = (l2->val + carry) % 10;
t->next = NULL;
carry = (l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l2 = l2->next;
}
else if(l2 == NULL){
ListNode* t = new ListNode;
t->val = (l1->val + carry) % 10;
t->next = NULL;
carry = (l1->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
}
}
if(carry){
ListNode* t = new ListNode;
t->val = carry;
t->next = NULL;
curr->next = t;
}
return root;
}
void print(ListNode *root){
ListNode *curr = root;
while(curr){
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
};
#include <iostream>
#include <vector>
using namespace std;
/*
Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
};
class VectorList {
public:
ListNode *root;
VectorList() : root(nullptr){}
VectorList(vector<int> &array){
root = new ListNode();
ListNode *curr;
curr = root;
int _size = array.size();
for(int i = _size - 1; i >= 0; i--){
ListNode *t = new ListNode(array[i]);
curr->next = t;
curr = curr->next;
}
root = root->next;
}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* curr, *root = NULL;
int carry = 0;
while(l1 || l2){
if(l1 != NULL && l2 != NULL){
ListNode* t = new ListNode;
t->val = (l1->val + l2->val + carry) % 10;
t->next = NULL;
carry = (l1->val + l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
l2 = l2->next;
}
else if(l1 == NULL){
ListNode* t = new ListNode;
t->val = (l2->val + carry) % 10;
t->next = NULL;
carry = (l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l2 = l2->next;
}
else if(l2 == NULL){
ListNode* t = new ListNode;
t->val = (l1->val + carry) % 10;
t->next = NULL;
carry = (l1->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
}
}
if(carry){
ListNode* t = new ListNode;
t->val = carry;
t->next = NULL;
curr->next = t;
}
return root;
}
void print(ListNode *root){
ListNode *curr = root;
while(curr){
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
};
int main(int argc, const char * argv[]) {
Solution *sol = nullptr;
vector<int> A = {2,4,3};
vector<int> B = {5,6,4};
VectorList list1 = VectorList(A);
VectorList list2 = VectorList(B);
ListNode *root = sol->addTwoNumbers(list1.root, list2.root);
sol->print(root);
return 0;
}
l1 = [2,4,3], l2 = [5,6,4]
[7 0 8]