Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   linked list problem (http://www.go4expert.com/forums/linked-list-problem-t22272/)

josh.1981 31May2010 20:35

linked list problem
 
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 ...

Mike911 7Jun2010 11:00

Re: linked list problem
 
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)


All times are GMT +5.5. The time now is 00:00.