V
volk
I hope someone can resolve this. I've spent all morning trying to work
the problem out by I keep getting a seg. fault. I believe it has to do
in the pop function when I later try to pop them out.
If I only called top without ever calling pop, I will get the data
without any problems. If I call pop and then call top, then I get a
seg. fault.
Thanks
http://codepad.org/950oTNEQ
OR
#include <iostream>
class IntNode
{
public:
IntNode() {}
IntNode(int data, IntNode* link) : _data(data),
_link(link) {}
IntNode* getLink() const { return _link; }
int getData () const { return _data; }
void setData(int & data) { _data = data; }
void setLink(IntNode * link) { _link = link; }
void addNode(int data)
{
setLink(new IntNode(data, NULL) );
}
void insert(IntNode* link, int & data) { link-
IntNode* search(IntNode* head, int target)
{
IntNode* temp = head;
if(temp == NULL)
return NULL;
else
{
while(temp->getData() != target &&
temp->getLink() != NULL)
temp = temp->getLink();
if(temp->getData() == target)
return temp;
else
return NULL;
}
}
private:
int _data;
IntNode* _link;
};
class Stack
{
public:
Stack(){ head = new IntNode(0,NULL);}
void push(const int & X)
{
head->addNode(X);
}
int top ()
{
IntNode* t = head->getLink();
return t->getData();
}
void pop()
{
if( head->getLink() != NULL)
{
IntNode* t = head->getLink();
head->setLink(t->getLink());
delete t;
}
}
bool isEmpty(){ if(head->getLink() == NULL) return
true; return
false; }
private:
IntNode* head;
};
int main()
{
Stack* st = new Stack;
st->push(5);
st->push(6);
st->push(7);
st->push(8);
for(int i = 0; i <= 3; i++)
{
std::cout << st->top() << " ";
// st->pop();
}
st->pop();
std::cout << st->top();
/* for(int i = 0; i <= 5; i++)
{
std::cout << st->top() << " ";
st->pop();
}*/
delete st;
return 0;
}
the problem out by I keep getting a seg. fault. I believe it has to do
in the pop function when I later try to pop them out.
If I only called top without ever calling pop, I will get the data
without any problems. If I call pop and then call top, then I get a
seg. fault.
Thanks
http://codepad.org/950oTNEQ
OR
#include <iostream>
class IntNode
{
public:
IntNode() {}
IntNode(int data, IntNode* link) : _data(data),
_link(link) {}
IntNode* getLink() const { return _link; }
int getData () const { return _data; }
void setData(int & data) { _data = data; }
void setLink(IntNode * link) { _link = link; }
void addNode(int data)
{
setLink(new IntNode(data, NULL) );
}
void insert(IntNode* link, int & data) { link-
IntNode(data, link->getLink()) ); }setLink( new
IntNode* search(IntNode* head, int target)
{
IntNode* temp = head;
if(temp == NULL)
return NULL;
else
{
while(temp->getData() != target &&
temp->getLink() != NULL)
temp = temp->getLink();
if(temp->getData() == target)
return temp;
else
return NULL;
}
}
private:
int _data;
IntNode* _link;
};
class Stack
{
public:
Stack(){ head = new IntNode(0,NULL);}
void push(const int & X)
{
head->addNode(X);
}
int top ()
{
IntNode* t = head->getLink();
return t->getData();
}
void pop()
{
if( head->getLink() != NULL)
{
IntNode* t = head->getLink();
head->setLink(t->getLink());
delete t;
}
}
bool isEmpty(){ if(head->getLink() == NULL) return
true; return
false; }
private:
IntNode* head;
};
int main()
{
Stack* st = new Stack;
st->push(5);
st->push(6);
st->push(7);
st->push(8);
for(int i = 0; i <= 3; i++)
{
std::cout << st->top() << " ";
// st->pop();
}
st->pop();
std::cout << st->top();
/* for(int i = 0; i <= 5; i++)
{
std::cout << st->top() << " ";
st->pop();
}*/
delete st;
return 0;
}