R
ritchie
Hi,
I am writing to ask for some info with a program I am trying out. I
am teaching myself
linked lists in C.
I have a linked list (see code below), and I accept user input which
is the inputted into
the list via an 'insert function'. The users value is passed as a
paramater to 'insert'.
All is ok with this, but what I was wondering is, if I have a node
with say 10 variables instead of one, is it still ok/efficent to pass
these values to 'insert' as paramaters?
Is there another way??
Also, I know that you can delete nodes, but is there any other actions
that can be performed on linked lists that would help me learn more?
Thanks a million,
Ritchie
~~~~~~~~~~~~~~~~~~CODE START ~~~~~~~~~~~~~~~~~~~
struct node {
int iOrderDate;
struct node *nextPtr;
};
typedef struct node Node;
typedef Node *NodePtr;
/********************** fns *****************************/
void insert ( NodePtr *, int );
/********************************************************/
....
void insert (NodePtr *sPtr, int lDate)
{
NodePtr newPtr, prevPtr, currPtr;
newPtr = malloc( sizeof(Node) );
if( newPtr != NULL )
{
newPtr->iOrderDate = lDate;
newPtr->nextPtr = NULL;
prevPtr = NULL;
currPtr = *sPtr;
while( currPtr != NULL && lDate > currPtr->iOrderDate )
{
prevPtr = currPtr;
currPtr = currPtr->nextPtr;
}
if( prevPtr == NULL )
{
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else
{
prevPtr->nextPtr = newPtr;
newPtr->nextPtr = currPtr;
}
}
else
printf("unable to allocate memory!");
}
~~~~~~~~~~~~~~CODE END ~~~~~~~~~~~~~~~~~
I am writing to ask for some info with a program I am trying out. I
am teaching myself
linked lists in C.
I have a linked list (see code below), and I accept user input which
is the inputted into
the list via an 'insert function'. The users value is passed as a
paramater to 'insert'.
All is ok with this, but what I was wondering is, if I have a node
with say 10 variables instead of one, is it still ok/efficent to pass
these values to 'insert' as paramaters?
Is there another way??
Also, I know that you can delete nodes, but is there any other actions
that can be performed on linked lists that would help me learn more?
Thanks a million,
Ritchie
~~~~~~~~~~~~~~~~~~CODE START ~~~~~~~~~~~~~~~~~~~
struct node {
int iOrderDate;
struct node *nextPtr;
};
typedef struct node Node;
typedef Node *NodePtr;
/********************** fns *****************************/
void insert ( NodePtr *, int );
/********************************************************/
....
void insert (NodePtr *sPtr, int lDate)
{
NodePtr newPtr, prevPtr, currPtr;
newPtr = malloc( sizeof(Node) );
if( newPtr != NULL )
{
newPtr->iOrderDate = lDate;
newPtr->nextPtr = NULL;
prevPtr = NULL;
currPtr = *sPtr;
while( currPtr != NULL && lDate > currPtr->iOrderDate )
{
prevPtr = currPtr;
currPtr = currPtr->nextPtr;
}
if( prevPtr == NULL )
{
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else
{
prevPtr->nextPtr = newPtr;
newPtr->nextPtr = currPtr;
}
}
else
printf("unable to allocate memory!");
}
~~~~~~~~~~~~~~CODE END ~~~~~~~~~~~~~~~~~