// 预处理
// 1,用条件编译的方法实现以下功能:
// 输入一行电报文字,可以任选两种输出: 一为原文输出;一为字母变成其下以字母
// (即:'a'变成'b',....,'z'变成'a','A'变成'B',....,'Z'变成'A'),其他非字母字符不变。
// 用#define命令来控制是否要译成密码。
// 例如:#define CHANG 1 则译成密码。
// 若 #define CHANG 0 则不译成密码,按原码输出。
//
// #include<stdio.h>
// #define max 80
// #define change 1
//
// void main()
// {
// char str[max];
// int i;
// printf("input string:\n");
// gets(str);
//
// #if change
// {
// for (i=0;i<max;i++)
// {
// if (str[i]!='\0')
// if(str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')
// str[i]+=1;
// else if (str[i]=='z'||str[i]=='Z')
// str[i]-=25;
// }
// }
// #endif
// puts(str);
// }
// 例程2:例2.txt对候选人得票的统计程序。
//设计3个候选人,每次输入一个得票的候选人名字,要求最后输出个人得票结果。
// #include <stdio.h>
// #include <string.h>
// struct person
// {
// char name[20];
// int count;
// };leader[3]={{"Li",0},{"Zhang",0},{"Fun",0}};
//
// void main()
// {
// int i,j;
// char leader_name[20];
// for (i=1;i<=10;i++)
// {
// scanf("%s",leader_name);
// for(j=0;j<3;j++)
// if(strcmp(leader_name,leader[j].name)==0)
// leader[j].count++;
// }
// printf("\n");
// for (i=0;i<3;i++)
// {
// printf("%5s:%d\n",leader[i].name,leader[i].count);
// }
// }
// #include <stdio.h>
// #include <string.h>
// #define FORMAT "%d\n%s\n%f\n%f\n%f\n"
// struct student
// {int num;
// char name[20];
// float score[3];
// };
// void print(struct student stu);
// // void print(struct student *p);
//
// void main()
// {
// struct student stu;
// stu.num=12345;
// strcpy(stu.name,"Li Li");
// stu.score[0]=67.5;
// stu.score[1]=89;
// stu.score[2]=78.6;
// print(stu);
// /*print(&stu);*/
// }
//
// void print(struct student stu)
// /*void print(struct student *p)*/
// {
// printf(FORMAT,stu.num,stu.name, stu.score[0], stu.score[1],stu.score[2]);
// /*printf(FORMAT,p->num,p->name, p->score[0], p->score[1],p->score[2]);*/
// printf("\n");
// }
/***************************************/
/*文件包含,预定义,变量声明,函数声明*/
/***************************************/
#include <stdio.h>
#include <malloc.h>
//#define NULL 0 //令NULL代表0;用它表示“空地址”
#define LEN sizeof(struct student) //LEN代表struct student类型数据的长度,sizeof是求字节数运算符
struct student
{
long num;
float score;
struct student *next;
}; //学生信息包括学号和分数
int n; //n为全局变量,本文件模块中各函数均可使用它,代表节点的个数
struct student *creat(void);
void print(struct student *head);
struct student *del(struct student *head, long num);
struct student *insert(struct student *head,struct student *stud);
/******************************************************/
/*主函数:对链表的综合操作
/********************************************************/
void main()
{
struct student *head, stu;
long del_num;
printf("input records:\n");
head=creat(); //建立链表,返回头指针
print(head); //输出全部节点
printf("\ninput the deleted number:");
scanf("%ld",&del_num); //输入要删除的学号
head=del(head,del_num); //删除后链表的头地址
print(head); //输出全部节点
printf("\ninput the inserted number:"); //输入要插入的节点
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu); //插入一个节点,返回头节点地址
print(head); //输出全部节点
}
/*************************************************/
/*建立一个有3名学生数据的单项链表
/*************************************************/
struct student *creat(void) //定义函数,此函数带回一个指向链表头的指针
{
struct student *head;
struct student *p1, *p2;
n=0;
p1=(struct student *)malloc(LEN); //开辟一个新单元
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
p2 = NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/***********************************************/
/*输出链表
/***********************************************/
void print(struct student *head)
{
struct student *p;
printf("\nNow, These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld%5.1f\n",p->num,p->score);
p=p->next;
} while(p!=NULL);
}
/***************************************************/
/*删除动态链表中指定的结点
/***************************************************/
struct student *del(struct student *head, long num)
{
struct student *p1,*p2;
if(head==NULL) //如果是空表
{ printf("\nlist null!\n");
goto end;
}
p1=head;
while(num!=p1->num && p1->next!=NULL) //*p1指向的不是所要找的节点,并且后面还有节点
{ p2=p1; p1=p1->next; } //*p1后移一个节点
if(num==p1->num) //找到了
{
if(p1==head) head=p1->next; //若*p1指向的是首节点,把第二个节点地址赋予head
else p2->next=p1->next; //否则将下一个节点
printf("delete:%ld\n",num);
n=n-1;
}
else printf("%ld not been found!\n",num); //找不到该节点
end:
return(head);
}
/*************************************************************/
/*向链表中插入一个结点
/*************************************************************/
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0, *p1, *p2;
p1=head; //使p1指向第一个节点
p0=stud; //p0指向要插入的节点
if(head==NULL) //如果原来的链表是空链表
{
head=p0; //使p0指向的节点作为头节点
p0->next=NULL;
}
else
{ //如果p0->num > p1->num以及p1所指的不是表尾节点
while((p0->num > p1->num)&&(p1->next!=NULL))
{
p2=p1; //使p2指向刚才p1指向的节点
p1=p1->next; //p1后移一个节点
}
if(p0->num <= p1->num)
{
if(head==p1) head=p0; //插到原来第一个节点之前
else p2->next=p0; //插到p2指向的节点之后
p0->next=p1;
}
else
{
p1->next=p0; //插到最后的节点之后
p0->next=NULL;
}
}
n=n+1; //节点数加1
return(head);
}
// 1,用条件编译的方法实现以下功能:
// 输入一行电报文字,可以任选两种输出: 一为原文输出;一为字母变成其下以字母
// (即:'a'变成'b',....,'z'变成'a','A'变成'B',....,'Z'变成'A'),其他非字母字符不变。
// 用#define命令来控制是否要译成密码。
// 例如:#define CHANG 1 则译成密码。
// 若 #define CHANG 0 则不译成密码,按原码输出。
//
// #include<stdio.h>
// #define max 80
// #define change 1
//
// void main()
// {
// char str[max];
// int i;
// printf("input string:\n");
// gets(str);
//
// #if change
// {
// for (i=0;i<max;i++)
// {
// if (str[i]!='\0')
// if(str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')
// str[i]+=1;
// else if (str[i]=='z'||str[i]=='Z')
// str[i]-=25;
// }
// }
// #endif
// puts(str);
// }
// 例程2:例2.txt对候选人得票的统计程序。
//设计3个候选人,每次输入一个得票的候选人名字,要求最后输出个人得票结果。
// #include <stdio.h>
// #include <string.h>
// struct person
// {
// char name[20];
// int count;
// };leader[3]={{"Li",0},{"Zhang",0},{"Fun",0}};
//
// void main()
// {
// int i,j;
// char leader_name[20];
// for (i=1;i<=10;i++)
// {
// scanf("%s",leader_name);
// for(j=0;j<3;j++)
// if(strcmp(leader_name,leader[j].name)==0)
// leader[j].count++;
// }
// printf("\n");
// for (i=0;i<3;i++)
// {
// printf("%5s:%d\n",leader[i].name,leader[i].count);
// }
// }
// #include <stdio.h>
// #include <string.h>
// #define FORMAT "%d\n%s\n%f\n%f\n%f\n"
// struct student
// {int num;
// char name[20];
// float score[3];
// };
// void print(struct student stu);
// // void print(struct student *p);
//
// void main()
// {
// struct student stu;
// stu.num=12345;
// strcpy(stu.name,"Li Li");
// stu.score[0]=67.5;
// stu.score[1]=89;
// stu.score[2]=78.6;
// print(stu);
// /*print(&stu);*/
// }
//
// void print(struct student stu)
// /*void print(struct student *p)*/
// {
// printf(FORMAT,stu.num,stu.name, stu.score[0], stu.score[1],stu.score[2]);
// /*printf(FORMAT,p->num,p->name, p->score[0], p->score[1],p->score[2]);*/
// printf("\n");
// }
/***************************************/
/*文件包含,预定义,变量声明,函数声明*/
/***************************************/
#include <stdio.h>
#include <malloc.h>
//#define NULL 0 //令NULL代表0;用它表示“空地址”
#define LEN sizeof(struct student) //LEN代表struct student类型数据的长度,sizeof是求字节数运算符
struct student
{
long num;
float score;
struct student *next;
}; //学生信息包括学号和分数
int n; //n为全局变量,本文件模块中各函数均可使用它,代表节点的个数
struct student *creat(void);
void print(struct student *head);
struct student *del(struct student *head, long num);
struct student *insert(struct student *head,struct student *stud);
/******************************************************/
/*主函数:对链表的综合操作
/********************************************************/
void main()
{
struct student *head, stu;
long del_num;
printf("input records:\n");
head=creat(); //建立链表,返回头指针
print(head); //输出全部节点
printf("\ninput the deleted number:");
scanf("%ld",&del_num); //输入要删除的学号
head=del(head,del_num); //删除后链表的头地址
print(head); //输出全部节点
printf("\ninput the inserted number:"); //输入要插入的节点
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu); //插入一个节点,返回头节点地址
print(head); //输出全部节点
}
/*************************************************/
/*建立一个有3名学生数据的单项链表
/*************************************************/
struct student *creat(void) //定义函数,此函数带回一个指向链表头的指针
{
struct student *head;
struct student *p1, *p2;
n=0;
p1=(struct student *)malloc(LEN); //开辟一个新单元
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
p2 = NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/***********************************************/
/*输出链表
/***********************************************/
void print(struct student *head)
{
struct student *p;
printf("\nNow, These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld%5.1f\n",p->num,p->score);
p=p->next;
} while(p!=NULL);
}
/***************************************************/
/*删除动态链表中指定的结点
/***************************************************/
struct student *del(struct student *head, long num)
{
struct student *p1,*p2;
if(head==NULL) //如果是空表
{ printf("\nlist null!\n");
goto end;
}
p1=head;
while(num!=p1->num && p1->next!=NULL) //*p1指向的不是所要找的节点,并且后面还有节点
{ p2=p1; p1=p1->next; } //*p1后移一个节点
if(num==p1->num) //找到了
{
if(p1==head) head=p1->next; //若*p1指向的是首节点,把第二个节点地址赋予head
else p2->next=p1->next; //否则将下一个节点
printf("delete:%ld\n",num);
n=n-1;
}
else printf("%ld not been found!\n",num); //找不到该节点
end:
return(head);
}
/*************************************************************/
/*向链表中插入一个结点
/*************************************************************/
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0, *p1, *p2;
p1=head; //使p1指向第一个节点
p0=stud; //p0指向要插入的节点
if(head==NULL) //如果原来的链表是空链表
{
head=p0; //使p0指向的节点作为头节点
p0->next=NULL;
}
else
{ //如果p0->num > p1->num以及p1所指的不是表尾节点
while((p0->num > p1->num)&&(p1->next!=NULL))
{
p2=p1; //使p2指向刚才p1指向的节点
p1=p1->next; //p1后移一个节点
}
if(p0->num <= p1->num)
{
if(head==p1) head=p0; //插到原来第一个节点之前
else p2->next=p0; //插到p2指向的节点之后
p0->next=p1;
}
else
{
p1->next=p0; //插到最后的节点之后
p0->next=NULL;
}
}
n=n+1; //节点数加1
return(head);
}