/* 范例:9-17 */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <iomanip.h>
typedef struct node
{
char name[11]; /* 姓名最长10字符 */
int score; /* 成绩 */
struct node *next; /* 指向下一个节点 */
} Node_type;
char choose_menu(void);
Node_type *head; /* 链表开始地址 */
Node_type *tail; /* 链表结尾地址 */
Node_type *nowptr; /* 链表目前节点地址 */
Node_type *prev; /* 记录上一节点地址 */
Node_type *newptr; /* 新增节点地址 */
char str1[11];
void insert_mode(void);
void delete_mode(void);
void show_mode(void);
/* 新增模式 */
void insert_mode(void)
{
char yn='n';
yn='y';
while((tolower(yn)=='y'))
{
newptr = (Node_type *)malloc(sizeof(Node_type));
printf("\n请输入姓名: ");
gets(str1);
if(strlen(str1)>10)
{
str1[10]='\0';
printf("姓名超过长度,已截去!\n\n");
}
strcpy(newptr->name,str1);
printf("请输入分数(0~100): ");
gets(str1);
newptr->score=atoi(str1);
while(1)
{
if((newptr->score)<=100 && (newptr->score)>=0)
break;
printf("请重新输入(0~100):");
gets(str1);
newptr->score=atoi(str1);
}
nowptr=head;
if(head==NULL) /* 原先无数据 */
{
newptr->next=NULL;
head=newptr;
tail=newptr;
nowptr=newptr;
}
else
{
/* 插入第一笔前 */
if((newptr->score)>(head->score))
{
newptr->next=head;
head=newptr;
nowptr=newptr;
}
else /* 插到中间 */
{
while(nowptr->next!=NULL)
{
prev=nowptr;
nowptr=nowptr->next;
if((newptr->score)>(nowptr->score))
{
newptr->next=nowptr;
prev->next=newptr;
nowptr=newptr;
break;
}
}
/* 插到结尾 */
if(nowptr==tail && newptr->score<=nowptr->score)
{
nowptr->next=newptr;
newptr->next=NULL;
tail=newptr;
nowptr=newptr;
}
}
}
/* 注意此处千万别自作聪明 free 掉 newptr; */
printf("是否继续?(y/n)");
do
{
yn = getche();
}while(tolower(yn)!='y' && tolower(yn)!='n');
}
}
void delete_mode()
{
Node_type *clear;
char tmpName[11]="";
if(head==NULL) /* 无数据 */
{
printf("\n无数据可输出!\n");
tail=NULL;
return;
}
else
{
printf("\n请输入欲删除的姓名: ");
gets(tmpName);
if(strlen(tmpName)>10)
{
tmpName[10]='\0';
printf("查无此人!\n\n");
return;
}
nowptr=head;
/* 删第一笔 */
if(strcmp(nowptr->name,tmpName)==0)
{
clear=nowptr;
head=nowptr->next;
if(nowptr->next==NULL) /* 只第一笔 */
tail=NULL;
printf("\n%s已删除!\n",clear->name);
free(clear);
return;
}
else
{
prev=nowptr;
while(nowptr->next!=NULL) /* 删除中间的 */
{
nowptr=nowptr->next;
if(strcmp(nowptr->name,tmpName)==0)
{
clear=nowptr;
prev->next=nowptr->next;
printf("\n%s已删除!\n",clear->name);
free(clear);
return;
}
prev=prev->next; /* 上一笔也要跟着+1 */
}
if(nowptr->next == NULL) /* 要删除最后一个节点 */
{
if(strcmp(nowptr->name,tmpName)==0)
{
clear=nowptr;
prev->next=NULL;
tail=prev;
printf("\n%s已删除!\n",clear->name);
free(clear);
return;
}
else
printf("\n查无此人!\n");
}
}
}
}
void show_mode()
{
int count;
count=0;
if(head==NULL)
{
printf("\n\n无数据可显示!\n");
printf("\n\n请按任意键继续....");
getchar();
return;
}
else
{
nowptr=head;
printf("\n\n姓名\t\t分数");
printf("\n====\t\t====\n\n");
while((nowptr->next)!=NULL)
{
printf("%s",nowptr->name);
printf("%*d\n",20-strlen(nowptr->name), nowptr->score);
count++;
nowptr=nowptr->next;
}
printf("%s",nowptr->name);
printf("%*d\n",20-strlen(nowptr->name),nowptr->score);
count++; /* "%*d...",...详见第四章printf()的用法 */
nowptr=nowptr->next;
}
printf("====================\n");
printf("共%d笔数据!\n\n",count);
printf("\n\n请按任意键继续....");
getchar();
}
char choose_menu(void)
{
char option1;
system("cls");
printf("\n************************************");
printf("\n\t1.新增数据");
printf("\n\t2.删除数据");
printf("\n\t3.列出数据");
printf("\n\t4.离开系统");
printf("\n************************************");
printf("\n\t请输入您的选择(1,2,3,4): ");
do
{
option1 = getch();
}while((option1 != '1')&&(option1 != '2')&&(option1 != '3')
&&(option1 != '4'));
return option1;
}
void main(void)
{
char op='1';
while(1)
{
do{
op=choose_menu();
}while(op!='1' && op!='2' &&op!='3' && op!='4');
switch(op)
{
case '1':
insert_mode();
break;
case '2':
delete_mode();
printf("\n\n请按任意键继续...");
getchar();
break;
case '3':
show_mode();
break;
case '4':
return;
}
}
}
输出结果如下:
没有合适的资源?快使用搜索试试~ 我知道了~
C_C++完美演绎源代码
共224个文件
txt:222个
htm:1个
url:1个
需积分: 0 80 下载量 39 浏览量
2008-07-25
08:59:40
上传
评论
收藏 125KB RAR 举报
温馨提示
代码,大家一起学习学习
资源详情
资源评论
资源推荐
收起资源包目录
C_C++完美演绎源代码 (224个子文件)
使用说明.htm 5KB
9-17.txt 6KB
11-5.txt 4KB
15-1.txt 4KB
13-1.txt 3KB
3-15.txt 3KB
15-3.txt 3KB
17-10.txt 3KB
6-22.txt 3KB
9-14.txt 2KB
15-2.txt 2KB
13-2.txt 2KB
11-2.txt 2KB
4-4.txt 2KB
4-3.txt 2KB
11-3.txt 2KB
14-13.txt 2KB
11-8.txt 2KB
18-11.txt 2KB
8-18.txt 2KB
7-14.txt 2KB
18-4.txt 2KB
17-11.txt 1KB
14-6.txt 1KB
4-2.txt 1KB
5-2.txt 1KB
16-2.txt 1KB
9-5.txt 1KB
18-12.txt 1KB
7-16.txt 1KB
20-1.txt 1KB
7-10.txt 1KB
14-11.txt 1KB
21-6.txt 1KB
9-30.txt 1KB
18-1.txt 1KB
18-7.txt 1KB
21-8.txt 1KB
14-29.txt 1KB
9-16.txt 1KB
13-4.txt 1KB
16-7.txt 1KB
16-1.txt 1KB
7-9.txt 1KB
19-2.txt 1KB
7-13.txt 1KB
19-1.txt 1KB
21-1.txt 1KB
18-3.txt 1KB
9-19.txt 1KB
9-8.txt 1KB
11-1.txt 1KB
7-4.txt 1KB
5-3.txt 1KB
14-2.txt 1KB
14-19.txt 1KB
14-9.txt 1KB
18-10.txt 1KB
3-16.txt 1KB
17-2.txt 1KB
16-6.txt 1KB
17-8.txt 1KB
14-7.txt 994B
7-5.txt 991B
16-5.txt 989B
17-9.txt 989B
14-15.txt 980B
14-10.txt 978B
16-3.txt 974B
3-9.txt 973B
9-18.txt 952B
21-4.txt 951B
11-6.txt 949B
9-22.txt 949B
7-7.txt 943B
21-3.txt 934B
3-1.txt 931B
14-28.txt 914B
4-10.txt 910B
14-12.txt 901B
9-25.txt 893B
14-17.txt 890B
7-2.txt 882B
4-7.txt 867B
14-14.txt 856B
8-12.txt 853B
9-15.txt 836B
16-4.txt 819B
13-5.txt 812B
6-15.txt 790B
9-23.txt 780B
14-18.txt 776B
18-5.txt 776B
14-33.txt 774B
11-7.txt 769B
7-15.txt 761B
5-7.txt 760B
14-26.txt 749B
9-10.txt 747B
3-5.txt 747B
共 224 条
- 1
- 2
- 3
yflying1988
- 粉丝: 8
- 资源: 251
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0