linked list related

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • basavaraj linga
    New Member
    • Dec 2010
    • 3

    linked list related

    Hi aal,
    I am trying to draw different shapes in BREW MP. it’s a mob OS by Qualcomm. The programming is in C.
    The operation is I’ve to draw different shapes on screen based on the selected button. But before drawing the next shape I need to store the values of old shapes which I had drew. So I need to maintain data structure for each shapes. When I press particular button(say circle) after drawing that circle values need to be stored in linked list before drawing next shape.

    I’ve taken a linked list after drawing each shape add it as a node to linked list then before calling new shape traverse this list n draw all available shapes.

    for that i've bulit union inside a structure with type_def

    Code:
    struct shapes{
              int type_in_union;
               union draw{
               struct line lines;
               struct ellipse ellipses;
               struct rect rects;
               struct polygon polygons;
    
                     }draw;
    }shapes1;
    
    
    
    
    struct line{
    int sx,sy;
    int ex,ey;
    }line;
    
    struct ellipse{
    int cx,cy;
    int wx;
    int wy;
    }ellipse;
    
    struct rect{
    int x,y;
    int dx,dy;
    }rect;
    
    struct polygon{
    int len;
    struct point *points;
    }poly;
    
    struct point{
    int x,y;
    }points;


    now i want to dump after each operation(each shape ) that structure into the linked list so that when next time before drawing any new shape i can call all the previous shapes i had drew using linked list .

    for this i've declared a liked list

    Code:
    struct Node{
    struct shapes *shape;
    struct Node *pNext; 
    struct Node *pPrev;
    int type;
    };

    I am Trying to create nodes of structure

    Code:
    insert_node(create_node(&shape_type,&a,&b,&x,&y));
    
    void insert_node(struct Node *pNode)
    {
            
            struct Node *t;
            struct Node *pTemp = NULL;
            if(pStart == NULL)
            {
            pStart = pNode;   /* Store address of the node as the start node */
            return;
            }
            else
            {
                t=pStart;
                while(t->pNext!=NULL)
                {
                    t=t->pNext;
                 }
                     t->pNext=pNode;
                    pNode->pPrev=t;
             }
    }
    
    struct Node* create_node(int *shape_type,int *a,int *b,int *x,int *y)
    {
      struct Node *pNode = NULL;                         // Pointer to the new node                 
      pNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for node                
      pNode->pNext = NULL;                               // No next node yet                        
      pNode->pPrev = NULL;                                 // No previous node
      pNode->type=*shape_type;
      pNode->shape = create_record(&shape_type,&a,&b,&x,&y); // Create record and store address in node 
      return pNode;
    }

    create_record function


    Code:
    struct shapes*  create_record(int *shape_type,int *a,int *b,int *x,int *y)
    {
        struct shapes *shape=NULL;
        
        if(*shape_type==LINE)
        {
        //struct shapes *shape=NULL;
        shape = (struct shapes*)malloc(sizeof(struct shapes)); 
        shape->draw.lines=createline(&shape_type,&a,&b,&x,&y);
        }
            return shape;
        
    }
    createline function

    Code:
    struct line* createline(int *shape_type,int *a,int *b,int *x,int *y)
    {
        struct line *line1=NULL;
        line1 = (struct line*)malloc(sizeof(struct line));
        line1->start_x=*a;
        line1->start_y=*b;
        line1->end_x=*x;
        line1->end_y=*y;
        return line1;
    }
    Is It Right way to store the things into structures????? ??????
    n also i wantt to retrieve the things how it can be done....??????
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I suggest you write a main(). Create a struct variable and create ints f various values. Then add the values to the struct variable. Next use printf to display the values you put in the variable.

    Remember that you use the "." operator for struct variable members when you use the variable name and the "->" operator for the members when you use the address of a struct variable.

    Comment

    • basavaraj linga
      New Member
      • Dec 2010
      • 3

      #3
      HI,
      There is no main in this BREW SDK man... Its not giving any compilation error but giving error during run time..:(

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Then you probably have a bad pointer value.

        I suggest you step through the code using your debugger. When you find the line that fails and you can't fix it, then post again.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Probably not your problem, but you should always check if malloc failed.

          Comment

          • basavaraj linga
            New Member
            • Dec 2010
            • 3

            #6
            Hey now its working fine ..:) Thanks for the Replies:)

            Comment

            Working...