第17章 高级数据表示 17.7 二叉搜索树 （第三部分 试用树） 顶原

idreamo

``````/*petclub.c --使用二叉搜索树*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "tree.h"

void deoppet(Tree * pt);
void showpets(const Tree * pt);
void findpet(const Tree * pt);
void printitem(Item item);
void uppercase(char * str);

int main(void)
{
Tree pets;
char choice;

IntializeTree(&pets);
{
switch(choice)
{
break;
case 'l':showpets(&pets);
break;
case 'f':findpet(&pets);
break;
case 'n':printf("%d pets in club\n",TreeItemCount(&pets));
break;
case 'd':droppet(&pets);
break;
default:puts("Switching error");
}
}
DeleteAll(&pets);
puts("Bye.");

return 0;
}

{
int ch;

puts("Nerfville Pet Club Membership program");
puts("Enter the letter corresponding to your choice: ");
puts("a) add a pet        l) show list of pets");
puts("n) number of pets   f) find pets");
puts("d) delete a pet     q) quit");
while((ch=getchar())!=EOF)
{
while(getchar()!='\n')
continue;
ch=tolower(ch);
if(strchr("alrfndq"==NULL))
puts("Please enter an a,l,f,n,d, or q:");
else
break;
}
if(ch==EOF)  /*令EOF导致程序退出*/
ch='q';

return ch;
}

{
Item temp;

if(TreeIsFull(pt))
puts("No room in the club!");
else
{
puts("please enter name of pet: ");
gets(temp.petname);
puts("please enter kind of pet: ");
gets(temp.petkind);
uppercase(temp.petname);
uppercase(temp.petkind);
}
}

void showpets(const Tree * pt)
{
if(TreeIsEmpty(pt))
puts("No entries!");
else
Traverse(pt,printitem);
}

void printitem(Item item)
{
printf("Pet: %-19s kind: %-19s\n",item.petname,item.petkind);
}

void findpet(const Tree * pt)
{
Item temp;

if(TreeIsEmpty(pt))
{
puts("No entries!");
return;    /*如果树为空，则退出函数*/
}

puts("Please enter the name of pet you wish to find: ");
gets(temp.petname);
gets(temp.petkind);
uppercase(temp.petname);
uppercase(temp.petkind);
printf("%s the %s",temp.petname,temp.petkind);
if(Intree(&temp,pt))
printf("is a member.\n");
else
printf("is not a member.\n");
}

void droppet(Tree * pt)
{
Item temp;

if(TreeIsEmpty(pt))
{
puts("No entries!");
return;  /*如果树为空，则退出函数*/
}

puts("please enter name of pet you wish to delete: ");
gets(temp.petname);
gets(temp.kind);
uppercase(temp.petname);
uppercase(temp.petkind);
if(DeleteItem(&temp,pt))
printf("is dropped from the club.\n");
else
printf("is not a member.\n");
}

void uppercase(char * str)
{
while(*str)
{
*str = toupper(*str);
str++;

}
}``````

``````Nerfville pet Club Membership Program
Enter the letter corresponding to your choice
a) add a pet       l) show list of pets
n) number of pets  f) find pets
q) quit
a
Quincy
pig
Nerfville pet Club Membership Program
Enter the letter corresponding to your choice
a) add a pet       l) show list of pets
n) number of pets  f) find pets
q) quit
a
Betty
Boa
Nerfville pet Club Membership Program
Enter the letter corresponding to your choice
a) add a pet       l) show list of pets
n) number of pets  f) find pets
q) quit
a
Hiram Jinx
domestic cat
Nerfville pet Club Membership Program
Enter the letter corresponding to your choice
a) add a pet       l) show list of pets
n) number of pets  f) find pets
q) quit
n
3 pets in club
Nerfville pet Club Membership Program
Enter the letter corresponding to your choice
a) add a pet       l) show list of pets
n) number of pets  f) find pets
q) quit
l
pet:BETTY    kind:BOA
pet:HIRAM JINX  kind:DOMESTIC CAT
pet:QUINCY      kind:PIG
Nerfville pet Club Membership Program
Enter the letter corresponding to your choice
a) add a pet       l) show list of pets
n) number of pets  f) find pets
q) quit
q
Bye.

``````

idreamo

程序 = 数据结构 + 算法 　　——图灵奖得主，计算机科学家N.Wirth(沃斯) 　　 　　作为程序员，我们做机器学习也好，做python开发也好，java开发也好。 　　有一种对所有程序员无一...

java进阶架构师
2018/10/25
0
0

2017/12/18
0
0
LeetCode算法题-Convert Sorted Array to Binary Search Tree（Java实现）

2018/11/09
0
0
[算法总结] 20 道题搞定 BAT 面试——二叉树

2018/09/04
0
0

2017/11/08
0
0

HTTP 304状态码

Jack088
9分钟前
0
0
MyBatis学习笔记（二）

mybatis执行过程架构图 1、mybatis配置 SqlMapConfig.xml，此文件作为mybatis的全局配置文件，配置了mybatis的运行环境等信息。 mapper.xml文件即sql映射文件，文件中配置了操作数据库的sql...

14分钟前
0
0

zbbmaster
24分钟前
0
0
PHP接收前端传值各种情况整理

PHP接收前端传值各种情况整理 服务端代码: header('Access-Control-Allow-Origin:*');var_dump(\$_POST);exit; 情况 1) 传null \$.post('http://xxxxx.xx/index.php', { "test": null}......

SSSWIIILLL
47分钟前
3
0

CodeSheep

19
0