一种程序,用于使用递归搜索链接字符列表中的元素(使用随机数生成器插入字符)

时间:2021-09-03 07:17:43

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();
}

2 个解决方案

#1


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.

函数find_node返回一个节点。因此,您可以使用它来查找字母的所有出现。

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

顺便说一句,在俄罗斯也有名称凯瑟琳:)

#2


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


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.

函数find_node返回一个节点。因此,您可以使用它来查找字母的所有出现。

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

顺便说一句,在俄罗斯也有名称凯瑟琳:)

#2


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;
   }

`