qq_37174526
Liu Zhian
2017-09-12 09:35

c++链表操作时出现的指针错误

  • c++
  • 指针
 struct Node
{
    int data;
    Node* next;
};
typedef Node* List_Node;
class List
{
public:
    List();     

    ~List();
    void creat_List_last();   // 创建新链表
    void print_List(); // 遍历输出链表
private:
    List_Node head;
};
List::List()
{
    head = NULL;
}
void List::creat_List_last()
{
    List_Node p, q;

    p = new Node;
    q = p;
    cout << "请输入数据(以-1表示结束):"<<endl;
    cin >> p->data;
    while (p->data != -1)
    {
        if (head == NULL)
            head = p;
        else
            q->next = p;

        q = p;
        p = new Node;
        cout << "请输入数据(以-1表示结束):" << endl;
        cin >> p->data;
    }
    q->next = NULL;
    delete p;
}
void List::print_List()
{
    cout << "所有数据如下:" << endl;
    List_Node p=head;
    while (p)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
}

List::~List()
{
    List_Node p=head;
    while (p)
    {
        head = p->next;
        delete p;
    }
}
int main()
{
    List l;
    l.creat_List_last();
    l.print_List();
}

提示:c++ - _Block_Type_Is_Valid (pHead->nBlockUse) Error

对指针操作不是很熟悉,望博友指教

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答