By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

linked list related

P: 3
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

Expand|Select|Wrap|Line Numbers
  1. struct shapes{
  2.           int type_in_union;
  3.            union draw{
  4.            struct line lines;
  5.            struct ellipse ellipses;
  6.            struct rect rects;
  7.            struct polygon polygons;
  8.  
  9.                  }draw;
  10. }shapes1;
  11.  
  12.  
  13.  
  14.  
  15. struct line{
  16. int sx,sy;
  17. int ex,ey;
  18. }line;
  19.  
  20. struct ellipse{
  21. int cx,cy;
  22. int wx;
  23. int wy;
  24. }ellipse;
  25.  
  26. struct rect{
  27. int x,y;
  28. int dx,dy;
  29. }rect;
  30.  
  31. struct polygon{
  32. int len;
  33. struct point *points;
  34. }poly;
  35.  
  36. struct point{
  37. int x,y;
  38. }points;
  39.  
  40.  


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

Expand|Select|Wrap|Line Numbers
  1. struct Node{
  2. struct shapes *shape;
  3. struct Node *pNext; 
  4. struct Node *pPrev;
  5. int type;
  6. };
  7.  

I am Trying to create nodes of structure

Expand|Select|Wrap|Line Numbers
  1. insert_node(create_node(&shape_type,&a,&b,&x,&y));
  2.  
  3. void insert_node(struct Node *pNode)
  4. {
  5.  
  6.         struct Node *t;
  7.         struct Node *pTemp = NULL;
  8.         if(pStart == NULL)
  9.         {
  10.         pStart = pNode;   /* Store address of the node as the start node */
  11.         return;
  12.         }
  13.         else
  14.         {
  15.             t=pStart;
  16.             while(t->pNext!=NULL)
  17.             {
  18.                 t=t->pNext;
  19.              }
  20.                  t->pNext=pNode;
  21.                 pNode->pPrev=t;
  22.          }
  23. }
  24.  
  25. struct Node* create_node(int *shape_type,int *a,int *b,int *x,int *y)
  26. {
  27.   struct Node *pNode = NULL;                         // Pointer to the new node                 
  28.   pNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for node                
  29.   pNode->pNext = NULL;                               // No next node yet                        
  30.   pNode->pPrev = NULL;                                 // No previous node
  31.   pNode->type=*shape_type;
  32.   pNode->shape = create_record(&shape_type,&a,&b,&x,&y); // Create record and store address in node 
  33.   return pNode;
  34. }
  35.  

create_record function


Expand|Select|Wrap|Line Numbers
  1.  
  2. struct shapes*  create_record(int *shape_type,int *a,int *b,int *x,int *y)
  3. {
  4.     struct shapes *shape=NULL;
  5.  
  6.     if(*shape_type==LINE)
  7.     {
  8.     //struct shapes *shape=NULL;
  9.     shape = (struct shapes*)malloc(sizeof(struct shapes)); 
  10.     shape->draw.lines=createline(&shape_type,&a,&b,&x,&y);
  11.     }
  12.         return shape;
  13.  
  14. }
  15.  
  16.  
createline function

Expand|Select|Wrap|Line Numbers
  1. struct line* createline(int *shape_type,int *a,int *b,int *x,int *y)
  2. {
  3.     struct line *line1=NULL;
  4.     line1 = (struct line*)malloc(sizeof(struct line));
  5.     line1->start_x=*a;
  6.     line1->start_y=*b;
  7.     line1->end_x=*x;
  8.     line1->end_y=*y;
  9.     return line1;
  10. }
  11.  
  12.  
Is It Right way to store the things into structures???????????
n also i wantt to retrieve the things how it can be done....??????
Dec 17 '10 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Dec 17 '10 #2

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

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Dec 21 '10 #4

Expert 100+
P: 2,398
Probably not your problem, but you should always check if malloc failed.
Dec 21 '10 #5

P: 3
Hey now its working fine ..:) Thanks for the Replies:)
Dec 22 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.