i had this code in my last program and it worked fine and it works on my friends mac but it wont work on my g++ compiler or devc it gives a seg fault at the t=t->next; how can i fix this?
sorthon, now I figured out what was wrong with your code. You do not initialize the head (at least you didn't in the code you gave me last time). if(head == NULL) - last time you didn't initialize head to NULL in your constructor, and I bet $10 you still don't.
And again sorthon, I have told you like 5 times already- segmentation faults are not caused by the line they appear in. Segmentation faults are caused by accessing illegal memory regions, which usually happens due to incorrect handling of pointers.
Also, I told you last time that your push code creates memory leaks, it appears you still didn't fix that.
Aside from that, you don't really create stack functionality there.
#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
usingnamespace std;
// Specification file for a linked list abstract data type class.
// This linked list is actually a stack, since all additions and
// removals are at the head.
template<class type>
struct node // Each node has two fields:
{
type data; // a data field,
node<type> *next; // and a pointer field.
};
template<class type>
class stack
{
private:
node<type> *head; // Pointer to the first cell.
public:
stack();
void push(type item);
bool pop();
void search();
void view();
};
template<class type>
stack<type>::stack()
{
}
template<class type>
void stack<type> :: push (type item)
{
node<type> *t=new node<type>;
node<type> *trail = new node<type>;
node<type> *tmp = new node<type>;
trail = NULL;
t==NULL;
if(head == NULL)
{
t->data = item;
t->next = head;
head = t;
}
elseif(head != NULL)
{
t->next = head;
while(t != NULL)
{
trail = t;
t->next = t->next;
}
tmp->data = item;
tmp->next = t;
trail->next = tmp;
}
}
// Function to remove the first element from the stack and
// return it to the caller.
template<class type>
bool stack<type> :: pop ()
{
node<type>* cur;
cur = head;
if(cur == NULL)
{
returnfalse;
}
else
{
head = cur -> next;
delete cur;
returntrue;
}
}
// Accessor functions.
// Function to see whether an element is on the list.
template<class type>
void stack<type> :: search ()
{
type tmp;
type tmp2;
type tmp3;
type tmp4;
node<type>* cur=head;
if(cur!=NULL)do
{
if(cur-> data == '(')
tmp = cur->data;
if(cur -> data == ')')
tmp2= cur->data;
if(cur-> data == '{')
tmp3 = cur->data;
if(cur -> data == '}')
tmp4 = cur->data;
cur=cur -> next;
}
while(cur != NULL);
if (tmp == tmp2)
cout << "The operation has the correct amount of parenthesis." << endl;
else
cout << "You do not have the appropriate number of parenthesis." << endl;
if (tmp3 == tmp4)
cout << "The operation has the correct amount of braces." << endl;
else
cout << "You do not have the appropriate number of braces." << endl;
if(tmp==0 && tmp2==0 && tmp3==0 && tmp4==0)
cout<<"There is proper grouping"<<endl;
}
template<class type>
void stack<type> :: view ()
{
node<type>* tmp = head;
while(tmp != NULL){
cout << tmp -> data;
tmp = tmp -> next;
}
}
int main()
{
stack<int> b;
//b.push('(');
b.push(8);
//b.push('*');
b.push(9);
b.view();
cout << endl;
//b.search();
}
off to the class this is due in haha just gana turn it in late been really sick and not in the mood to think i just dot get why it worked before on a mac but not on my computer.
i get and understand that there are leeks, but that doesn't explain why im looking at it working on a mac but the exact same thing will not work on my pc. even more confusing is that they use the same compiler.
template<class type>
stack<type>::stack()
{
head = NULL;
}
Done. The value of head is undefined if you don't do this- and with undefined things, even the same compilers may behave differently with different implementations. It may be set to NULL automatically, or it's just left as it is (which might be any random value that was in the memory previously at that point).
wow... my teacher is an idiot, i asked him this several times and he assured me that its defaulted to NULL alright well that did in fact fix it, thank you very much for sticking in there with me and helping sorry if i at any point come over as rude or snappy.