2023/2/7 顺序表的通讯录管理
对与一个学生来说,存储信息[姓名、年龄、分数,手机号]
功能1:在堆区申请空间,通过尾插添加学生信息
功能2:输入姓名,删除该学生信息
功能3:对学生姓名排序[直接插入排序]
功能4:对年龄按升序排序[快速排序],并使用折半查找, 判断是否存在key年龄的学生
选做[不会做,就把哈希表基础练习-下]
{功能5:使用哈希存储以手机号为关键字,实现存储自己定义哈希函数[使用除留取余
法],[链地址法解决哈希冲突] ,输入-个手机号查找其他信息}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 20
typedef struct
{
char name[20];
int age;
int score;
long number;
}Student;
typedef struct
{
Student data[MAXSIZE];
int len;
}Seqlist;
typedef struct node
{
Student data;
struct node *next;
}*Node;
Seqlist *CreateSpace();
void InsertRear(Seqlist *s,Student e);
void DeleteBySub(Seqlist *s,int sub);
int SearchByName(Seqlist *s,char *p);
void DeleteByName(Seqlist *s,char *p);
void SeqlistShow(Seqlist *s);
void InsertSortByName(Seqlist *s);
int OneSort(Seqlist *s,int low,int high);
void QuickSortByAge(Seqlist *s,int low,int high);
int HalfSearch(Seqlist *s,int low,int high,int key);
void HashSearchByNumber(Seqlist *s);
void Init(Node hash[],int n);
void InsertHash(Seqlist *s,int i,int n,Node hash[]);
void HashSearch(Node hash[],long number,int n);
#endif
test.c
#include "head.h"
Seqlist *CreateSpace()
{
Seqlist *s=(Seqlist *)malloc(sizeof(Seqlist));
if(s==NULL)
{
return NULL;
}
s->len=0;
return s;
}
void InsertRear(Seqlist *s,Student e)
{
if(s==NULL)
{
return;
}
s->data[s->len++]=e;
}
void DeleteBySub(Seqlist *s,int sub)
{
if(s==NULL||sub<0||sub>=s->len)
{
return;
}
for(int i=sub;i<s->len-1;i++)
{
s->data[i]=s->data[i+1];
}
s->len--;
}
int SearchByName(Seqlist *s,char *p)
{
if(s==NULL)
{
return -1;
}
for(int i=0;i<s->len;i++)
{
if(strcmp(s->data[i].name,p)==0)
{
return i;
}
}
return -1;
}
void DeleteByName(Seqlist *s,char *p)
{
int sub=SearchByName(s,p);
if(sub==-1)
{
printf("删除元素不存在!\n");
return;
}
DeleteBySub(s,sub);
}
void SeqlistShow(Seqlist *s)
{
for(int i=0;i<s->len;i++)
{
printf("姓名:%s 年龄:%d 分数:%d 手机号:%ld\n",s->data[i].name,s->data[i].age,s->data[i].score,s->data[i].number);
}
printf("\n");
}
void InsertSortByName(Seqlist *s)
{
int i,j;
Student temp;
for(i=1;i<s->len;i++)
{
temp=s->data[i];
for(j=i-1;j>=0&&strcmp(temp.name,s->data[j].name)<0;j--)
{
s->data[j+1]=s->data[j];
}
s->data[j+1]=temp;
}
}
int OneSort(Seqlist *s,int low,int high)
{
Student key=s->data[low];
while(low<high)
{
while(s->data[high].age>=key.age&&low<high)
{
high--;
}
s->data[low]=s->data[high];
while(s->data[low].age<=key.age&&low<high)
{
low++;
}
s->data[high]=s->data[low];
}
s->data[low]=key;
return low;
}
void QuickSortByAge(Seqlist *s,int low ,int high)
{
Seqlist *p=s;
if(low>=high)
{
return;
}
int mid=OneSort(p,low,high);
QuickSortByAge(p,low,mid-1);
QuickSortByAge(p,mid+1,high);
}
int HalfSearch(Seqlist *s,int low,int high,int key)
{
Seqlist *p=s;
int mid=(low+high)/2;
while(low<high)
{
if(key==p->data[mid].age)
{
return 0;
}else if(key<p->data[mid].age)
{
high=mid-1;
}else if(key>p->data[mid].age)
{
low=mid+1;
}
}
return -1;
}
void Init(Node hash[],int n)
{
for(int i=0;i<n;i++)
{
hash[i]=NULL;
}
}
void InsertHash(Seqlist *s,int i,int n,Node hash[])
{
long sub=s->data[i].number%n;
Node p=(Node)malloc(sizeof(struct node));
if(p==NULL)
{
return;
}
p->data=s->data[i];
p->next=NULL;
p->next=hash[sub];
hash[sub]=p;
}
void HashSearch(Node hash[],long number,int n)
{
int sub=number%n;
Node p=hash[sub];
while(p)
{
if(number==p->data.number)
{
printf("姓名:%s 年龄:%d 分数:%d 手机号:%ld\n",p->data.name,p->data.age,p->data.score,p->data.number);
return;
}
}
}
main,.c
#include "head.h"
int main(int argc, const char *argv[])
{
Seqlist *s=CreateSpace();
if(s==NULL)
{
return -1;
}
int n;
int key;
char str[20]="";
Student e;
printf("输入学生个数:");
scanf("%d",&n);
int N;
for(int i=n*4/3;i>=2;i--)
{
int flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
N=i;
break;
}
}
Node hash[N];
Init(hash,N);
for(int i=0;i<n;i++)
{
printf("输入学生姓名:");
scanf("%s",e.name);
printf("输入学生年龄:");
scanf("%d",&e.age);
printf("输入学生分数:");
scanf("%d",&e.score);
printf("输入学生手机号:");
scanf("%ld",&e.number);
InsertRear(s,e);
}
SeqlistShow(s);
/*
printf("输入删除学生的姓名:");
scanf("%s",str);
DeleteByName(s,str);
SeqlistShow(s);
printf("对学生姓名的排序结果为:\n");
InsertSortByName(s);
SeqlistShow(s);
*/
printf("对学生年龄的升序排序结果为:\n");
QuickSortByAge(s,0,n-1);
SeqlistShow(s);
for(int i=0;i<n;i++)
{
InsertHash(s,i,N,hash);
}
printf("输入查找的年龄:");
scanf("%d",&key);
int flag=HalfSearch(s,0,s->len-1,key);
if(flag==-1)
{
printf("查找年龄不存在!\n");
}else{
printf("查找年龄存在!\n");
}
long number;
printf("输入查找的手机号:");
scanf("%ld",&number);
HashSearch(hash,number,N);
return 0;
}