#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string.h>
#include<stdlib.h>
#define LEN sizeof(struct student) //*定义结构体长度
typedef struct student
{
char name[20];
int number;
struct student *next;
}STU;
{
char name[20];
int number;
struct student *next;
}STU;
STU * init_student() //*初始化操作
{
STU *head;
head=(STU *)malloc(LEN);//*定义head地址长度
if(head==NULL)
{
printf("ERROR\n");
}
head->next=NULL; //*使指向下一个为空
printf("\n");
printf(">>>>INIT IS OVER!<<<<\n"); //*初始化完成
printf("\n");
return head;
}
void print_student(STU *head)//*显示出所有学生信息
{
STU *p;
p=head->next;
if(p==NULL)
printf("PLEASE CREATE YOUR student!\n");
while(p!=NULL)
{
printf("%s %d\n",p->name,p->number);
p=p->next;//*循环指向下一个,全部输出地址,显示所有值
}
}
STU * create_student(STU *head) //*新建通讯录
{
STU *p;
head = (STU *)malloc(LEN);
//head->next = NULL; /* 先建立一个带头结点的空的单链表 */
p=head->next;
STU *p;
head = (STU *)malloc(LEN);
//head->next = NULL; /* 先建立一个带头结点的空的单链表 */
p=head->next;
while(1)
{
p = (STU *)malloc(LEN); /* 生成新结点 */
{
p = (STU *)malloc(LEN); /* 生成新结点 */
printf(">> 姓名 :");
scanf(" %s",& p->name); //*输入姓名信息
if(strcmp(p->name,"$")==0)//*判断结束条件“$”
break;
printf(">> 学号 :");
scanf("%d",& p->number); //*输入学号信息
p->next = head->next; //*已经定义head->next为NULL,即先让p->next指向NULL
head->next = p;/* 插入到表头 *///*将head->next指向NULL的箭头断掉,改为指向p,在while循环中插入新的值
}
printf("************************************\n");
return head;
}
STU * insert_student(STU *head,int i) //*新增学生信息
{
char a[20]; int b;//*定义存放姓名学号的数据类型
STU *p,*s;
int j=1;
p = head;
printf(">>增加 姓名 :");
scanf("%s",&a);
printf(">>增加 学号 :");
scanf("%d",&b);
scanf("%s",&a);
printf(">>增加 学号 :");
scanf("%d",&b);
while (p&& j< i) /* 寻找第i个结点 */
{
p = p->next;
++j;
}
if (!p || j > i)
printf("没有查询到结果!"); /* 第i个元素不存在 */
s = (STU *)malloc(LEN); /* 生成新结点*/
strcpy(s->name,a);
s->number=b;
s->next = p->next; /* 将p的后继结点赋值给s的后继 */
p->next = s; /* 将s赋值给p的后继 */
printf("************************************\n");
return head;
strcpy(s->name,a);
s->number=b;
s->next = p->next; /* 将p的后继结点赋值给s的后继 */
p->next = s; /* 将s赋值给p的后继 */
printf("************************************\n");
return head;
}
STU *sort_student(STU *head) //*重新进行排序
{
STU *p;
char a[20];
int i,j,temp;
{
STU *p;
char a[20];
int i,j,temp;
p=head->next;
for(j=0;j<3;j++)
{
p=head->next;
for(i=0;i<3-j;i++)
{
if(strcmp(p->name,p->next->name)>0)
{
temp=p->number;
p->number=p->next->number;
p->next->number=temp;
strcpy(a,p->name);
strcpy(p->name,p->next->name);
strcpy(p->next->name,a);
}
p=p->next;
}
}
return head;
}
STU *delete_student(STU *head) //*删除链表中指定信息
{
STU *present,*after;
char s[20];
printf(">>删除 姓名 :");
scanf("%s",&s);
if(head==NULL)
{
printf("不存在!!!\n");
return head;
}
if(strcmp(head->name,s)==0)
{
present=head;
head=head->next;
free(present);
}
else
{
present=head;
after=head->next;
while((after!=NULL)&&(strcmp(after->name,s)!=0))
{
present=after;
after=after->next;
}
if(after!=NULL)
{
present->next=after->next;
free(after);
}
else
printf("没有查询到结果!!!\n");
}
return head;
}
void find_student(STU *head) //*查询学生信息
{
STU *p,*q;
char c[20];
printf("查询 姓名 :");
scanf(" %s",&c);
p=head;
while(1)
{
if(strcmp(p->name,c)==0)
{
printf("姓名 学号\n");
printf("************************************\n");
printf("%s %d\n",p->name,p->number);
printf("\n");
}
p=p->next;
if(p==NULL)
{
printf("************************************\n");
printf("查询结束!\n");
break;
}
}
}
void modify_student(STU *head) //*修改信息
{
STU *p,*q;
char c[20];
int g;
printf("修改 姓名 :");
scanf(" %s",&c);
p=head;
while(1)
{
if(strcmp(p->name,c)==0)
{
printf("修改 学号 :");
scanf(" %d",&g);
p->number=g;
printf("姓名 学号\n");
printf("************************************\n");
printf("%s %d\n",p->name,p->number);
break;
}
p=p->next;
if(p==NULL)
{
printf("************************************\n");
printf("没有查询到结果!\n");
break;
}
}
}
void interface(void)
{
printf("********************************您好!********************************\n");
printf("\n");
printf("****************欢迎来到154班通讯录!*************************************\n");
printf("** KEY 0<< >> INIT (初始化) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("****************欢迎来到154班通讯录!********************************\n");
printf("\n");
printf("********************************您好!********************************\n");
}
{
printf("********************************您好!********************************\n");
printf("\n");
printf("****************欢迎来到154班通讯录!*************************************\n");
printf("** KEY 0<< >> INIT (初始化) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("****************欢迎来到154班通讯录!********************************\n");
printf("\n");
printf("********************************您好!********************************\n");
}
int main()
{
STU *head;
int KEY;
interface();
while(1)
{
printf(">>输入您的选择<<");
scanf("%d",&KEY);
switch(KEY)
{
case 0: head=init_student();break;
case 1: head=create_student(head);
printf("姓名 学号\n");
print_student(head);
printf("\n");
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (初始化) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 2: head=insert_student(head,1);
printf("姓名 学号\n");
print_student(head);
printf("\n");
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (输入) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 3: find_student(head);
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (输入) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 4: head=sort_student(head);
printf("姓名 学号\n");
print_student(head);
printf("\n");
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (输入) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 5: head=delete_student(head);
print_student(head);
printf("\n");
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (输入) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 6: modify_student(head);
printf("\n");
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (输入) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 7: print_student(head);
printf("\n");
printf("**************************************************************\n");
printf("** 请继续选择您的操作 **\n");
printf("** KEY 0<< >> INIT (输入) **\n");
printf("** KEY 1<< >> NEW (创建) **\n");
printf("** KEY 2<< >> ADD (新增) **\n");
printf("** KEY 3<< >> FIND (查询) **\n");
printf("** KEY 4<< >> SORT (排序) **\n");
printf("** KEY 5<< >> DELETE (删除) **\n");
printf("** KEY 6<< >> MODIFY (修改) **\n");
printf("** KEY 7<< >> PRINT (显示全部) **\n");
printf("** KEY 8<< >> EXIT (退出) **\n");
printf("**************************************************************\n");
break;
case 8: exit(0);
break;
default: printf("错误的选择!!!\n");
break;
}
}
return 0;
break;
default: printf("错误的选择!!!\n");
break;
}
}
return 0;
}