![]() |
| | #1 |
| Registered User Join Date: Oct 2002
Posts: 10
| problem with structures and linked list Can anyone figure out what I am doing wrong? Please help! Code: #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Result {
float value; /* value of result */
struct Result *pt_res_next;
};
struct Exp {
char ExpName[10];
struct Result *pt_res;
struct Exp *pt_exp_next;
};
/* Declare the list node to tie all the Experiment structs together into
a list.
*/
struct ExpList
{
struct Exp* pExp;
struct ExpList* value;
};
struct Exp* GetExp()
/*
Assumes: ???????
Results: Allocates a new Experiment structure and asks the user for the
values to fill the Experiment fields (Experiment name and result number).
Returns: A pointer to a new struct Experiment*. It is the responsibility
of the caller to delete this struct.
*/
{
int i;
int ExpResult;
struct Exp* pNewExp;
struct Result* pNewRes;
pNewExp = (struct Exp*)malloc(sizeof(struct Exp));
printf("How many results for Experiment? :");
scanf("%d", &ExpResult);
getchar();/* this getchar eats the return left by the scanf. */
for (i = 0; i < ExpResult; ++i)
{
printf("Please enter the name of the Experiment:");
gets(pNewExp->ExpName);
pNewRes = (struct Result*)malloc(sizeof(struct Result));
printf("Please enter result %d for Experiment %s :", i + 1, pNewExp->ExpName);
/* pNewValue = (struct Result*)malloc(sizeof(struct Result)); */
scanf("%f", pNewRes->value);
getchar();/* this getchar eats the return left by the scanf. */
}
return pNewExp;
}
void PrintExp(struct Exp* pExp)
/*
Assumes: ???????
Results: Prints the name and value number fields of the Experiment struct
Returns: none
*/
{
printf("Experiment name%s\n", pExp->ExpName);
printf("Result: %f\n", pExp->pt_res);
}
void PrintExpList(struct ExpList* pList)
/*
Assumes: ???????
Results: Prints the name and phone number for every Experiment in the list
pList. The Experiment records are seperated on the screen by a
blank line.
Returns: none
*/
{
/* The first element in the list is a special case */
PrintExp(pList->pExp);
while (pList->value)
{
pList = pList->value;
PrintExp(pList->pExp);
}
}
void DeleteExp(struct Exp* pExp)
/*
Assumes: ???????
Results: frees all memory allocated for the Experiment struct pointed to
by pEmp.
Returns: none
*/
{
free(pExp);
}
void DeleteExpList(struct ExpList* pList)
/*
Assumes: ???????
Results: Deletes every Experiment in the list pList and every ExperimentList
struct except the first one.
Returns: none
*/
{
struct ExpList* pCursor;
/* we have to treat the first node in the list as a special case */
DeleteExp(pList->pExp);
pList = pList->value;
while (pList)
{
DeleteExp(pList->pExp);
pCursor = pList;
pList = pList->value;
free(pCursor);
}
}
int main()
{
/* this is our list handle for the duration of main() */
struct ExpList list;
/* a utility pointer to help us build the list */
struct ExpList* pListTail;
/* a utility pointer for new list nodes */
struct ExpList* pNewListElement;
/* a utility pointer for new data elements */
struct Exp* pNewExp;
/* declare some variables for the control logic */
int nNumExps;
int i;
/* Do some initialization to make our list safe (and empty) */
list.pExp = NULL;
list.value = NULL;
/* Ask the user how many Exps to enter into the list */
printf("How many Experiments are there?: ");
scanf("%d", &nNumExps);
getchar();/* this getchar eats the return left by the scanf. */
/* Now for each Experiment get a new record and add it to the list. */
for (i = 0; i < nNumExps; i++)
{
/* Get a new data object from the user */
pNewExp = GetExp();
if (list.pExp == NULL)
{
/* This is the first element in the list... */
list.pExp = pNewExp;
pListTail = &list;
}
else
{
/* there are already elements in the list (at least one),
so add this one to the end */
/* Step one is to make a new list node */
pNewListElement = (struct ExpList*)malloc(sizeof(struct ExpList));
/* Now hook that new list node up to the end of the list */
pListTail->value = pNewListElement;
/* then move the tail marker to the new last node */
pListTail = pNewListElement;
/* Now make sure the new last node's next points to NULL so we can
find the end of the list */
pListTail->value = NULL;
/* and lastly, make the new last node point at the new Experiment
record. */
pListTail->pExp = pNewExp;
}
}
/* Print the list of Experiments out to the screen */
PrintExpList(&list);
/* The last thing we need to do before we exit is to free all the memory we
allocated. */
DeleteExpList(&list);
return 0;
}
|
| Gkitty is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Please edit your post and apply code tags. It makes it much easier to read. [code] ...Your code here... [/code] That way it looks nice and formatted. You shouldn't use gets. It's prone to buffer overflow which likes to crash programs. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Well since Quzah asked so nice and I'm not seeing code tags yet, I'll just add that you would use fgets in gets' place. |
| master5001 is offline | |
| | #4 |
| Registered User Join Date: Oct 2002
Posts: 10
| I get an error message for "too few parameters" with fgets. What parameters am I suppose to add here? Some code would help, please help. I have never used fgets before and I know very little about it. |
| Gkitty is offline | |
| | #6 |
| Registered User Join Date: Oct 2002
Posts: 10
| I still do not understand how I am suppose to use fgets with structures - What parameters am I missing if I replace the "gets" with "fgets"? Am I missing a variable that I need to add for fgets to work in my program? Please help! |
| Gkitty is offline | |
| | #7 |
| Registered User Join Date: Oct 2002
Posts: 10
| Thanks Salem! |
| Gkitty is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with linked list | tallgeese84 | C++ Programming | 6 | 11-17-2005 10:51 PM |
| C programming-Linked list problem | male4329 | C Programming | 18 | 06-02-2005 02:05 AM |
| Problem with linked list ADT and incomplete structure | prawntoast | C Programming | 1 | 04-30-2005 01:29 AM |
| Linked List Problem | animeaholic | C Programming | 1 | 12-09-2002 06:36 PM |