#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct WORD {
char name[30];
int times;
} WORD;
// 链表节点定义
typedef struct Node {
char value[30];
struct Node* next;
} Node;
// 插入节点
void insertNode(Node** head, char* str) {
Node* newNode = (Node*) malloc(sizeof(Node));
if (newNode == NULL) {
return; // Memory allocation failed, return or handle the error
}
strcpy(newNode->value, str);
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void UP_to_LOW(char arr[])
{
int i=0;
int len=strlen(arr);
for(i=0;i<len;i++)
{
if('A'<=arr[i]&&arr[i]<='Z')
{
arr[i]=arr[i]+32;
}
}
}
void input(int n,Node**head){//一个链表存储一个单词
char str[21];
int i;
for(i=1;i<=n;i++){
scanf("%s",str);
UP_to_LOW(str);
insertNode(head,str);
}
}
int Count(char word[],Node**head){
Node*current=*head;
int count=0;
while(current!=NULL){
if(strcmp(current->value,word)==0){
count++;
}
current=current->next;
}
return count;
}
void Bubble_Times(WORD arr[],int wordcount){
int temp=0;
char st1[21];
int i;int j;
for(i=0;i<wordcount;i++){
for(j=1;j<wordcount-i;j++){
if(arr[j-1].times<arr[j].times){
temp=arr[j-1].times;
arr[j-1].times=arr[j].times;
arr[j].times=temp;
strcpy(st1,arr[j-1].name);strcpy(arr[j-1].name,arr[j].name);strcpy(arr[j].name,st1);
}
}
}
}
void Bubble_alapha(WORD arr[],int wordcount){
char tempstr[21];
int i;
int j;
for(i = 0; i < wordcount; i++) {
for(j = 0; j < wordcount - 1 - i; j++) {
if (arr[j].times == arr[j+1].times) {
if (strcmp(arr[j].name, arr[j+1].name) > 0) {
strcpy(tempstr,arr[j+1].name);
strcpy(arr[j+1].name,arr[j].name);
strcpy(arr[j].name, tempstr);
}
}
}
}
}
void outputprint(WORD arr[], int wordcount)
{
int i;
int words_to_print = wordcount > 5 ? 5 : wordcount;
for (i = 0; i < words_to_print; i++)
{
printf("%s", arr[i].name);
if (i != words_to_print - 1)
printf(" ");
}
}
void erase(Node**head,char word[],char ch){
Node*current=*head;
while(current!=NULL){
if(strcmp(current->value,word)==0){
if(ch=='z'){
current->value[0]=ch-1;
}
else{
current->value[0]=ch+1;
}
}
current=current->next;
}
}
void freeList(Node* head) {
Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
}
int main() {
Node* head = NULL;
int n;int count;int wordcount=0;
int i=0;int k=0;int j=0;
char ch;
char word[21]={'\0'};
scanf("%d",&n);
WORD wordnum[n];
for(i=0;i<n;i++){
for(k=0;k<21;k++){
wordnum[i].name[k]='\0';
}
}
input(n,&head);
getchar();
scanf(" %c",&ch);
if('A'<=ch&&ch<='Z'){
ch=ch+32;
}
Node*current=head;
while(current != NULL){
strcpy(word,current->value);
if(word[0]==ch){
count=Count(word,&head);
wordnum[j].times=count;
strcpy(wordnum[j].name,word);
wordcount++;
erase(&head,word,ch);
j++;
}
current=current->next;
}
freeList(head);
Bubble_Times(wordnum,wordcount);
Bubble_alapha(wordnum,wordcount);
outputprint(wordnum,wordcount);
return 0;
}
xdoj-469输入联想
最新推荐文章于 2024-09-13 21:36:15 发布