顺序表的通讯录管理
对与一个学生来说,存储信息【姓名、年龄、分数,手机号】
功能1:在堆区申请空间,通过尾插添加学生信息
功能2:输入姓名,删除该学生信息
功能3∶对学生姓名排序【直接插入排序】
功能4:对年龄按升序排序【快速排序】,并使用折半查找,判断是否存在key年龄的学生选做【不会做,就把哈希表基础练习一下】
功能5:使用哈希存储以手机号为关键字,实现存储自己定义哈希函数【使用除留取余法】,【链地址法解决哈希冲突】,输入一个手机号查找其他信息}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "head.h"
#define MAXSIZE 5
//
int main(void)
{
//===开辟空间===
Seqlist *stu=Create();
if(stu==NULL)
return -1;
//===输入信息===
InsertData(stu,5);
//===遍历顺序表===
SeqlistShow(stu);
//===输入姓名删除信息===
char name[20];
printf("请输入要删除的学生姓名:")
scanf("%s",name);
NameDelete(stu,name);
//===学生姓名排序===
UpNameSort(stu);
printf("学生姓名排序\n");
SeqlistShow(stu);
//===对年龄按升序排序===
QuickUpYearSort(stu,0,stu->len-1);
printf("学生年龄排序\n");
SeqlistShow(stu);
//===折半排序===
//===判断是否存在key===
int key;
printf("输入要查找的key:");
scanf("%d",&key);
int flag=SearchKey(stu,0,stu->len,key);
if(flag==-1)
printf("查找失败\n");
else
printf("%d出现在下标%d\n",key,flag);
//===哈希查找===
Node Hash[N];
for(int i=0;i<N;i++)
{
Hash[i]=NULL;
}
for(int j=0;j<stu->len;j++)
{
InsertHash(stu->data[j],Hash);
}
char num[11];
printf("输入电话号码:");
scanf("%s",num);
SearchHash(Hash,num);
return 0;
}
head.c
#include "head.h"
#include <stdio.h>
#include <string.h>
Seqlist *Create()
{
Seqlist *stu=(Seqlist *)malloc(sizeof(Seqlist));
if(stu==NULL)
return NULL;
stu->len=0;
return stu;
}
//判断顺序表空,成立返回-1,不满返回0;
int SeqlistEmpty(Seqlist *s)
{
if(s->len==0)
{
return -1;
}
return 0;
}
//输入数据
void InsertData(Seqlist *S,int num)
{
for(int i=0;i<num;i++)
{
printf("请输入学生姓名\n");
scanf("%s",S->data[i].name);
printf("请输入学生年龄\n");
scanf("%d",&S->data[i].year);
printf("请输入学生分数\n");
scanf("%f",&S->data[i].score);
printf("请输入学生手机号\n");
scanf("%s",S->data[i].number);
S->len++;
}
}
//遍历输出
void SeqlistShow(Seqlist *stu)
{
for(int i=0;i<stu->len;i++)
{
printf("第%d位学生姓名:%s\n",i+1,stu->data[i].name);
printf("第%d位学生年龄: %d\n",i+1,stu->data[i].year);
printf("第%d位学生分数: %.2f\n",i+1,stu->data[i].score);
printf("第%d位学生手机号 %s\n",i+1,stu->data[i].number);
}
printf("\n");
}
//根据名字删除
void NameDelete(Seqlist *stu,char name[])
{
//1.判断顺序表是否为空
//2.判断下标是否合法
if(SeqlistEmpty(stu))
{
printf("删除失败\n");
return ;
}
int sub;//记录要删除学生的下标
for(int i=0;i<stu->len;i++)
{
if(strcmp(stu->data[i].name,name)==0)
{
sub=i;
}
}
for(int j=sub;j<stu->len;j++)
{
strcpy(stu->data[j].name,stu->data[j+1].name);
stu->data[j].year=stu->data[j+1].year;
stu->data[j].score=stu->data[j+1].score;
strcpy(stu->data[j].number,stu->data[j+1].number);
}
stu->len--;
SeqlistShow(stu);
}
//根据名字排序
void UpNameSort(Seqlist *stu)
{
student q;
for(int i=0;i<stu->len;i++)
{
int count=0;
for(int j=0;j<stu->len-i;j++)
{
if(strcmp(stu->data[j].name,stu->data[j+i].name)>0)
{
q=stu->data[j];
stu->data[j]=stu->data[j+1];
stu->data[j+1]=q;
count++;
}
if(count==0)
{
return;
}
}
}
}
//按年龄升序
//用快速排序
void QuickUpYearSort(Seqlist *stu,int low,int high)
{
if(low<high)
{
//中间值
int mid=OneSort(stu,low,high);
//左递归
QuickUpYearSort(stu,low,mid-1);
//右递归
QuickUpYearSort(stu,mid+1,high);
}
}
//排序
int OneSort(Seqlist *stu,int low,int high)
{
//定义一个student结构体,将数据存入studengt结构体内
student q=stu->data[low];
//将year的值存入num
int num=stu->data[low].year;
//当low小于high
while(low<high)
{
while(low<high&&num<=stu->data[high].year)
{
high--;
}
stu->data[low]=stu->data[high];
while(low<high&&num>=stu->data[low].year)
{
low++;
}
stu->data[high]=stu->data[low];
}
stu->data[low]=q;
return low;
}
//折半排序
int SearchKey(Seqlist *stu,int low,int high,int key)
{
int mid;
while(low<high)
{
mid=(low+high)/2;
if(stu->data[mid].year==key)
{
return mid;
}
else if(stu->data[mid].year>key)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return -1;
}
//哈希排序
void InsertHash(student stu,Node Hash[])
{
char str[11];
strcpy(str,stu.number);
int num=atoi(str);
int sub=num%N;//将数据存入sub下标的哈希表内
Node p=(Node)malloc(sizeof(struct node));
if(p==NULL)
return;
p->data=stu;
p->next=NULL;
p->next=Hash[sub];
Hash[sub]=p;
}
//哈希查找
//查找成功输出,查找失败返回;
void SearchHash(Node Hash[],char *num)
{
char str[11];
strcpy(str,num);
int num1=atoi(str);
int sub=num1%N;
Node p=Hash[sub];
while(p)
{
p=p->next;
char str1[11];
strcpy(str1,p->data.number);
if(strcmp(str1,num)==0);
{
printf("学生姓名:%s\n",p->data.name);
printf("学生年龄: %d\n",p->data.year);
printf("学生分数: %.2f\n",p->data.score);
printf("学生手机号 %s\n",p->data.number);
}
}
}
head.h
#ifndef __HEAD_H_
#define __HEAD_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 5
#define N 6
//定义学生信息结构体
typedef struct
{
char name[20];
int year;
float score;
char number[11];
}student;
//定义顺序表
typedef struct
{
int len;
student data[MAXSIZE];
}Seqlist;
//哈希表
typedef struct node
{
student data;
struct node *next;
}*Node;
Seqlist *Create();
int SeqlistEmpty(Seqlist *s);
void InsertData(Seqlist *S,int num);
void SeqlistShow(Seqlist *stu);
void NameDelete(Seqlist *stu,char name[]);
void UpNameSort(Seqlist *stu);
void QuickUpYearSort(Seqlist *stu,int low,int high);
int OneSort(Seqlist *stu,int low,int high);
int SearchKey(Seqlist *stu,int low,int high,int key);
void InsertHash(student stu,Node Hash[]);
void SearchHash(Node Hash[],char *num);
#endif