Take this program as a template for your program. This program has a recursive method of searching a character in the list.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct Node
{
char data;
struct Node *next;
};
void insert_front( struct Node **head, char c )
{
struct Node *tmp = malloc( sizeof( struct Node ) );
if ( tmp != NULL )
{
tmp->data = c;
tmp->next = *head;
*head = tmp;
}
}
void free_list( struct Node *head )
{
while ( head != NULL )
{
struct Node *tmp = head;
head = head->next;
free( tmp );
}
}
void display_list( struct Node *head )
{
for ( ; head != NULL; head = head->next ) printf( "%c ", head->data );
}
struct Node * find_node( struct Node *head, char c )
{
return head == NULL || head->data == c ? head : find_node( head->next, c );
}
int main( void )
{
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct Node *head = NULL;
srand( ( unsigned int )time( NULL ) );
const size_t N = 10;
for ( size_t i = 0; i < N; i++ )
{
insert_front( &head, alphabet[rand() % ( sizeof( alphabet ) - 1 )] );
}
display_list( head );
printf( "\n" );
while ( 1 )
{
struct Node *node;
char c = '@';
printf( "Enter letter to search in the list (@-exit): " );
scanf( " %c", &c );
if ( c == '@' ) break;
node = find_node( head, c );
if ( node != NULL ) printf( "Letter %c is present in the list\n", c );
else printf( "Letter %c is not present in the list\n", c );
}
free_list( head );
}
If to enter for example
A E I O U @
then the program output might look like
W H T C E H N J F N
Enter letter to search in the list (@-exit): A
Letter A is not present in the list
Enter letter to search in the list (@-exit): E
Letter E is present in the list
Enter letter to search in the list (@-exit): I
Letter I is not present in the list
Enter letter to search in the list (@-exit): O
Letter O is not present in the list
Enter letter to search in the list (@-exit): U
Letter U is not present in the list
Enter letter to search in the list (@-exit): @
Function find_node
returns a node. Thus you can use it for example to find all occurences of a letter.
for example
size_t count = 0;
struct Node *node = head;
char c = 'A';
while ( ( node = find_node( node, c ) ) != NULL )
{
++count;
node = node->next;
}
printf( "There are %zu letters %c in the list\n", count, c );
By the way in Russia there is also name Katherine :)