I've been trying to fix this code for 2 days now and i still don't understand where the error is coming from.

this is my stackLL.h file

#include<iostream>
 
using namespace std;
 
class Node
{
 
public:
 int data;
 Node *link;
 
};
class stackLL
{

public:
 stackLL();
 ~stackLL();
 bool isEmpty();
 bool isFull();
 void push(int value);
 void pop();
 int returnTop();
 
 
private:
 
 Node *top;
};
 
 
void stackLL::push(int value)
{
 Node *temp= new Node;
 
 
  
 
  temp->data=value;
  temp->link=top;
  top=temp;
 
  delete temp;
  
}
void stackLL::pop()
{
 Node *temp= new Node;
 if(top!= NULL)
 {
  temp=top;
  top=top->link;
  delete temp;
 }
 else
  cout << "Cannot remove from an empty stack" << endl;
 
 
}
bool stackLL::isEmpty()
{
 return(top==0);
}
bool stackLL::isFull()
{
 return false;
}
int stackLL::returnTop()
{
  return top->data;
}
stackLL::stackLL()
{
 top=NULL;
 
 
}
stackLL::~stackLL()
{
 Node *temp=new Node;
 
 while(top!=NULL)
 {
  temp=top;
  top=top->link;
  delete temp;
 }
}

this is my stackLL.cpp file

#include<iostream>
#include "stackLL.h"
 
using namespace std;
int main()
{
      stackLL stack;
 
      stack.push(3);

      stack.returnTop();
      
      return 0;
}

Which error are u talking about or is it a crash? Its better to post the errors you get from ur code rather than asking us to find it for u.


Still some points which can be clarified about the code are:

> Also it is not advisable to put the entire implementation in the header file since in the end teh compiler ends up pasting the whole code of the header file at the place it is included. It would be better if u have three files like "stack.h" , "stack.cpp" and "test.cpp" where stack.cpp would contain the implementaion present in the header file.

> Since a stack can dynamically increase in size to the max of the size of virtual memory (heap size) what is the point in keeping a function bool isFull ( ) . What is the code u will implement there ?

> Also I think that by keepig the stmt delete temp; in your push (int value); you are actually deleting the newly formed node thereby deleting all the nodes that are created and removing or claiming back the memory allocated to it.

Hope it helped,
bye.

Yes, it crashes. By deleting temp, i'm deallocating the pointer that I created because I no longer need it. Its sole purpose is to push a value into the stack so after this value has been pushed into the stack, it's no longer needed through the code because it's in

top

.

void stackLL::push(int value)
{
 Node *temp= new Node;
 
 
 
 
  temp->data=value;
  temp->link=top;
  top=temp;
 
delete temp;
 
}

Why are you deleting the memory pointed by temp? top is also pointing to the same location. I think that is causing the problem. I commented that part and compiled. It didnt crash.

Wow it sure doesn't crash... All of the code I've been looking at for reference lately has been deleting temp . Ok... so what other problems are there? There's nothing being returned by the returnTop function.

Could it be my constructor or destructor?

Why is your

bool stackLL::isFull() { return false; }

always returning false. While allocating memory you can check whether the memory is allocated. If not then return false.

Wow. I just did something that worked! It was just some code in the .cpp that I was missing out on... Silly rabbit..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.