xdoj-469输入联想

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

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值