_CtrlsValidHeapPointer(Block)

  c++, data-structures, linked-list, visual-studio

I’m new to c++ and I use c++ to learn data structure recently. I stuck when the error occurs, I’ve tried to debug but It’s still confused me. So, this error is I try to implement two operations concatenate and merge, my terminal shows the result that seems to successful on logical. However, the error is coming in the next second. Just like img:
enter image description here

Here is my code. Hopefully someone can help me solve this issue, thanks!

#pragma once
#include <iostream>
using namespace std;
class LinkedList;
class Node
{
private:
    int data;
    Node* next;
public:
    friend class LinkedList;
};

class LinkedList
{
private:
    Node* first;
    Node* last;
    Node* third;
public:
    LinkedList()
    {
        first = nullptr;
        last = nullptr;
        third = nullptr;
    }
    LinkedList(int A[], int n);
    ~LinkedList();
    void Display();
    void RDisplay();//Recursive
    void RDisplay(Node* p);
    Node* getFirstNode();
    int Count();
    int RCount(Node* p);//Recursive
    int Sum();
    int RSum(Node* p);//Recursive
    int Max();
    int RMax(Node* p);//Recursive
    int LSearch(int key); //linear search
    int RSearch(int key);
    int RSearch(Node* p, int key);
    void Insert(int position, int x);
    void InsertLast(int x);
    void SortedInsert(int x);
    int Delete(int position);
    int isSorted();
    void RemoveDuplicate();
    void ReverseE();//Elements
    void ReverseL();//Links
    void RReverse();//Recursive
    void RReverse(Node* q,Node *p);
    void Concatenate(Node* second);
    void Merge(Node *second);
    int Mid();
    int isLoop();
};
void LinkedList::Merge(Node* second)
{
    Node* last;
    if (first->data < second->data)
    {
        third =  first;
        last = first;
        first = first->next;
        last->next = nullptr;
    }
    else
    {
        third = last = second;
        second = second->next;
        last->next = NULL;
    }
    while (first != NULL && second != NULL)
    {
        if (first->data < second->data)
        {
            last->next = first;
            last = first;
            first = first->next;
            last->next = NULL;
        }
        else
        {
            last->next = second;
            last = second;
            second = second->next;
            last->next = NULL;
        }
    }
    if (first != NULL)
    {
        last->next = first;
        first = third;
    }
    else
    {
        last->next = second;
        first = third;
    }
}

//concatenate
void LinkedList::Concatenate(Node* second)
{
    Node* p = first;
    while (p->next != NULL)
    {
        p = p->next;
    }
    p->next = second;
}
#include <iostream>
#include "Linkedlist.h"
#include "Circular.h"
#include "Doubly.h"
#include "CircularDoubly.h"

int main()
{
    int A[] = { 1,2,3,4,6,7 };
    int B[] = { 5,6,7,8 };
    LinkedList c1(A,6);
    LinkedList c2(B, 4);
    c1.Concatenate(c2.getFirstNode());
    c1.Display();
}

Should I offer more detail?

Source: Windows Questions C++

LEAVE A COMMENT