2015-10-17 36 views
0

堆棧堆棧使用鏈表使用鏈表錯誤

我收到以下錯誤:

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

歡迎來到Stack Overflow,呃,* Coder *。我敢打賭,如果你試圖將這個問題細化爲一個[最小完整示例](http://stackoverflow.com/help/mcve),那麼這個bug就會跳出來。 – Beta

+0

「訪問衝突寫入位置0x00000000」 - 您的代碼很可能嘗試使用NULL指針。您可以嘗試在調試器中運行它,以便在發生錯誤時捕獲錯誤,或者將cout/logging語句放在代碼中以瞭解問題發生的位置。請您詳細說明一下 – TheUndeadFish

回答

1

您在設置topNULL構造函數,然後在Push1中對其進行解引用,導致崩潰時嘗試setData就可以了。

+0

。我應該做些什麼來使這個代碼工作。 – Coder

+0

@Coder在寫入之前,您需要使用'new'分配'Node '的實例。 '節點 * nTop =新節點();'。 – 1201ProgramAlarm

+0

我已經嘗試過,但仍然沒有運氣。相同的錯誤 – Coder