2015-10-17 3 views
0

стека с использованием 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 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"); 
} 
+0

Добро пожаловать в Stack Overflow, uh, * Coder *. Готов поспорить, если вы попробуете свернуть это до [минимального полного примера] (http://stackoverflow.com/help/mcve), ошибка выскочит на вас. – Beta

+0

«Место записи нарушения доступа 0x00000000» - ваш код, скорее всего, пытается использовать указатель NULL. Вы можете попробовать запустить его в отладчике, чтобы поймать ошибку, как это происходит, или поместить инструкции cout/logging через код, чтобы понять, где происходит эта проблема. – TheUndeadFish

ответ

1

Вы установите top в NULL в конструктор, затем разыщите его в Push1, вызывая сбои при попытке setData на нем.

+0

не могли бы вы уточнить. что я должен сделать, чтобы этот код работал. – Coder

+0

@Coder Вы должны выделить экземпляр 'Node ' используя 'new', прежде чем писать на него. 'Node * nTop = новый узел ();'. – 1201ProgramAlarm

+0

Я пробовал это, но все равно не повезло. Такая же ошибка – Coder

Смежные вопросы