利用自己的姓名拼音字母建立一个单链表(带头结点)。
输入格式样例:
请输入姓名:LIMING
建立的单链表为:L->I->M->I->N->G
在题1所建立的单链表基础上,统计某一个字母出现的次数。
输入格式样例:
请输入姓名:LIMING
字母I出现的次数:2
#include<stdio.h>
#include<stdlib.h>
struct cha
{
char c;
cha *next;
}*head;
struct cha * create(char *p)
{
struct cha *tail = NULL;
for(int i=0; p[i] != 0; i++)
{
struct cha *q = (struct cha *)malloc(sizeof(struct cha));
q->c = p[i];
q->next = NULL;
if(head == NULL)
{
head = q;
}
else
{
tail->next = q;
}
tail = q;
}
return head;
}
int calcchar(char c)
{
int num = 0;
struct cha *p = head;
while(p != NULL)
{
if(p->c == c)
num++;
p = p->next;
}
return num;
}
void main()
{
char name[20];
scanf("%s",name);
head = create(name);
getchar();
char c;
scanf("%c",&c);
int num = calcchar(c);
printf("字母%c共出现%d次",c,num);
system("pause");
}