C语言版本,具有增,删,查,改功能
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#define T 1
#define F -1
#define F -1
struct Person
{
char ID[10];
char NAME[4];
char NUM[20];
struct Person* next;
};
{
char ID[10];
char NAME[4];
char NUM[20];
struct Person* next;
};
int init(struct Person** head);
int insert(struct Person* head);
int delete_id(struct Person* head);
int delete_name(struct Person* head);
int delete_num(struct Person* head);
int update(struct Person* head);
int query_id(struct Person* head);
int query_name(struct Person* head);
int query_num(struct Person* head);
void print(struct Person* head);
int insert(struct Person* head);
int delete_id(struct Person* head);
int delete_name(struct Person* head);
int delete_num(struct Person* head);
int update(struct Person* head);
int query_id(struct Person* head);
int query_name(struct Person* head);
int query_num(struct Person* head);
void print(struct Person* head);
int main()
{
struct Person* head = NULL;
init(&head); //初始化
{
struct Person* head = NULL;
init(&head); //初始化
print(head);
char a[6];
while (strcmp(a, "finish") != 0)
{
printf("You want to (insert/delete/query/update/finish): ");
scanf("%s", a);
{
printf("You want to (insert/delete/query/update/finish): ");
scanf("%s", a);
if (strcmp(a, "insert") == 0)
{
insert(head);
print(head);
}
if (strcmp(a, "delete") == 0)
{
char b[4];
{
insert(head);
print(head);
}
if (strcmp(a, "delete") == 0)
{
char b[4];
printf("The way you want to delete (id/name/num): ");
scanf("%s", b);
scanf("%s", b);
if (strcmp(b, "id") == 0)
{
delete_id(head);
print(head);
}
else if (strcmp(b, "name") == 0)
{
delete_name(head);
print(head);
}
else if (strcmp(b, "num") == 0)
{
delete_num(head);
print(head);
}
else
{
printf("out of range\n");
}
}
if (strcmp(a, "query") == 0)
{
char b[4];
{
delete_id(head);
print(head);
}
else if (strcmp(b, "name") == 0)
{
delete_name(head);
print(head);
}
else if (strcmp(b, "num") == 0)
{
delete_num(head);
print(head);
}
else
{
printf("out of range\n");
}
}
if (strcmp(a, "query") == 0)
{
char b[4];
printf("The way you want to query (id/name/num): ");
scanf("%s", b);
scanf("%s", b);
if (strcmp(b, "id") == 0)
{
query_id(head);
}
else if (strcmp(b, "name") == 0)
{
query_name(head);
}
else if (strcmp(b, "num") == 0)
{
query_num(head);
}
else
{
printf("out of range\n");
}
}
if (strcmp(a, "update") == 0)
{
update(head);
print(head);
}
}
{
query_id(head);
}
else if (strcmp(b, "name") == 0)
{
query_name(head);
}
else if (strcmp(b, "num") == 0)
{
query_num(head);
}
else
{
printf("out of range\n");
}
}
if (strcmp(a, "update") == 0)
{
update(head);
print(head);
}
}
return 0;
}
}
int init(struct Person** head)
{
struct Person* newp = (struct Person*)malloc(sizeof(struct Person));
{
struct Person* newp = (struct Person*)malloc(sizeof(struct Person));
if (NULL == newp)
{
return F;
}
newp->next = NULL;
*head = newp;
{
return F;
}
newp->next = NULL;
*head = newp;
return T;
}
}
int insert(struct Person* head)
{
struct Person* newp = (struct Person*)malloc(sizeof(struct Person));
{
struct Person* newp = (struct Person*)malloc(sizeof(struct Person));
if(NULL == newp)
{
return F;
}
char id[10];
char name[4];
char num[20];
{
return F;
}
char id[10];
char name[4];
char num[20];
printf("The ID is: ");
scanf("%s", id);
printf("The NAME is: ");
scanf("%s", name);
printf("The NUM is: ");
scanf("%s", num);
scanf("%s", id);
printf("The NAME is: ");
scanf("%s", name);
printf("The NUM is: ");
scanf("%s", num);
while ((head->next != NULL) && (strcmp(head->next->ID, id) < 0))
{
head = head->next;
}
strcpy(newp->ID, id);
strcpy(newp->NAME, name);
strcpy(newp->NUM, num);
{
head = head->next;
}
strcpy(newp->ID, id);
strcpy(newp->NAME, name);
strcpy(newp->NUM, num);
newp->next = head->next;
head->next = newp;
head->next = newp;
return T;
}
}
int delete_id(struct Person* head)
{
char id[10];
{
char id[10];
printf("The ID you want to delete is: ");
scanf("%s", id);
scanf("%s", id);
while (head->next != NULL)
{
if (strcmp(head->next->ID, id) == 0)
{
struct Person* temp = head->next->next;
{
if (strcmp(head->next->ID, id) == 0)
{
struct Person* temp = head->next->next;
free(head->next);
head->next = temp;
head->next = temp;
return T;
}
else
{
head = head->next;
}
}
if(head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if(head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
int delete_name(struct Person* head)
{
char name[4];
{
char name[4];
printf("The NAME you want to delete is: ");
scanf("%s", name);
scanf("%s", name);
while (head->next != NULL)
{
if (strcmp(head->next->NAME, name) == 0)
{
struct Person* temp = head->next->next;
{
if (strcmp(head->next->NAME, name) == 0)
{
struct Person* temp = head->next->next;
free(head->next);
head->next = temp;
head->next = temp;
return T;
}
else
{
head = head->next;
}
}
if(head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if(head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
int delete_num(struct Person* head)
{
char num[20];
{
char num[20];
printf("The NUM you want to delete is: ");
scanf("%s", num);
scanf("%s", num);
while (head->next != NULL)
{
if (strcmp(head->next->NUM, num) == 0)
{
struct Person* temp = head->next->next;
{
if (strcmp(head->next->NUM, num) == 0)
{
struct Person* temp = head->next->next;
free(head->next);
head->next = temp;
head->next = temp;
return T;
}
else
{
head = head->next;
}
}
if(head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if(head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
int update(struct Person* head)
{
char id[10];
char name[5];
char num[20];
{
char id[10];
char name[5];
char num[20];
printf("The ID you want to update is: ");
scanf("%s", id);
printf("The new NAME is: ");
scanf("%s", name);
printf("The new NUM is: ");
scanf("%s", num);
scanf("%s", id);
printf("The new NAME is: ");
scanf("%s", name);
printf("The new NUM is: ");
scanf("%s", num);
while (head->next != NULL)
{
if (strcmp(head->next->ID, id) == 0)
{
strcpy(head->next->NAME, name);
strcpy(head->next->NUM, num);
{
if (strcmp(head->next->ID, id) == 0)
{
strcpy(head->next->NAME, name);
strcpy(head->next->NUM, num);
return T;
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
int query_id(struct Person* head)
{
char id[10];
{
char id[10];
printf("The ID you want to query is: ");
scanf("%s", id);
scanf("%s", id);
while (head->next != NULL)
{
if (strcmp(head->next->ID, id) == 0)
{
printf("ID: %s\nNAME: %s\nNUM: %s\n", head->next->ID, head->next->NAME, head->next->NUM);
{
if (strcmp(head->next->ID, id) == 0)
{
printf("ID: %s\nNAME: %s\nNUM: %s\n", head->next->ID, head->next->NAME, head->next->NUM);
return T;
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
int query_name(struct Person* head)
{
char name[4];
{
char name[4];
printf("The NAME you want to query is: ");
scanf("%s", name);
scanf("%s", name);
while (head->next != NULL)
{
if (strcmp(head->next->NAME, name) == 0)
{
printf("ID: %s\nNAME: %s\nNUM: %s\n", head->next->ID, head->next->NAME, head->next->NUM);
{
if (strcmp(head->next->NAME, name) == 0)
{
printf("ID: %s\nNAME: %s\nNUM: %s\n", head->next->ID, head->next->NAME, head->next->NUM);
return T;
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
int query_num(struct Person* head)
{
char num[20];
{
char num[20];
printf("The NUM you want to query is: ");
scanf("%s", num);
while (head->next != NULL)
{
if (strcmp(head->next->NUM, num) == 0)
{
printf("ID: %s\nNAME: %s\nNUM: %s\n", head->next->ID, head->next->NAME, head->next->NUM);
scanf("%s", num);
while (head->next != NULL)
{
if (strcmp(head->next->NUM, num) == 0)
{
printf("ID: %s\nNAME: %s\nNUM: %s\n", head->next->ID, head->next->NAME, head->next->NUM);
return T;
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
}
else
{
head = head->next;
}
}
if (head->next == NULL)
{
printf("out of range\n");
return F;
}
}
}
}
void print(struct Person* head)
{
int count = 0;
system("clear");
printf(" ********************************************************************\n");
printf(" Welcome to the addressbook system! \n");
printf(" ********************************************************************\n");
printf(" ID NAME NUM\n");
printf(" ----------------------------\n");
{
int count = 0;
system("clear");
printf(" ********************************************************************\n");
printf(" Welcome to the addressbook system! \n");
printf(" ********************************************************************\n");
printf(" ID NAME NUM\n");
printf(" ----------------------------\n");
while (head->next != NULL)
{
count++;
printf("%d. %s %3s %s\n", count, head->next->ID, head->next->NAME, head->next->NUM);
head = head->next;
}
printf(" ----------------------------\n");
}
{
count++;
printf("%d. %s %3s %s\n", count, head->next->ID, head->next->NAME, head->next->NUM);
head = head->next;
}
printf(" ----------------------------\n");
}