c - A program to search for an element in a linked list of characters (insert characters using random number generator) using recursion -
can 1 please me how implement this?? unable create logic this. how insert random characters , how search recursively through it? have done till now...
#include<stdio.h> #include<conio.h> #include<alloc.h> //------------------------------------------------- struct node { char data; struct node *next; }*start=null; //------------------------------------------------------------ void create() { char ch; { struct node *new_node,*current; new_node=(struct node *)malloc(sizeof(struct node)); /*i want random characters inserted here*/ new_node->next=null; if(start==null) { start=new_node; current=new_node; } else { current->next=new_node; current=new_node; } printf("ndo want creat : "); ch=getche(); }while(ch!='n'); } void main() { create(); display(); }
take program template program. program has recursive method of searching character in 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 ) { ( ; 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; ( size_t = 0; < 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 search in list (@-exit): " ); scanf( " %c", &c ); if ( c == '@' ) break; node = find_node( head, c ); if ( node != null ) printf( "letter %c present in list\n", c ); else printf( "letter %c not present in list\n", c ); } free_list( head ); }
if enter example
a e o u @
then program output might like
w h t c e h n j f n enter letter search in list (@-exit): letter not present in list enter letter search in list (@-exit): e letter e present in list enter letter search in list (@-exit): letter not present in list enter letter search in list (@-exit): o letter o not present in list enter letter search in list (@-exit): u letter u not present in list enter letter search in list (@-exit): @
function find_node
returns node. can use example find occurences of 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 %zu letters %c in list\n", count, c );
by way in russia there name katherine :)
Comments
Post a Comment