Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linked list stack

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
0
0
GR
Hi all,
I am trying to solve a problem found on a book slides(Problem solving with C++,Walter Savitch) and i got some problems.

I am supposed to do my own the push function for the stack, so i create a new ptr,
void Stack::push(char the_symbol)
{StackFramePtr temp_ptr = new StackFrame;
temp_ptr->data=the_symbol;

and then i try to point the link from the head to the new node
top->link=temp_ptr;
and i get this error
Segmentation fault (core dumped)
Apart that I dont know what this error means, even though i search for it on the net, i dont see why this command top->link=temp_ptr; would cause that!

Top is declared on the header file as
StackFramePtr top;

There is this function
Stack::Stack():top(NULL)
{
//Body intentionally empty
}

would i have to initialize the top pointer here? The problem is that this function is not called by anyone else on the main program that was predone for me.

I guess i could initialise it if the stack is empty and never again.

Thanks in advance
Ilias
 
No worries everybody,
I figured it out. Well actually i didnt, i just typed everything again and it worked! most likely some typo somewhere!
 
May be it was not a misprint? Have you any logic before
Code:
top->link - temp_ptr;
in Stack::push? You have top == 0 when the 1st char pushed into the empty stack, so segmentation fault etc...

Use TGML tag CODE for your snippets, please.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top