-
Linked list problem
Hi ,I am trying to understand linked list ,and I have hard time with the red part of the code ,is this a pointer to the first record
Code:
#include <stdio.h>
#include <stdlib.h>
typedef
struct listNode {
char data; /* each listNode contains a character */
struct listNode *nextPtr; /* pointer to next node*/
}ListNode; /* end structure listNode */
typedef ListNode *ListNodePtr; /* synonym for ListNode* */
void insert( ListNodePtr *sPtr, char value );
void printList( ListNodePtr currentPtr );
int main( void )
{
ListNodePtr startPtr = NULL; /* initially there are no nodes */
insert( &startPtr, 'a' );
insert( &startPtr, 'b' );
insert( &startPtr, 'c' );
printList( startPtr );
getch();
return 0;
} /* end main */
void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr; /* pointer to new node */
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */
newPtr = malloc( sizeof( ListNode ) ); /* create node */
if ( newPtr != NULL ) { /* is space available */
newPtr->data = value; /* place value in node */
newPtr->nextPtr = NULL; /* node does not link to another node */
previousPtr = NULL;
currentPtr = *sPtr;
/* loop to find the correct location in the list */
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
} /* end while */
/* insert new node at beginning of list */
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
} /* end if */
else { /* insert new node between previousPtr and currentPtr */
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
} /* end else */
} /* end if */
else {
printf( "%c not inserted. No memory available.\n", value );
} /* end else */
} /* end function insert */
void printList( ListNodePtr currentPtr )
{
/* if list is empty */
if ( currentPtr == NULL ) {
printf( "List is empty.\n\n" );
} /* end if */
else {
printf( "The list is:\n" );
/* while not the end of the list */
while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
} /* end while */
printf( "NULL\n\n" );
} /* end else */
}
-
No, it's a typedef (a type definition). It simply creates a new type called ListNodePtr which is a ListNode *. It allows you to hide the * (which in my opinion is a bad thing).
So this
Code:
ListNodePtr newPtr; /* pointer to new node */
is actually the same as
Code:
ListNode* newPtr; /* pointer to new node */
-
Thanks,how shall i represent to be more understndable