0

Stack using Linked List

I am getting the following errors:

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.

Using 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");
}
2
  • Welcome to Stack Overflow, uh, Coder. I'll bet if you try whittling this down to a minimal complete example, the bug will jump out at you. Commented Oct 17, 2015 at 2:56
  • "Access violation writing location 0x00000000" - Your code is most likely trying to use a NULL pointer. You could try running it in a debugger to catch the error as it happens or put cout/logging statements through out the code to get an idea of where the problem is occuring. Commented Oct 17, 2015 at 5:46

1 Answer 1

1

You set top to NULL in the constructor, then dereference it in Push1, causing the crash when you try to setData on it.

Sign up to request clarification or add additional context in comments.

3 Comments

will you please elaborate. what should i do to make this code work.
@Coder You need to allocate an instance of Node<KeyType> using new before you write to it. Node<KeyType> *nTop = new Node<KeyType>();.
@Coder did you take out the nTop = top; line in Push after doing the allocation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.