Two doubly linked list to capture two numbers ( input and String and convert it to numbers)

  add, c++, doubly-linked-list, numbers

Please help with what I am mistaken with below code.

I am getting a run time error as
terminate called after throwing an instance of ‘std::logic_error’
what(): basic_string::_S_construct null not valid
…Program finished with exit code 0

Further I need to add these 2 numbers and store the result in linked list. ANy pointers on that will be helpful too.

Code is as follows

    
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;
struct Node {
  
/*   Node( const string  &data, Node* prev = nullptr, Node* next = nullptr )
        : data(data), prev(prev), next(next) {}
   Node( const string &&data, Node* prev = nullptr, Node* next = nullptr )
        : data(data), prev(prev), next(next) {}
*/
  
  int data;
  struct Node* prev;
  struct Node* next;
};
//struct Node* head = NULL;

void insert(int newdata, Node** head) {
  
  //Node* newnode = new Node(newdata, nullptr, head );
  
  Node* newnode = new Node[(sizeof(Node))];
  newnode->data = newdata;
   newnode->next = (*head);
   (*head) = newnode;
  
  cout << "nNode inserted";
}

void display(Node* node) 
{

  while (node != NULL) 
  {
        cout << node->data << " ";
        node = node->next;
    }
    cout << endl;
  
}
int main() {
  string n1=NULL,n2=NULL,sum=NULL,temp=NULL;
  Node *head1 = NULL, *head2 = NULL, *head3 = NULL;
  int temp1;
  cout << "Enter 1st numbern";
  cin >> n1;
  
  cout << "Enter 2nd numbern";
  cin >> n1;
  
  int len,len2,i;
  len = n1.size();
  len2 = n2.size();
  cout << "n Length of first number is " << len;
  cout << "n Length of second number is " << len2;
  
  getch();
  // temp= n1;
  int num;
  // cout<<"n line is "<<temp.substr(len);

  for (i = 0; i < len; i = i + 4) 
    {
        temp1=stoi(n1.substr(i, 4));
        insert(temp1,&head1);
    }

  for (i = 0; i < len2; i = i + 4) 
    {
        temp1=stoi(n2.substr(i, 4));
        insert(temp1,&head2);

    }

  cout << "nThe first doubly linked list is: ";
  display(head1);
  cout << "nThe second doubly linked list is: ";
  display(head2);
  return 0;
}


Source: Windows Questions C++

LEAVE A COMMENT