edit list

R

ritchie

Hi,

I am writing for some help with editing a linked list.
I would be gratful if anybody has the time to take a look at this.

I am trying to edit a node by entering a date as a key.
If an existing date is entered then you can edit the specified node.

I have the date defined as a nested structure and it is in three parts
(ie: yyyy, mm, dd ) and referenced by the main structure.
My problem is how do I refer to the full date in the edit function to
check if the date entered is a valid date (ie: already stored in the
list) ?

I have tried this but with no luck.
Has anyone got any idea where i'm going wrong?

Also, is my edit function correct?
Any help would be much aprechiated.

Thanks in advance,
Ritchie

------------ code -------------
struct orderDate {
int year;
int month;
int day;
};
typedef struct orderDate OrderDate;

struct node {
OrderDate order_date;
int name
.
.
struct node *next;
};
typedef Node *nPtr;
.
.
.
/***iEdit is the node (date entered) to be edited***/
void editList( NodePtr *sPtr, int iEdit )
{
NodePtr currPtr, prevPtr = NULL;

if( *sPtr == NULL ) {
printf( "List empty\n" );
}
else
if( iEdit == (*sPtr)->order_date ) //PROBLEM HERE
{
printf("Enter new value: "); scanf("%d", &(*sPtr)->name);
...
----------------end--------------------------------
 
D

David Rubin

ritchie said:
I am trying to edit a [LL] node by entering a date as a key.
If an existing date is entered then you can edit the specified node.

I have the date defined as a nested structure and it is in three parts
(ie: yyyy, mm, dd ) and referenced by the main structure.
My problem is how do I refer to the full date in the edit function to
check if the date entered is a valid date (ie: already stored in the
list) ?
[snip]
------------ code -------------
struct orderDate {
int year;
int month;
int day;
};
typedef struct orderDate OrderDate;
struct node {
OrderDate order_date;
int name
.
.
struct node *next;
};
typedef Node *nPtr;

Firstly, it is generally considered bad practice to typedef pointer
types. Also, your syntax is incorrect here. ITYM

typedef struct node Node;

Then, you can use Node* when referring to pointers to Nodes.

Secondly, you need a function which either compares Nodes by OrderDate,
or a function which simply compares OrderDates. Given two objects a and
b, such a function will typically return 0 if a==b, < 0 (e.g., -1) if
/***iEdit is the node (date entered) to be edited***/
void editList( NodePtr *sPtr, int iEdit )

void editList( Node *sPtr, int iEdit)

Your description of iEdit is confusing. I think you want iEdit to have
type OrderDate. Thus, you will edit the node matching the given date
(iEdit) if your list (sPtr) contains such a node.
{
NodePtr currPtr, prevPtr = NULL;

if( *sPtr == NULL ) {
printf( "List empty\n" );
}
else
if( iEdit == (*sPtr)->order_date ) //PROBLEM HERE
{
printf("Enter new value: "); scanf("%d", &(*sPtr)->name);
...

You want something more like

int
editList(Node *l, OrderDate date)
{
Node *np;

for(np=l; np != 0; np=np->next){
if(datecmp(date, np->order_date) == 0){
printf("Enter new value: ");
fflush(stdout); /* this is important! */
scanf("%d", np->name); /* use correct pointer! */
/* ... */
return 1;
}
}
return 0; /* node not found */
}

BTW, the part where you read values can (and probably should) be
formulated as a separate function.

/david
 
R

ritchie

Hi,

Thanks for the reply, but I am still having trouble accessing the full date.
My date is in three parts in a nested structure - ie:yyyymmdd.

For example if I even try:
/***********************************************************/
while( curPtr != NULL && dateIn > curPtr->order_date )
{
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/***********************************************************/

I get an error saying " '>' illegal for struct"...
Or even if I try to printf the date ie:
/* printf("%d", currPtr->order_date); */

but it's ok if I
/* printf("%d%d", currPtr->order_date.year, currPtr->order_date.month ); */

Is it possible to access the full date (yyyymmdd).

Can anyone see why this would cause an error?
I really would aprechiate any help on this.

Thanks again,
Ritchie




David Rubin said:
ritchie said:
I am trying to edit a [LL] node by entering a date as a key.
If an existing date is entered then you can edit the specified node.

I have the date defined as a nested structure and it is in three parts
(ie: yyyy, mm, dd ) and referenced by the main structure.
My problem is how do I refer to the full date in the edit function to
check if the date entered is a valid date (ie: already stored in the
list) ?
[snip]
------------ code -------------
struct orderDate {
int year;
int month;
int day;
};
typedef struct orderDate OrderDate;
struct node {
OrderDate order_date;
int name
.
.
struct node *next;
};
typedef Node *nPtr;

Firstly, it is generally considered bad practice to typedef pointer
types. Also, your syntax is incorrect here. ITYM

typedef struct node Node;

Then, you can use Node* when referring to pointers to Nodes.

Secondly, you need a function which either compares Nodes by OrderDate,
or a function which simply compares OrderDates. Given two objects a and
b, such a function will typically return 0 if a==b, < 0 (e.g., -1) if
/***iEdit is the node (date entered) to be edited***/
void editList( NodePtr *sPtr, int iEdit )

void editList( Node *sPtr, int iEdit)

Your description of iEdit is confusing. I think you want iEdit to have
type OrderDate. Thus, you will edit the node matching the given date
(iEdit) if your list (sPtr) contains such a node.
{
NodePtr currPtr, prevPtr = NULL;

if( *sPtr == NULL ) {
printf( "List empty\n" );
}
else
if( iEdit == (*sPtr)->order_date ) //PROBLEM HERE
{
printf("Enter new value: "); scanf("%d", &(*sPtr)->name);
...

You want something more like

int
editList(Node *l, OrderDate date)
{
Node *np;

for(np=l; np != 0; np=np->next){
if(datecmp(date, np->order_date) == 0){
printf("Enter new value: ");
fflush(stdout); /* this is important! */
scanf("%d", np->name); /* use correct pointer! */
/* ... */
return 1;
}
}
return 0; /* node not found */
}

BTW, the part where you read values can (and probably should) be
formulated as a separate function.

/david
 
N

Nick Landsberg

ritchie said:
Hi,

Thanks for the reply, but I am still having trouble accessing the full date.
My date is in three parts in a nested structure - ie:yyyymmdd.

For example if I even try:
/***********************************************************/
while( curPtr != NULL && dateIn > curPtr->order_date )
{
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/***********************************************************/

I get an error saying " '>' illegal for struct"...
Or even if I try to printf the date ie:
/* printf("%d", currPtr->order_date); */

but it's ok if I
/* printf("%d%d", currPtr->order_date.year, currPtr->order_date.month ); */

Is it possible to access the full date (yyyymmdd).

Can anyone see why this would cause an error?
I really would aprechiate any help on this.

Thanks again,
Ritchie

You specifically declared stuct orderDate to contain
three element. You must the three elements
explicity.

On the other hand, you could try a union
which would allow you to access the same
information in an alternative way.
David Rubin said:
ritchie wrote:

I am trying to edit a [LL] node by entering a date as a key.
If an existing date is entered then you can edit the specified node.

I have the date defined as a nested structure and it is in three parts
(ie: yyyy, mm, dd ) and referenced by the main structure.
My problem is how do I refer to the full date in the edit function to
check if the date entered is a valid date (ie: already stored in the
list) ?
[snip]

------------ code -------------
struct orderDate {
int year;
int month;
int day;
};
typedef struct orderDate OrderDate;


struct node {
OrderDate order_date;
int name
.
.
struct node *next;
};
typedef Node *nPtr;

Firstly, it is generally considered bad practice to typedef pointer
types. Also, your syntax is incorrect here. ITYM

typedef struct node Node;

Then, you can use Node* when referring to pointers to Nodes.

Secondly, you need a function which either compares Nodes by OrderDate,
or a function which simply compares OrderDates. Given two objects a and
b, such a function will typically return 0 if a==b, < 0 (e.g., -1) if
a < b, and > 0 (e.g., 1) if a > b; the function indicates how a compares
to b.

/***iEdit is the node (date entered) to be edited***/
void editList( NodePtr *sPtr, int iEdit )

void editList( Node *sPtr, int iEdit)

Your description of iEdit is confusing. I think you want iEdit to have
type OrderDate. Thus, you will edit the node matching the given date
(iEdit) if your list (sPtr) contains such a node.

{
NodePtr currPtr, prevPtr = NULL;

if( *sPtr == NULL ) {
printf( "List empty\n" );
}
else
if( iEdit == (*sPtr)->order_date ) //PROBLEM HERE
{
printf("Enter new value: "); scanf("%d", &(*sPtr)->name);
...

You want something more like

int
editList(Node *l, OrderDate date)
{
Node *np;

for(np=l; np != 0; np=np->next){
if(datecmp(date, np->order_date) == 0){
printf("Enter new value: ");
fflush(stdout); /* this is important! */
scanf("%d", np->name); /* use correct pointer! */
/* ... */
return 1;
}
}
return 0; /* node not found */
}

BTW, the part where you read values can (and probably should) be
formulated as a separate function.

/david
 
C

CBFalconer

ritchie said:
Thanks for the reply, but I am still having trouble accessing the
full date. My date is in three parts in a nested structure -
ie:yyyymmdd.

For example if I even try:
/***********************************************************/
while( curPtr != NULL && dateIn > curPtr->order_date )
{
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/***********************************************************/

I get an error saying " '>' illegal for struct"...
Or even if I try to printf the date ie:
/* printf("%d", currPtr->order_date); */

but it's ok if I
/* printf("%d%d", currPtr->order_date.year, currPtr->order_date.month ); */

Is it possible to access the full date (yyyymmdd).

Can anyone see why this would cause an error?
I really would aprechiate any help on this.

Due to top-posting (don't do that, your answer goes below the
material to which you reply, with non-germane parts snipped) there
is no definition of such things as curPtr, dateIn, nextPtr, etc.
However, the error message seems self-explanatory to me.

Next time, define everything used, and DO NOT top-post in this
newsgroup.
 
C

Christopher Benson-Manica

ritchie said:
My date is in three parts in a nested structure - ie:yyyymmdd.
while( curPtr != NULL && dateIn > curPtr->order_date )
I get an error saying " '>' illegal for struct"...
/* printf("%d", currPtr->order_date); */

Why does this surprise you? order_date IS a struct, not an integer or
anything else that you can meaningfully compare to an integer.
but it's ok if I
/* printf("%d%d", currPtr->order_date.year, currPtr->order_date.month ); */
Is it possible to access the full date (yyyymmdd).

Not without resorting to undesireable tricks without much prospects
for treats. What you're trying to do just isn't possible, for a
number of reasons. You can't access elements (other than the first)
of a struct without explicitly doing so, period. The FAQ has some
interesting suggestions concerning structs that you may find useful.
Even if this worked, of course, you do realize that you wouldn't get
yyyymmdd if the month and/or day were less than 10.
 
D

David Rubin

ritchie wrote:

[snip - top posting corrected]
[snip]
Secondly, you need a function which either compares Nodes by OrderDate,
or a function which simply compares OrderDates. Given two objects a and
b, such a function will typically return 0 if a==b, < 0 (e.g., -1) if
a < b, and > 0 (e.g., 1) if a > b; the function indicates how a compares
to b.
[snip]
> Thanks for the reply, but I am still having trouble accessing the full date.
> My date is in three parts in a nested structure - ie:yyyymmdd.
>
> For example if I even try:
> /***********************************************************/
> while( curPtr != NULL && dateIn > curPtr->order_date )
> {
> prevPtr = curPtr;
> curPtr = curPtr->nextPtr;
> }
> /***********************************************************/
>
> I get an error saying " '>' illegal for struct"...
> Or even if I try to printf the date ie:
> /* printf("%d", currPtr->order_date); */

What you want is something like this:

/* return -1, 0, 1 depending on whether d1 is before, same, or after d2 */
int
datecmp(const OrderDate *d1, const OrderDate *d2)
{
if(d1->year < d2->year) return -1;
if(d1->year > d2->year) return 1;

if(d1->month < d2->month) return -1;
if(d1->month > d2->month) return 1;

if(d1->day < d2->day) return -1;
if(d1->day > d2->day) return 1;

/* dates are the same */
return 0;
}

I think you need to review structures and how to access fields...

/david
 
R

ritchie

Hi,

Just wanted to thanks everyone for their replies and comments.

Thanks again,
Ritchie



David Rubin said:
ritchie wrote:

[snip - top posting corrected]
------------ code -------------
struct orderDate {
int year;
int month;
int day;
};
typedef struct orderDate OrderDate;
[snip]
Secondly, you need a function which either compares Nodes by OrderDate,
or a function which simply compares OrderDates. Given two objects a and
b, such a function will typically return 0 if a==b, < 0 (e.g., -1) if
a < b, and > 0 (e.g., 1) if a > b; the function indicates how a compares
to b.
[snip]
Thanks for the reply, but I am still having trouble accessing the full date.
My date is in three parts in a nested structure - ie:yyyymmdd.

For example if I even try:
/***********************************************************/
while( curPtr != NULL && dateIn > curPtr->order_date )
{
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
/***********************************************************/

I get an error saying " '>' illegal for struct"...
Or even if I try to printf the date ie:
/* printf("%d", currPtr->order_date); */

What you want is something like this:

/* return -1, 0, 1 depending on whether d1 is before, same, or after d2 */
int
datecmp(const OrderDate *d1, const OrderDate *d2)
{
if(d1->year < d2->year) return -1;
if(d1->year > d2->year) return 1;

if(d1->month < d2->month) return -1;
if(d1->month > d2->month) return 1;

if(d1->day < d2->day) return -1;
if(d1->day > d2->day) return 1;

/* dates are the same */
return 0;
}

I think you need to review structures and how to access fields...

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top