|  | Challenging Logic |  | |
| | | Nash |  |
| Posted: Thu May 29, 2008 7:38 am Post subject: Challenging Logic |  |
Hi, I am new to c language and i have a problem to solve. Imagine a parent child relationship let me take menu as an example
File Edit New Cut Window Copy Message Open
here File and Edit are Top Level Parents(root menu), New and Open are children of File. Window and MEssage are children of NEw. Similarly Cut and Copy are children of Edit.
I can have only 2 functions like Next and GetSubMenu. Initially i will display File when i say next it should show me edit when i say next it should show me file.
When i am in File when is say get submenu it should display new and now next should give me open and next should take me to File since Open is the last child of File. Similary when i am in New and say GetSubMenu it should give me Window and Next should give me Message and Next should give me New.
I dont know whether i need to use a bidirectional circular linked list or array. i need some help from you.
Thanks. |
| |
| | | Chris Thomasson |  |
| Posted: Thu May 29, 2008 7:38 am Post subject: Re: Challenging Logic |  |
| |  | |
"Nash" <jeevs007@gmail.com> wrote in message news:63af9f3d-ca8b-4ce6-8d0e-63df576c89bc@i18g2000prn.googlegroups.com...
| Quote: | Hi, I am new to c language and i have a problem to solve. Imagine a parent child relationship let me take menu as an example
File Edit New Cut Window Copy Message Open
|
You can make a simplistic model of this with an array, linked-list and two data-structures...
#define ITEMS_PER_MENU 16 #define ITEM_NAME_SIZE 16 #define MENU_NAME_SIZE ITEM_NAME_SIZE
struct item { char name[ITEM_NAME_SIZE]; struct menu* child; };
struct menu { char name[MENU_NAME_SIZE]; size_t items; struct item items[ITEMS_PER_MENU]; };
static struct menu my_file_new_submenu = { { "File::New" }, 2, { { "Window" }, NULL }, { { "Message" }, NULL } };
static struct menu my_file_menu = { { "File" }, 2, { { "New" }, &my_file_new_submenu }, { { "Open" }, NULL } };
static struct menu my_edit_menu = { { "Edit" }, 2, { { "Cut" }, NULL }, { { "Copy" }, NULL } };
[...] |
| |
| | | Malcolm McLean |  |
| Posted: Thu May 29, 2008 5:56 pm Post subject: Re: Challenging Logic |  |
| |  | |
"Chris Thomasson" <cristom@comcast.net> wrote in message
| Quote: | "Nash" <jeevs007@gmail.com> wrote in message news:63af9f3d-ca8b-4ce6-8d0e-63df576c89bc@i18g2000prn.googlegroups.com... Hi, I am new to c language and i have a problem to solve. Imagine a parent child relationship let me take menu as an example
File Edit New Cut Window Copy Message Open
You can make a simplistic model of this with an array, linked-list and two data-structures...
#define ITEMS_PER_MENU 16 #define ITEM_NAME_SIZE 16 #define MENU_NAME_SIZE ITEM_NAME_SIZE
struct item { char name[ITEM_NAME_SIZE]; struct menu* child; };
struct menu { char name[MENU_NAME_SIZE]; size_t items; struct item items[ITEMS_PER_MENU]; };
static struct menu my_file_new_submenu = { { "File::New" }, 2, { { "Window" }, NULL }, { { "Message" }, NULL } };
static struct menu my_file_menu = { { "File" }, 2, { { "New" }, &my_file_new_submenu }, { { "Open" }, NULL } };
static struct menu my_edit_menu = { { "Edit" }, 2, { { "Cut" }, NULL }, { { "Copy" }, NULL } };
[...] A better way to solve the problem is to think linked lists. |
struct node { struct node *next; struct node *child; char label[64]; void *ptr; /* for later use */ };
Now we can have an arbitrary menu system, with the first File entry at the root. Allocate all the nodes using malloc().
-- Free games and programming goodies. LINK |
| |
|
|