【问题标题】:Stack using linked list error堆栈使用链表错误
【发布时间】:2015-10-17 02:47:54
【问题描述】:

使用链表堆叠

我收到以下错误:

Exception thrown at 0x003D28A9 in ConsoleApplication3.exe: 0xC0000005: Access violation writing location 0x00000000`.
Exception thrown at 0x003D28A9 in ConsoleApplication3.exe: 0xC0000005: Access violation writing location 0x00000000.

使用 Visual Studio 2015 Professional

#pragma once
#ifndef NODE_H
#define NODE_H

template <class KeyType>
class Node //4 marks
{
public:
    // constructor
    Node(KeyType pdata);
    Node();
    //sets the data in the Node
    void setData(KeyType pVal);
    // returns the KeyType data in the Node
    KeyType getData();
    // returns the link to the next node
    Node* getNext();
    // sets the link to the next node
    void setNext(Node* x);

private:
    KeyType data;
    Node *next;

};

#pragma once
#include "Node.h"
#include <iostream>


using namespace std;

template <class KeyType> 
Node <KeyType>::Node(KeyType pdata)
{

    data = pdata;
    next = NULL;
}

template <class KeyType>
Node <KeyType>::Node()
{

    data = 0;
    next = NULL;
}


template <class KeyType>
void Node <KeyType> :: setData (KeyType pval)
{
    data = pval;
}

template <class KeyType>
KeyType Node <KeyType> :: getData()
{
    return data;
}

template <class KeyType>
Node<KeyType>* Node <KeyType> ::getNext()
{
    return next;
}

template <class KeyType>
void Node <KeyType> ::setNext(Node<KeyType>* x)
{
    next = x;
}


#pragma once
#ifndef Stack_H
#define Stack_H
#include "Node.h"
#include "Node.cpp"
template <class KeyType>
class Stack {
public:
    // constructor , creates an empty stack
    Stack(int maxsize);
    // returns true if Stack is full, otherwise return false
    bool IsFull();
    //If number of elements in the Stack is zero return true, otherwise return false
    bool IsEmpty();
    // If Stack is not full, insert item into the Stack
    // Must be an O(1) operation
    void Push(const KeyType item);
    // If Stack is full return 0 or NULL;
    // else return appropriate item from the Stack. Must be an O(1) operation
    KeyType Pop();
    //Print the data
    void print();
private:
    int size;
    int count;
    Node<KeyType> *top;

};

#pragma once
#include <iostream>
#include "Stack.h"
#include "Node.h"
#include "Node.cpp"

using namespace std;

template <class KeyType>
Stack <KeyType>::Stack(int maxsize )
{
    size = maxsize;
    top = NULL;
    count = -1;
}

template <class KeyType>
bool Stack<KeyType> :: IsFull()
{
    if (count == size-1)
        return true;
    else
        return false;
}

template <class KeyType>
bool Stack<KeyType> :: IsEmpty()
{
    if (count == -1)
        return true;
    else
        return false;
}
template <class KeyType>
void  Stack<KeyType> ::Push(const KeyType item)
{
    if (IsFull())
        cout << "Stack is Full" << endl;
    else
    {
        count++;
        Node<KeyType> *nTop ;
        nTop = top;
        if (count == -1)
        {

            nTop->setData(item);
            nTop->setNext(NULL);
            top = nTop;
        }
        else
        {

            nTop->setData(item);
            nTop->setNext(top);
            top = nTop;

        }
    }
}

template <class KeyType>
KeyType Stack<KeyType> ::  Pop()
{
    if (top == NULL)
    {
        cout << "nothing to pop";
        return 0;               
    }
    else
    {
        Node<KeyType> *oldTop;
        old = top;
        KeyType oldData = top->getData();
        top = top->getNext();
        count--;
        delete(old);
        return oldData;          // return tthe value of the node which is deleted
    }

}

template <class KeyType>
void Stack<KeyType> ::print()
{
    Node<KeyType>* temp;
    temp = top;
    while (temp)
    {
        cout << temp->getData() << endl;
        temp = temp->getNext();
    }
}

#include<iostream>
#include "Node.h"
#include "Node.cpp"
#include "Stack.h"
#include "Stack.cpp"
using namespace std;


void main()
{

    Stack<int> s(5);    
    s.Push(1);

//  s.print();

system("pause");
}

【问题讨论】:

  • 欢迎来到 Stack Overflow,嗯,Coder。我敢打赌,如果您尝试将其缩减为 minimal complete example,那么该错误会向您扑来。
  • "访问冲突写入位置 0x00000000" - 您的代码很可能尝试使用 NULL 指针。您可以尝试在调试器中运行它以在错误发生时捕获错误,或者在代码中放置 cout/logging 语句以了解问题发生的位置。

标签: c++ data-structures linked-list stack singly-linked-list


【解决方案1】:

您在构造函数中将top 设置为NULL,然后在Push1 中取消引用它,导致您尝试对其进行setData 时崩溃。

【讨论】:

  • 请您详细说明一下。我应该怎么做才能使这段代码正常工作。
  • @Coder 您需要在写入之前使用new 分配Node&lt;KeyType&gt; 的实例。 Node&lt;KeyType&gt; *nTop = new Node&lt;KeyType&gt;();.
  • @Coder 分配完Push 中的nTop = top; 行了吗?
猜你喜欢
  • 2013-10-05
  • 2013-10-05
  • 2021-01-17
  • 1970-01-01
  • 2011-07-29
  • 2016-09-28
  • 2012-09-30
  • 2011-05-04
相关资源
最近更新 更多