#include <stdio.h>
#include <stdlib.h>
struct Phone{
char name[32];
long int number;
struct Phone *next;
};
void welcome(void);//打印欢迎和使用说明
void getInput(struct Phone **phone);//录入数据
void insert(struct Phone **head);//插入新的联系人
struct Phone *searchEvent(struct Phone *phonebook, char *name);//查找已有的联系人
void change(struct Phone **head);//更改
void delete(struct Phone **head, char *name);//删除
void print(struct Phone *head);//打印
void release(struct Phone *head);//释放内存
void welcome(void){//打印欢迎和使用说明
printf("| 欢迎使用通讯录管理程序 |\n");
printf("|--- 1:插入新的联系人 ---|\n");
printf("|--- 2:查找已有联系人 ---|\n");
printf("|--- 3:更改已有联系人 ---|\n");
printf("|--- 4:删除已有联系人 ---|\n");
printf("|--- 5:显示当前通讯录 ---|\n");
printf("|--- 6:退出通讯录程序 ---|\n");
printf("|--------------------|\n");
}
void getInput(struct Phone **phone){//录入数据
struct Phone *temp = *phone;
printf("请输入姓名:");
scanf("%s", temp->name);
printf("请输入电话号码:");
scanf("%ld", &temp->number);
}
void insert(struct Phone **head){//插入新的联系人
struct Phone *before;//前
struct Phone *now;//现
struct Phone *new;//后
now = *head;
before = NULL;
new = (struct Phone *)malloc(sizeof(struct Phone));//给新的链表分配内存
if(new == NULL){
printf("内存分配失败了。\n");
exit(1);
}
getInput(&new);
while (now != NULL && now->number < new->number){//现在的链表的number值要不小于i
before = now;
now = now->next;
}
if (before == NULL){//*head是空的
*head = new;//把新生成的链表放在第一个
}
else{
before->next = new;//前一个的next要指向现在的
}
}
struct Phone *searchEvent(struct Phone *phonebook, char *name){//查找已有的联系人
struct Phone *phone;
phone = phonebook;
while (phone != NULL){
if (!strcmp(phone->name, name)){
break;
}
phone = phone->next;
}
return phone;
}
void change(struct Phone **head){//更改
struct Phone *before;//前
struct Phone *now;//现
struct Phone *new;//后
char name[32];
long int number;
printf("请输入要更改的联系人:");
scanf("%s", name);
now = *head;
while (now != NULL){
if (!strcmp(now->name, name)){
break;
}
now = now->next;
}
if(now == NULL){
printf("抱歉,没有找到。\n");
}
else{
free(now);
now = *head;
before = NULL;
new = (struct Phone *)malloc(sizeof(struct Phone));//给新的链表分配内存
if(new == NULL){
printf("内存分配失败了。\n");
exit(1);
}
new->name = name;
printf("请输入电话号码:");
scanf("%ld", &new->number);
while (now != NULL && now->number < new->number){//现在的链表的number值要不小于输入的
before = now;
now = now->next;
}
if (before == NULL){//*head是空的
*head = new;//把新生成的链表放在第一个
}
else{
before->next = new;//前一个的next要指向现在的
}
}
}
void delete(struct Phone **head, char *name){//删除
struct Phone *before;//前
struct Phone *now;//现
now = *head;
before = NULL;
while (now != NULL && strcmp(now->name, name)){//现在的链表的name要相等
before = now;
now = now->next;
}
if (now == NULL){//没有相等的或者head进来就是NULL
printf("找不到匹配的节点\n");
return ;
}
else{
if (before == NULL){//第一个就是要删除的节点
*head = now->next;
}
else{
before->next = now->next;
}
free(now);
}
}
void print(struct Phone *head){//打印
struct Phone *now;
now = head;
while (now != NULL){
printf("姓名:%s ", now->name);
printf("电话号码:%ld\n", &now->number);
now = now->next;
}
putchar('\n');
}
void release(struct Phone *head){//释放内存
struct Phone *temp;
while (head != NULL){
temp = head;
head = head->next;
free(temp);
}
}
int main(void){
struct Phone *head;
struct Phone *temp;
char name[32];
int i;
welcome();
while (1){
printf("\n请输入指令代码:");
scanf("%d", &i);
switch (i){
case 1: insert(&head); break;
case 2: printf("请输入联系人:");
scanf("%s", name);
temp = searchEvent(head, name);
printf("电话号码:temp->number");
break;
case 3: change(&head); break;
case 4: printf("请输入联系人:");
scanf("%s", name);
delete(&head, name); break;
case 5: print(head); break;
case 6: release(head);exit(1);
}
}
return 0;
}
本帖最后由 jhq999 于 2022-12-7 22:20 编辑
静态数组指针不能被赋值 //new->name = name;
int i=0;
while((new->name[i]=name[i++]));
或者 int i=0;
while(i<32)new->name[i]=name[i++];
|