linked list problem

josh.1981's Avatar
Newbie Member
hi everybody .. let me first thank you for this great website ..
I am java programmer but I have to make linked list using C or C++ . well I wrote the following simple example:
Code:
#include<iostream.h>
struct  rec{
    int val;
    rec *next;
    };
 
void build(rec  *p){
    rec *q=new rec;
    for(int i=1;i<6;i++){
         if(p==NULL){
           q->val=i;
           q->next=NULL;
            p=q;
        }else{
        q->val=i;
        q->next=p;
         p=q;
      }
    }
}
 
void print(rec *p){
     rec  *q=new rec;
     q=p;
    do{
         cout<<q->val<<" ";
        q=q->next;
     }while(q!=NULL);
}
 
 
int main()
{
   rec *e=NULL;
    build(e);
   print(e);
   return 0;
}
I compiled the source core using BC 5.02 under MS windows vista and g++ under ubuntu 10.04 and in both cases I got the same error ("violent access at the address")("Segmentation fault"). Thank you in advance for any help ...

Last edited by shabbir; 31May2010 at 22:05.. Reason: Code Block
0
Mike911's Avatar, Join Date: Jun 2010
Go4Expert Member
Yes, one knot points into two directions: forward and backward.
Take care of START and END description of the knots.
adding or deleting a knot results in many operations, take care of them like a baby !!
prevknot -> new (and back)
new <- nextknot (and forth)