Untitled
unknown
plain_text
2 years ago
4.1 kB
3
Indexable
#include <stdlib.h> struct listNode { // self-referential structure char info; struct listNode *nextPtr; }; typedef struct listNode LISTNODE; typedef LISTNODE *LISTNODEPTR; void insert( LISTNODEPTR *, char ); char delete( LISTNODEPTR *, char ); int isEmpty( LISTNODEPTR ); void printList( LISTNODEPTR ); void instructions( void ); int main() { LISTNODEPTR startPtr = NULL; int choice; char item; instructions(); // display the menu scanf( "%d", &choice ); while ( choice != 0 ) { switch (choice) { case 1: printf( "Enter a character: " ); scanf( " %c", &item ); insert( &startPtr, item ); printList( startPtr ); break; case 2: if ( !isEmpty( startPtr ) ) { printf( "Enter character to be deleted: " ); scanf( " %c", &item ); if ( delete( &startPtr, item ) ) { printf( "%c deleted.\n", item ); printList( startPtr ); } else printf( "%c not found.\n\n", item ); } else printf( "List is empty.\n\n" ); break; default: printf( "Invalid choice.\n\n" ); break; } instructions(); // display the menu scanf( "%d", &choice ); } printf( "Program is going to exit. Press any key to continue\n" ); getch(); return 0; } // Print the instructions void instructions( void ) { printf( "Enter your choice:\n" " 1 to insert an element into the list.\n" " 2 to delete an element from the list.\n" " 0 to end.\n" ); printf( " ? " ); } // Insert a new item into the list in sorted order void insert( LISTNODEPTR *sPtr, char item ) { LISTNODEPTR newNodeLink, prevNodeLink, curNodeLink; newNodeLink =(LISTNODEPTR) malloc( sizeof( LISTNODE ) ); if ( newNodeLink != NULL ) { // is space available newNodeLink->info = item; newNodeLink->nextPtr = NULL; prevNodeLink = NULL; curNodeLink = *sPtr; while ( curNodeLink != NULL && item > curNodeLink->info ) { prevNodeLink = curNodeLink; // walk to ... curNodeLink = curNodeLink->nextPtr; // ... next node } if ( prevNodeLink == NULL ) { newNodeLink->nextPtr = *sPtr; *sPtr = newNodeLink; } else { prevNodeLink->nextPtr = newNodeLink; newNodeLink->nextPtr = curNodeLink; } } else printf( "%c not inserted. No memory available.\n", item ); } // Delete a list element char delete( LISTNODEPTR *sPtr, char item ) { LISTNODEPTR prevNodeLink, curNodeLink, tempPtr; if ( item == ( *sPtr )->info ) { tempPtr = *sPtr; *sPtr = ( *sPtr )->nextPtr; // de-thread the node free( tempPtr ); // free the de-threaded node return item; } else { prevNodeLink = *sPtr; curNodeLink = ( *sPtr )->nextPtr; while ( curNodeLink != NULL && curNodeLink->info != item ) { prevNodeLink = curNodeLink; // walk to ... curNodeLink = curNodeLink->nextPtr; // ... next node } if ( curNodeLink != NULL ) { tempPtr = curNodeLink; prevNodeLink->nextPtr = curNodeLink->nextPtr; free( tempPtr ); return item; } } return '\0'; } // Return 1 if the list is empty, 0 otherwise int isEmpty( LISTNODEPTR sPtr ) { return sPtr == NULL; } // Print the list void printList( LISTNODEPTR curNodeLink ) { if ( curNodeLink == NULL ) printf( "List is empty.\n\n" ); else { printf( "The list is:\n" ); while ( curNodeLink != NULL ) { printf( "%c --> ", curNodeLink->info ); curNodeLink = curNodeLink->nextPtr; } printf( "NULL\n\n" ); system("pause"); } }
Editor is loading...