顺序表的建立及使用
顺序表: 就所谓的数组式操作
编程实现顺序存储结构中的基本操作的实现(电话本的建立、插入、删除、修改、逆置、查找、输出)
整个操作过于简陋, 只对上述功能做叙述, 采用结构体数组实现, 本打算用class操作, 可因为某种原因就搁置了………..
代码中的注释部分是用于测试数据, 电话本只存有number, name
//size全局变量表示电话本当前大小不包含0位置,0号位置作为中间变量处理, 重新申请中间变量太麻烦了就省略
//
#include<cstdio>
#include<cstring>
#define MAXSIZE 100
#define null 0int size=0; //表示当前电话本中有几个联系人的信息typedef struct{char name[10];char number[12];
}telephone;telephone t[MAXSIZE];//创建结构体数组的时候,可采用new,此处对此操作省略
void create();
int find(int lo,int hi,telephone t[],char* target_value);
void insert(telephone t[],int size);
void display(telephone t[],int size);
void convert(telephone t[],int size); void create(){int t_size;printf("输入需要创建的电话本大小");scanf("%d",&t_size);insert(t,t_size+1);
// printf("%d\n",size);
}int find(int lo,int hi,telephone t[],char target_value[]){//使用二分查找if(lo>=hi)return null;//递归基 if(strcmp(target_value,t[(lo+hi)/2].name)==0) //递归基 return (lo+hi)/2;if(strcmp(target_value,t[(lo+hi)/2].name)>0)return find((lo+hi)/2+1,hi,t,target_value);else if(strcmp(target_value,t[(lo+hi)/2].name)<0)return find(lo,(lo+hi)/2,t,target_value);}
//在插入的过程中使用插入排序,以便于find操作
void insert(telephone t[],int t_size){int i,j;for(i=size+1;i<t_size;i++){printf("输入姓名,电话\n"); scanf("%s%s",t[i].name,t[i].number);//t[0]为哨兵,作为每次插入的中间变量strcpy(t[0].name,t[i].name);//哨兵元素每次表示要插入的元素(该元素先插到数组的尾部)strcpy(t[0].number,t[i].number);j=i-1;//此时的j表示待插入元素的前一个元素 //待插入的元素比哨兵元素小,进行移动覆盖,查找到比哨兵元素小的值时//将哨兵元素放到此位置while((strcmp(t[j].name,t[0].name)>0)){strcpy(t[j+1].name,t[j].name);strcpy(t[j+1].number,t[j].number);j--;}//此处的j指向刚插入的元素的位置 strcpy(t[j+1].name,t[0].name);//哨兵放置 strcpy(t[j+1].number,t[0].number); ++size; //每插入一个人的信息,size增加一次
// printf("当前i=%d insert后size=%d\n",i,size); }
// printf("insert后size=%d\n",size); return;
}int remove(telephone t[],char* target_value,int t_size){
//在删除的过程中出现,将局部变量的值的名称设置了和全局变量一致,出现size无法减少的情况 int i; int location=find(0,size,t,target_value);
// printf("find=%d\n",location); if(location==0) return null;for(i=location;i<=size;i++){//将要删除的元素进行覆盖处理,令i=size,可实现将最后一个元素变为"\0" strcpy(t[i].name,t[i+1].name);strcpy(t[i].number,t[i+1].number);}
// strcpy(t[i].name,"\0");
// strcpy(t[i].number,"\0");//将要删除的元素的name.number置为空串
// if(location==size){//判断是否删除最后一个元素,若是则需要进行对0号元素更新,防止insert出现错误// strcpy(t[0].name,t[i-1].name);// strcpy(t[0].number,t[i-1].number);//}size--;return location;
// printf("remove后size=%d\n",size);
}void display(telephone t[],int size){char c;
//可在display中引入倒置, 此处不做叙述for(int i=1;i<=size;i++)printf("%d=姓名:%s, 号码:%s\n",i,t[i].name,t[i].number);return;
}void convert(telephone t[],int size){//倒置处理for(int i=1;i<=size/2;i++){strcpy(t[0].name,t[i].name);strcpy(t[i].name,t[size-i+1].name);strcpy(t[size-i+1].name,t[0].name);//name做倒置strcpy(t[0].number,t[i].number);strcpy(t[i].number,t[size-i+1].number);strcpy(t[size-i+1].number,t[0].number);//number倒置}
}int main(){int i,key=1;char *c; //用于插入, 删除时临时的中间变量, 也可使用数组while(key){ printf("1-创建电话本\n 2-插入账户信息\n 3-查找号码\n 4-删除号码\n 5-查看所有信息\n 6-将电话本倒置\n 7-关闭\n");scanf("%d",&i);switch(i){case 1:create();fflush(stdin);break;if(create_flag==1){ case 2:insert(t,size+2);fflush(stdin);break;//当执行单步插入的时候形参要修改为在size的基础上扩大1,但是t[0]并不存数据,所以size+2处理 case 3:printf("输入查找的元素,0表示查找失败\n 查找的姓名:");scanf("%s",c);printf("所在的位置是%d\n",find(1,size,t,c));fflush(stdin);break;case 4:printf("输入要删除的元素,0表示删除失败\n 删除的姓名:");scanf("%s",c);printf("删除元素的位置是%d\n",remove(t,c,size));fflush(stdin);break;case 5:display(t,size);fflush(stdin);break;case 6:convert(t,size);fflush(stdin);break;}else printf("未创建电话本,不可删除,查找,展示,倒置操作"); case 7: key=0;break; }} return 0;
}
在写代码的过程中出现的问题是:
- 二分查找的时候没有做尾递归处理,导致返回值出现问题, 不能正确的返回出是否查找到当前元素的位置, 处理find((lo+hi)/2+1,hi,t,target_value); 写成做 (lo+hi)/2; 导致在递归查找的过程中出现死递归, 不能返回出来
- 在remove中将局部变量的名称与全局变量的名称写成一样的(size), 导致size–出错; 真是愚蠢
- 进行代码调试的时候只是从逻辑的角度分析问题, 而没有写printf语句观察程序的运行过程, 细心考虑每个变量的变化情况, 造成无法准确地定位错误的位置