hi, I have created this LinkList in C. There is no problem with this program but I would like to hear some good advice from others if my coding needs modification. I cannot see any flaw on my own, but those who are experienced in C Programming I hope I would hear something from all of you that would help realize something I could use to improve my coding.
Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *nextPtr;
};
typedef struct Node NodePtr;
typedef NodePtr *ListNodePtr;
void addInFront( ListNodePtr*, int );
void addAtBack( ListNodePtr*, int );
void searchAndDestroy( ListNodePtr*, int );
void deleteFromFront( ListNodePtr* );
void deleteFromBack( ListNodePtr* );
void searchItemList( ListNodePtr, int );
void displayItemList( ListNodePtr );
void menu( void );
int found = 0;
int main( void )
{
ListNodePtr Head = NULL;
int inputValue, selection;
char choose;
clrscr();
do
{
clrscr();
gotoxy( 5, 3 );cprintf( "SINGLY Linked-List\n\n" );
gotoxy( 5, 5 );cprintf( "[A]dd Item\n" );
gotoxy( 5, 6 );cprintf( "[D]elete Item\n" );
gotoxy( 5, 7 );cprintf( "Dis[P]lay Item\n" );
gotoxy( 5, 8 );cprintf( "[S]earch Item\n" );
gotoxy( 5, 9 );cprintf( "[Q]uit Program\n" );
gotoxy( 5, 10 );choose = toupper( getch() );
if( choose == 'A' )
{
gotoxy( 5, 12 );printf( "Enter Item: " );scanf( "%d", &inputValue );fflush( stdin );
gotoxy( 5, 15 );printf( "1. Add In Front" );
gotoxy( 5, 16 );printf( "2. Add At Back " );scanf( "%d", &selection );fflush( stdin );
if( selection == 1 )
{
addInFront( &Head, inputValue );
}
else if( selection == 2 )
{
addAtBack( &Head, inputValue );
}
printf( "\n\n" );
displayItemList( Head );
}
if( choose == 'P' )
{
printf( "\n\n" );
displayItemList( Head );
}
if( choose == 'D' )
{
gotoxy( 5, 12 );printf( ">>>>Delete Item<<<<\n\n" );
gotoxy( 5, 14 );printf( "1. Delete from Front" );
gotoxy( 5, 15 );printf( "2. Delete from Back" );
gotoxy( 5, 16 );printf( "3. Search and Destroy" );scanf( "%d", &selection );fflush( stdin );
if( selection == 1 )
{
deleteFromFront( &Head );
}
else if( selection == 2 )
{
deleteFromBack( &Head );
}
else if( selection == 3 )
{
gotoxy( 5, 13 );printf( "Delete Item: " );scanf( "%d", &inputValue );fflush( stdin );
searchAndDestroy( &Head, inputValue );
}
displayItemList( Head );
}
if( choose == 'S' )
{
printf( ">>>>Search Item<<<<\n\n" );
printf( "Search item: " );scanf( "%d", &inputValue );fflush( stdin );
searchItemList( Head, inputValue );
printf( "\n\n" );
displayItemList( Head );
}
}while( choose != 'Q' );
return 0;
}
//add item in front
void addInFront( ListNodePtr *nodePtr, int rcvValue )
{
ListNodePtr NewPtr;
NewPtr = ( NodePtr * ) malloc( sizeof( NodePtr ) );
if( NewPtr == NULL )
{
printf( "No available memory" );
exit( 1 );
}
else
{
NewPtr->value = rcvValue;
NewPtr->nextPtr = *nodePtr;
*nodePtr = NewPtr;
}
}
//add item at back
void addAtBack( ListNodePtr *nodePtr, int rcvValue )
{
ListNodePtr NewPtr;
ListNodePtr LastPtr = *nodePtr;
NewPtr = ( NodePtr * ) malloc( sizeof( NodePtr ) );
if( NewPtr == NULL )
{
printf( "No available memory" );
exit( 1 );
}
else
{
if( *nodePtr == NULL )
{
NewPtr->value = rcvValue;
NewPtr->nextPtr = *nodePtr;
*nodePtr = NewPtr;
}
else
{
NewPtr->value = rcvValue;
NewPtr->nextPtr = NULL;
while( LastPtr->nextPtr != NULL )
{
LastPtr = LastPtr->nextPtr;
}
LastPtr->nextPtr = NewPtr;
}
}
}
void deleteFromFront( ListNodePtr *nodePtr )
{
ListNodePtr tempPtr = *nodePtr;
if( nodePtr == NULL )
printf( "List Empty!" );
else
*nodePtr = tempPtr->nextPtr;
free( tempPtr );
}
void deleteFromBack( ListNodePtr *nodePtr )
{
ListNodePtr tempPtr = *nodePtr;
ListNodePtr previousPtr = NULL;
if( *nodePtr == NULL )
{
printf( "Empty List!" );
}
else
{
while( tempPtr->nextPtr != NULL )
{
previousPtr = tempPtr;
tempPtr = tempPtr->nextPtr;
}
if( previousPtr == NULL )
*nodePtr = previousPtr->nextPtr;
else
previousPtr->nextPtr = tempPtr->nextPtr;
}
free( tempPtr );
}
void searchAndDestroy( ListNodePtr *nodePtr, int rcvValue )
{
ListNodePtr previousPtr, workingPtr;
previousPtr = NULL;
workingPtr = *nodePtr;
while( workingPtr != NULL && rcvValue != workingPtr->value )
{
previousPtr = workingPtr;
workingPtr = workingPtr->nextPtr;
}
if( workingPtr == NULL )
{
printf( "NULL" );
}
else
{
if( previousPtr == NULL )
{
*nodePtr = workingPtr->nextPtr;
}
else
{
previousPtr->nextPtr = workingPtr->nextPtr;
}
}
free( workingPtr );
}
void searchItemList( ListNodePtr nodePtr, int rcvValue )
{
if( nodePtr != NULL )
{
while( nodePtr != NULL && rcvValue != nodePtr->value )
{
nodePtr = nodePtr->nextPtr;
}
if( rcvValue == nodePtr->value )
{
printf( "\n\nFound %d\n\n", nodePtr->value );
}
}
}
void displayItemList( ListNodePtr nodePtr )
{
if( nodePtr != NULL )
{
while( nodePtr != NULL )
{
printf( "%d-->", nodePtr->value );
nodePtr = nodePtr->nextPtr;
}
}
printf( "NULL" );
getch();
}