linked list problem

Discussion in 'C++' started by josh.1981, May 31, 2010.

  1. josh.1981

    josh.1981 New Member

    Joined:
    May 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: May 31, 2010
  2. Mike911

    Mike911 New Member

    Joined:
    Jun 6, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice