#2
林月儿2020-03-04 19:46
|
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#define MaxSize 50
typedef struct {
char name[20];
char tel[15];
} ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
} SqList;
void lnitList(SqList *&L) {
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
}
void DispList(SqList*L) {
int i;
printf(" NO NAME TEL \n");
for(i=0; i<L->length; i++) {
printf(" %d %s %s",i+1,L->data[i].name,L->data[i].tel);
printf("\n");
//fflush(stdin);
}
printf("目前联系人总数: %d\n",L->length);
printf("\n");
}
bool Listlnsert(SqList*&L,int i,ElemType e) {
int j;
if(e.name=="\0")return false;
if(i<1||i>L->length+1)
return false;
i--;
for(j=L->length;j>i; j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
//printf("length=%d\n\n",L->length);
return true;
}
bool ListDelete(SqList*&L,int i,ElemType &e) {
int j;
if(i<1 || i>L->length)
return false;
i--;
e=L->data[i];
for(j=i; j<L->length-1; j++);
{
L->data[j]=L->data[j+1];
}
L->length--;
printf("length=%d\n\n",L->length);
return true;
}
void menu1() {
printf(" --------------\n");
printf(" 手机通讯录的应用(顺序表)\n");
printf(" --------------\n");
printf(" 1.建立通讯录\n");
printf(" 2.增加联系人信息\n");
printf(" 3.显示联系人信息\n");
printf(" 4.删除联系人信息\n");
printf(" 5.查找联系人信息\n");
printf(" 5.已退出!!!\n");
printf(" ------------------------\n\n");
}
int LocateElem(SqList*L,ElemType e) {
int i=0;
while(i<L->length && strcmp(L->data[i].name,e.name)!=0)
i++;
if(i>=L->length)
return 0;
else {
puts(L->data[i].name);
puts(L->data[i].tel);
//printf("%s,%s\n",L->data[i].name,L->data[i].tel);
return(i+1);
}
}
void main() {
SqList*L;
int flag=1;
int j;
ElemType s[4]= {"陆 毅","13975053309","张珊珊","13975053389","邓 超","13989003389","张 本","1297809987"};
ElemType e;
menu1();
printf(" (1)初始化顺序表L\n");
lnitList(L);
printf( " (2)依次插入元素\n");
for(int i=0; i<4; i++)
Listlnsert(L,i+1,s[i]);
DispList(L);
while(flag==1) {
printf("请选择: ");
scanf("%d",&j);
switch(j) {
case 2:
printf("请输入联系人姓名和电话号码: ");
scanf("%s%s",e.name,e.tel);
printf("请输入插入数据的位置: ");
scanf("%d",&i);
fflush(stdin);
printf("\n");
Listlnsert(L,i,e);
break;
case 3:
DispList(L);
break;
case 4:
printf("请输入删除数据的位置: ");
scanf("%d",&i);
ListDelete(L,i,e);
break;
case 5:
printf("请输入查找的联系人姓名: ");
scanf("%s",&e.name);
LocateElem(L,e);
break;
case 6:
flag=0;
printf("\n 不再输入记录,退出!!\n\n");
break;
}
}
}