2

Can some one please help me how to implement this?? I am unable to create a logic to do this. How to insert random characters and how to then search recursively through it? This is what I 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;
    do
    {
        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 you want to creat another : ");
        ch=getche();
    }while(ch!='n');
}


void main()
{
    create();
    display();
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Katherine
  • 281
  • 1
  • 2
  • 13
  • 2
    Actually storing the read-characters in the list rather than just creating a nodes you eventually leak and throwing away the chars you read would probably be a good place to start. Unrelated, (a) `void` isn't a standard return type for `main()`, (b) [don't cast `malloc` in C programs](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc?s=1|8.2015), and (c) Turbo C++ is probably older than *you* are. Consider a more recent toolchain (perhaps one from at least the last *decade*). – WhozCraig Aug 19 '15 at 17:34
  • For starting, you should do it without using functions but if you want to use functions then make the variables new_node and current as static. Your logic of linking the linked list (the if else block) is correct. BTW you haven't entered data (char data) in any of these linked list nodes. –  Aug 19 '15 at 17:38
  • Display function nowhere defined. –  Aug 19 '15 at 17:49
  • When calling function malloc, in C, do not cast the returned value as it is already a void* so can be assigned to any other pointer. Always check (!=NULL) the returned value to assure the operation was successful – user3629249 Aug 20 '15 at 13:16
  • what is 'start' and 'current'? – user3629249 Aug 20 '15 at 13:18

2 Answers2

2

I have commented where the changes have been made. You need to concrete your linked list concepts. Hope this helps

  `#include<stdio.h>
   #include<conio.h>
   #include<alloc.h>

   //-------------------------------------------------
   struct node
   {
    char data;
    struct node *next;
   }*start=NULL;
   //------------------------------------------------------------
 static struct node *new_node,*current;  //declared these static outside the create funtion
  void create()
    {
     char ch;
     do
      {


       new_node=(struct node *)malloc(sizeof(struct node));

        /*I want random characters inserted here*/
       new_node->next=NULL;
        printf("Enter the data in the nodes: "); //asking you to enter data in nodes
        scanf(" %c",&(new_node->data));    //entering data in the linked list node
       if(start==NULL)
        {
         start=new_node;
         current=new_node;
        }
       else
        {
        current->next=new_node;
        current=new_node;
        }

    printf("nDo you want to creat another : ");
    ch=getche();
}while(ch!='n');
}

void display()   //display function defined here
    {
      struct node *arbitrary_pointer;
      arbitrary_pointer=start;
      while(arbitrary_pointer!=NULL)
       {
         printf(" %c",arbitrary_pointer->data);
         arbitrary_pointer=arbitrary_pointer->next;
       }
    }
  int main()              //changed return type of main function from void to int
   {
    create();
    display();
    return 0;
   }

`

1

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 :)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335