Jump to content

Linked list

- - - - -

  • Please log in to reply
12 replies to this topic

#1
basavaraj_l

basavaraj_l

    Newbie

  • Members
  • Pip
  • 6 posts
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
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
struct Node{
struct shapes *shape;
struct Node *pNext; 
struct Node *pPrev;
int type;
};

I am Trying to create nodes of structure
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
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
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....??????

Edited by WingedPanther, 15 December 2010 - 01:53 PM.
add code tags (the # button)


#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,


I see a few problems!

Your Insert Function:

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;

                [B][COLOR="Red"]t->pNext=pNode; //here you may overwrite existing nodes[/COLOR][/B]

                pNode->pPrev=t;

                

            }

        }

}




The correct version should be like:


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;

 [COLOR="Red"]          //get the last node in the link list

  [/COLOR]          while(t->pNext!=NULL)

            {

                t=t->pNext;

                

            }

 [COLOR="Red"]           //now insert at the last

 [/COLOR]            t->pNext=pNode;

            pNode->pPrev=t;


        }

}


Rest seems to be fine!

I hope this helps!

Munir

#3
basavaraj_l

basavaraj_l

    Newbie

  • Members
  • Pip
  • 6 posts
yeah Thanks munir u r rite ... but accessing variable like which i've done is correct because i've used so many structures i am only getting confusion now...

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

You can use a flag[char Shape_type] which would be included in each node:
This would help you determine what kind of shape a node is contains on runtime.

Munir

#5
basavaraj_l

basavaraj_l

    Newbie

  • Members
  • Pip
  • 6 posts
But i've used Node type na that nuch is enough rite???

struct Node{
struct shapes *shape;
struct Node *pNext;
struct Node *pPrev;
int type; // Here is d node type
};

and also if i want to retrieve the elements can i do like thiS? is it works???


static void getlinedetails(widgets *pMe)
{
struct Node *pNode = NULL;
int x,y,a,b;
x=pNode->shape->draw.lines->start_x;
y=pNode->shape->draw.lines->start_y;
a=pNode->shape->draw.lines->end_x;
b=pNode->shape->draw.lines->end_y;
widgets_DrawLine(pMe,&x,&y,&a,&b);
}

#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I hope this should work: give it a try!

Munir

#7
basavaraj_l

basavaraj_l

    Newbie

  • Members
  • Pip
  • 6 posts
NO Compilation Error Munir but its giving run time error:(

#8
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

What are those run-time errors?

Munir

#9
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

What are those run-time errors?

Munir

#10
basavaraj_l

basavaraj_l

    Newbie

  • Members
  • Pip
  • 6 posts
when m tyring to access the variables using

static void getlinedetails(widgets *pMe)
{
struct Node *pNode = NULL;
int x,y,a,b;
x=pNode->shape->draw.lines->start_x // here its throwning exceptions by saying access violation reading memory

#11
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

That's 100% clear that you're doing a mistake :)

Your code:


static void getlinedetails(widgets *pMe)

{

struct Node *[COLOR="red"]pNode = NULL;[/COLOR] //you're setting it NULL

int x,y,a,b;

x=[COLOR="red"]pNode->shape->draw.lines->start_x[/COLOR] // You're using it pNode without being initialized and allocating memory.


You either allocate memory to pointer or you have to point it to a valid memory address by referencing to an existing variable.

I hope this helps!

Munir

#12
basavaraj_l

basavaraj_l

    Newbie

  • Members
  • Pip
  • 6 posts
Hey Its working properly nw man... There was problem with parameters...

Thank u:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users