C语言程序题要求: 尽量用数组、指针、函数 :输入N个字符串,统计其中出现次数最多的字符串并打印



#include <iostream>
using namespace std;

typedef struct node
{
int stat;
char str[128];
struct node *next;
}linklist;

void Insert(linklist*head, char *str)
{
linklist *rear, *s;

rear = head;
while (rear->next &&
strcmp(rear->str, str))
{
rear = rear->next;
}

if (!strcmp(rear->str, str))
{
rear->stat++;
return;
}

s = new linklist();
s->stat = 1;
strcpy(s->str, str);
s->next = rear->next;
rear->next = s;
}

void PrintMax(linklist *head)
{
linklist *rear, *s;

s = head;
rear = head->next;
while (rear)
{
if (rear->stat > s->stat)
{
s = rear;
}

rear = rear->next;
}

if (strlen(s->str) > 0)
{
printf("出现次数最多的字符串为'%s',共出现%d次\n",
s->str, s->stat);
return;
}

rear = head->next;
while (rear)
{
printf("出现次数最多的字符串为'%s',共出现%d次\n",
s->str, s->stat);
}
}

void main()
{
char str[128], ch;
linklist *head;

head = new linklist();
strcpy(head->str, "");
head->stat = 1;
head->next = NULL;
while (1)
{
printf("请输入一个字符串:");
cin>>str;
Insert(head, str);
printf("继续输入吗(y/n):");
cin>>ch;
if (toupper(ch) != 'Y')
{
break;
}
}

PrintMax(head);
system("pause");
}



Copyright © 2009-2011 All Rights Reserved.