doubly linked list

i want to create doubly linked list with head pointer only. i.e. i want to add nodes at start of doubly linked list... this is what i m trying to do.. please tell me whats wrong with this code?
void add(char x)
{
node *temp;
temp=new node;
temp->c=x;
temp->next=top;
top->precede=temp;
temp->precede=NULL;
top=temp;
}
top will be NULL the first time you call this function so top->precede = temp; will crash.
hello Troubled646,

first of all set top to null node *top = 0; the check top before you use it
1
2
if(top)
  top->precede=temp;

now it should not crash anymore
i had initiallized top with NULL... but it didn't work.. can you please give me a template???
Here is a template. Why reinvent the wheel?
http://cplusplus.com/reference/stl/list/

If you really want to reinvent the wheel you'll probably want to post a complete example so that we understand what you are doing. Showing one function won't help us understand your problem(s).
like i said:
coder777 wrote:
1
2
if(top)
  top->precede=temp;

without that if it will always crash the first time
Topic archived. No new replies allowed.