堆栈使用链表错误

时间:2015-10-17 02:47:54

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

使用关联列表进行堆叠

我收到以下错误:

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专业版

#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");
}

1 个答案:

答案 0 :(得分:1)

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