有人能帮我如何实现这个吗??我无法创建一个逻辑来做到这一点。如何插入随机字符,然后如何通过它递归搜索?这就是我到目前为止所做的……
#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<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 <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 listn", c );
else printf( "Letter %c is not present in the listn", 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
返回一个节点。因此,您可以使用它来查找一个字母的所有出现情况。
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 listn", count, c );
顺便说一下,在俄罗斯还有一个名字Katherine:)