Results 1 to 3 of 3
Thread: linked lists
Hybrid View
-
24th November 2010 13:51 #1Registered User
Join Date: Jun:2010
Location:
Posts: 171
linked lists
, linked list
typedef ListNode *ListNodePtr; /* synonym for ListNode* */
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 */ }If you understand what you're doing, you're not learning anything.
-
26th November 2010 12:58 #2
typedef- . , , :
:Code:typedef ListNode *ListNodePtr;
ListNodePtr ListNode*Code:typedef ListNode* ListNodePtr;
typedef e #define, , . (, - Java, )MSI B450 Gaming Pro Carbon AC | Ryzen 9 5900x | HyperX Predator 3200 | Asus Strix 3090 | Kingston KC3000 2TB | WD Red 4TB | Dell G3223Q + LG 27UK650-W | Arctic Freezer 360 | Seasonic Focus GX-1000 | Lian Li O11 Dynamic
-
28th November 2010 00:22 #3Registered User
Join Date: Jun:2010
Location:
Posts: 171
If you understand what you're doing, you're not learning anything.




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in