Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help with getting input with linklist in a loop (http://www.go4expert.com/forums/help-getting-input-linklist-loop-t13836/)

thamiz 14Sep2008 11:40

Help with getting input with linklist in a loop
 
Hi there, i am new to using link-list.....

Code:


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>

typedef struct ingredient Ingredient;
enum quantity_type {
  weight,
  volume,
  piece,
};
struct ingredient {
char *name;
int quantity;
enum quantity_type type;
Ingredient *next;
};

the user to has to enter a list of ingredients at a time. The selection may be re-entered to add to the already stored list of ingredients.

the list has to be in a loop until an empty string is entered in to te char name;
any one willing to help me out

hbilling 14Sep2008 17:16

Re: Help with getting input with linklist in a loop
 
Hi,
You could use a system which allocates memory for a new node in the Linked List only if it passes your condition. ie. If the name entered is an empty string, then just dont allocate memory for a new node.

......
char name_str[20]; // Its just hardcoded here for convenience

for(;;) {
printf("\n Enter the name\n");
scanf("%s", name_str);
if(strcmp(name_str, '')) {
break; <<<<<< Here u break off from the loop if name is a null String
}

......
// Similarly get the other elements also from the user

//if all is fine , u actually allocate memory for the node using malloc and add it to the list using ur API's.

}

HTH. If not i can explain in detail :-)

hbilling 14Sep2008 17:17

Re: Help with getting input with linklist in a loop
 
sorry abt the smiley above. its actually an unconditional for loop. :-)

xpi0t0s 14Sep2008 23:00

Re: Help with getting input with linklist in a loop
 
Where are you stuck? The code you posted doesn't have a main function...
Are you hoping we'll write the code for you?

hbilling 14Sep2008 23:42

Re: Help with getting input with linklist in a loop
 
Hi xpi0t0s,

First of all I assume ur comment was meant for thamiz. If so, I think he/she just wanted an idea, which i guess i gave in my solution:-) .
If its understood by him/her, its great! Having said that, i think having a full code with a main function is always easier to answer on.

Regards,
Harneet

xpi0t0s 15Sep2008 18:25

Re: Help with getting input with linklist in a loop
 
yep, the comment was aimed at thamiz.

thamiz 19Sep2008 15:16

Re: Help with getting input with linklist in a loop
 
hi
yeah sorry about not including the main function.
but i want to know, can we have a stsuct within a struct and how would u implement it
eg if we use the above code
i can add and print from the following structure
Code:

struct ingredient {
char *name;
int quantity;
enum quantity_type type;
Ingredient *left;
Ingredient *right;
};

Now i want to make a stucture called Recipe
Srtuct recipe {
char* recipename;
//i need to include the struct for ingredients here;
recipe *left;
recipe *right;
};
any body know how i can do this
thank in advance
cheers:

thamiz 19Sep2008 15:18

Re: Help with getting input with linklist in a loop
 
soory guys
i forgot use the correct standard
hi
yeah sorry about not including the main function.
but i want to know, can we have a stsuct within a struct and how would u implement it
eg if we use the above code
i can add and print from the following structure

Code:

struct ingredient {
char *name;
int quantity;
enum quantity_type type;
Ingredient *left;
Ingredient *right;
};
Now i want to make a stucture called Recipe
Code:

Srtuct recipe {
char* recipename;
//i need to include the struct for ingredients here;
recipe *left;
recipe *right;
};

any body know how i can do this
thank in advance
cheers:

thamiz 19Sep2008 16:04

Re: Help with getting input with linklist in a loop
 
is there any one willing to have a look at my code, and see where i am going wrong..
but i have to send it to u personally because it is kind of a homework thing.

xpi0t0s 20Sep2008 00:15

Re: Help with getting input with linklist in a loop
 
Embedded/nested structures are fine:
Code:

struct recipe {
char* recipename;
struct {
recipe *left;
recipe *right;
} links;
} R;

Then refer to R.recipename, R.links.left, R.links.right.

Sorry, post the code here, that's how the forum works, and we all get to look at it, which is useful if, say, I don't understand something it's doing but someone else does. If it's your homework and it's OK to post it on the internet to get others to fix it then that shouldn't be a problem. If it's not OK to post it on the internet, well you shouldn't be barking up this tree.

You could simplify the code, creating a small testcase that isolates the problematic stuff from the rest, I don't see any teacher objecting to that as you'd obviously not be plagiarising (which is generally considered a major sin in academic circles).


All times are GMT +5.5. The time now is 03:08.