使用递归在字符链表(使用随机数生成器插入字符)中搜索元素的程序
A program to search for an element in a linked list of characters (insert characters using random number generator) using recursion
有人可以帮我实现这个吗?
我无法创建执行此操作的逻辑。如何插入随机字符以及如何递归搜索它?这是我到目前为止所做的...
#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();
}
将此程序作为您程序的模板。该程序具有在列表中搜索字符的递归方法。
#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 );
}
如果输入例如
A E I O U @
那么程序输出可能看起来像
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): @
函数find_node
returns一个节点。因此,您可以使用它来查找字母的所有出现。
例如
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 );
顺便说一句,在俄罗斯也有名字 Katherine :)
我已经评论了更改的地方。您需要具体化链表概念。希望这有帮助
`#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;
}
`
有人可以帮我实现这个吗? 我无法创建执行此操作的逻辑。如何插入随机字符以及如何递归搜索它?这是我到目前为止所做的...
#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();
}
将此程序作为您程序的模板。该程序具有在列表中搜索字符的递归方法。
#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 );
}
如果输入例如
A E I O U @
那么程序输出可能看起来像
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): @
函数find_node
returns一个节点。因此,您可以使用它来查找字母的所有出现。
例如
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 );
顺便说一句,在俄罗斯也有名字 Katherine :)
我已经评论了更改的地方。您需要具体化链表概念。希望这有帮助
`#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;
}
`