基於Linux C 單鏈表的通訊錄程式
阿新 • • 發佈:2019-02-08
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <ctype.h>
#define MAX_NAME 100
#define MAX_TEL 11
#define OK 1
#define ERROR 0
typedef char Elementtype;
typedef char Status;
typedef struct node
{
Elementtype name[MAX_NAME];
Elementtype tel[MAX_TEL] ;
struct node * next;
}Node;
Status init (Node **head);
Status inserthead(Node *head);
Status deletename(Node *head);
void queryname(Node*head);
void printflist(Node*head);
Status updatetel(Node*head);
Status updatename(Node*head);
Status changelist(Node*head);
int length(Node*head);
void create_txt(Node*head);
Status wipedata(Node*head);
int main(int argc, char ** argv)
{
Node * head ;
init(&head );
int ret ;
int num;
int i;
ret = init (&head);
if(ret == ERROR)
{
return -1;
}
system("clear");
for(;;)
{
switch(menu_select())
{
case 1:
{
inserthead(head);
break;
}
case 2:
{
printflist(head);
break;
}
case 3:
{
deletename(head);
break;
}
case 4:
{
updatetel(head);
break;
}
case 5:
{
updatename(head);
break;
}
case 6:
{
queryname(head);
break;
}
case 7:
{
create_txt(head);
break;
}
case 8:
{
exit(0);
}
}
}
return 0;
}
menu_select()
{
int c;
char str[30];
printf("----- Welcome to the address book system ------\n");
printf("|--------------1. inserthead------------------|\n");
printf("|--------------2. list------------------------|\n");
printf("|--------------3. deletename------------------|\n");
printf("|--------------4. updatetel-------------------|\n");
printf("|--------------5. updatename----------------- |\n");
printf("|--------------6. queryname-------------------|\n");
printf("|--------------7. create_tel.txt--------------|\n");
printf("|--------------8. quit------------------------|\n");
printf("|---------------------------------------------|\n");
do
{
printf("please input num 1 ~ 8 :\n");
scanf ("%s",&str);
c = atoi(str);
}while(c < 0 || c > 8);
return c;
}
Status init (Node **head)
{
Node *new = (Node*) malloc(sizeof(Node));
if(NULL == new)
{
return ERROR;
}
*head = new;
new -> next = NULL;
return OK;
}
Status inserthead(Node *head)
{
char name[10];
char tel[15];
Node*new = (Node*)malloc (sizeof(Node));
if(new == NULL)
{
return ERROR;
}
strcpy(new -> name ,name);
strcpy(new -> tel ,tel);
if(head -> next == NULL)
{
head -> next = new;
new -> next = NULL;
}
else
{
new -> next = head -> next;
head -> next = new;
}
printf("you can enter name and tel .\n");
int n;
// scanf("%d",&n);
int i;
printf("please input name :\n");
scanf("%s",head ->next-> name);
printf("please input tel :\n");
scanf("%s",head ->next -> tel);
return OK ;
}
Status deletename(Node *head)
{
char name_1[10] ;
printf("please input the name you want to remove !\n");
scanf("%s",&name_1);
Node *new = (Node*)malloc(sizeof(Node));
Node* temp;
if(new == NULL)
{
return ERROR;
}
while(head -> next != NULL )
{
int a =strcmp(head -> next -> name,name_1);
if (a == 0)
{
temp = head -> next ;
head -> next = head -> next -> next;
free (temp);
temp = NULL;
}
else
{
head = head -> next ;
}
}
return OK;
}
void printflist(Node*head)
{
printf("----- Welcome to the address book system ----\n");
printf("---------------------------------------------\n");
while(head -> next != NULL)
{
printf("name :%s tel :%s \n",head ->next -> name,head ->next -> tel );
head = head -> next;
}
printf("---------------------------------------------\n");
}
void create_txt(Node*head)
{
FILE *file = fopen ("tel.txt","w+");
if(NULL == file)
{
perror(" open file error.");
exit(1);
}
while (head -> next != NULL)
{
fprintf(file,"%s\t%s\n",head -> next -> name ,head ->next -> tel);
head = head -> next ;
}
fclose(file);
printf("--------------------------\n");
printf("create_tel.txt successful.\n");
printf("--------------------------\n");
}
/*
void scanf_txt(Node*head)
{
FILE *file = fopen ("tel.txt","w+");
if(NULL == file)
{
perror(" open file.");
exit(1);
}
fread(stdout,sizeof(char),100,file);
}
*/
void queryname(Node*head)
{
char name_1[20];
printf("please input name you want to query !\n");
scanf("%s",&name_1);
int flag = 0;
int index = 1;
while (head -> next != NULL)
{
int a = strcmp(head -> next -> name ,name_1);
if(a == 0)
{ printf("------------------------------------------------\n");
printf(" name = %s ---- index = %d\n",name_1, index);
printf("------------------------------------------------\n");
flag ++;
}
index++;
head = head -> next;
}
if(flag == 0)
{
printf("----------------------------------------------------\n");
printf(" No name here , you can input agian !\n" );
printf("----------------------------------------------------\n");
}
}
Status updatetel(Node *head)
{
char te1_1[15];
char tel_2[15];
printf("Please input your original number. \n");
scanf("%s", &te1_1);
int i;
//for(i = 0;i < length(head);i++)
while(head -> next != NULL)
{
if( strcmp(te1_1, head -> next -> tel) == 0 )
{
printf("please input the tel you want to change .\n");
scanf("%s",&tel_2);
strcpy(head -> next-> tel ,tel_2);
// scanf("%s", head -> next -> tel);
}
head = head -> next;
}
return OK;
}
Status updatename(Node*head)
{
char name1[10];
char name2[10];
printf("Please input your original name. \n");
scanf("%s", &name1);
//printf("%s\n", name1);
int i;
//for(i = 0;i < length(head);i++)
while(head -> next != NULL)
{
if( strcmp(name1, head -> next -> name) == 0)
{
printf("please input the name you want to change !\n");
scanf("%s", &name2);
strcpy(head -> next -> name, name2);
}
head = head -> next;
}
return OK;
}
int length(Node*head)
{
int len = 0;
while(head -> next != NULL)
{
head = head -> next;
len++;
}
return len ;
}
/*
Status wipedata(Node *head)
{
Node *temp_1 ;
while(head)
{
temp_1 = head -> next;
free(head);
temp_1 = head;
}
return OK;
} */
/*
Status changelist(Node*head)
{
Node *temp;
char flag_1[100];
char flag_2[100];
while(head -> next != NULL)
{
temp = head -> next;
while(temp -> next != NULL)
{
if( strcmp(head-> next -> name , temp -> next -> name) == 1)
{
strcpy(flag_1 , head -> next -> name);
strcpy(head -> next -> name , temp -> next -> name);
strcpy(temp -> next -> name , flag_1);
strcpy(flag_2 , head -> next -> tel);
strcpy(head -> next -> tel , temp -> next -> tel);
strcpy(temp -> next -> tel , flag_2);
}
temp = temp -> next;
}
head = head -> next;
}
free(temp);
temp = NULL;
}
*/
#include <stdlib.h>
#include<string.h>
#include <ctype.h>
#define MAX_NAME 100
#define MAX_TEL 11
#define OK 1
#define ERROR 0
typedef char Elementtype;
typedef char Status;
typedef struct node
{
Elementtype name[MAX_NAME];
Elementtype tel[MAX_TEL] ;
struct node * next;
}Node;
Status init (Node **head);
Status inserthead(Node *head);
Status deletename(Node *head);
void queryname(Node*head);
void printflist(Node*head);
Status updatetel(Node*head);
Status updatename(Node*head);
Status changelist(Node*head);
int length(Node*head);
void create_txt(Node*head);
Status wipedata(Node*head);
int main(int argc, char ** argv)
{
Node * head ;
init(&head );
int ret ;
int num;
int i;
ret = init (&head);
if(ret == ERROR)
{
return -1;
}
system("clear");
for(;;)
{
switch(menu_select())
{
case 1:
{
inserthead(head);
break;
}
case 2:
{
printflist(head);
break;
}
case 3:
{
deletename(head);
break;
}
case 4:
{
updatetel(head);
break;
}
case 5:
{
updatename(head);
break;
}
case 6:
{
queryname(head);
break;
}
case 7:
{
create_txt(head);
break;
}
case 8:
{
exit(0);
}
}
}
return 0;
}
menu_select()
{
int c;
char str[30];
printf("----- Welcome to the address book system ------\n");
printf("|--------------1. inserthead------------------|\n");
printf("|--------------2. list------------------------|\n");
printf("|--------------3. deletename------------------|\n");
printf("|--------------4. updatetel-------------------|\n");
printf("|--------------5. updatename----------------- |\n");
printf("|--------------6. queryname-------------------|\n");
printf("|--------------7. create_tel.txt--------------|\n");
printf("|--------------8. quit------------------------|\n");
printf("|---------------------------------------------|\n");
do
{
printf("please input num 1 ~ 8 :\n");
scanf ("%s",&str);
c = atoi(str);
}while(c < 0 || c > 8);
return c;
}
Status init (Node **head)
{
Node *new = (Node*) malloc(sizeof(Node));
if(NULL == new)
{
return ERROR;
}
*head = new;
new -> next = NULL;
return OK;
}
Status inserthead(Node *head)
{
char name[10];
char tel[15];
Node*new = (Node*)malloc (sizeof(Node));
if(new == NULL)
{
return ERROR;
}
strcpy(new -> name ,name);
strcpy(new -> tel ,tel);
if(head -> next == NULL)
{
head -> next = new;
new -> next = NULL;
}
else
{
new -> next = head -> next;
head -> next = new;
}
printf("you can enter name and tel .\n");
int n;
// scanf("%d",&n);
int i;
printf("please input name :\n");
scanf("%s",head ->next-> name);
printf("please input tel :\n");
scanf("%s",head ->next -> tel);
return OK ;
}
Status deletename(Node *head)
{
char name_1[10] ;
printf("please input the name you want to remove !\n");
scanf("%s",&name_1);
Node *new = (Node*)malloc(sizeof(Node));
Node* temp;
if(new == NULL)
{
return ERROR;
}
while(head -> next != NULL )
{
int a =strcmp(head -> next -> name,name_1);
if (a == 0)
{
temp = head -> next ;
head -> next = head -> next -> next;
free (temp);
temp = NULL;
}
else
{
head = head -> next ;
}
}
return OK;
}
void printflist(Node*head)
{
printf("----- Welcome to the address book system ----\n");
printf("---------------------------------------------\n");
while(head -> next != NULL)
{
printf("name :%s tel :%s \n",head ->next -> name,head ->next -> tel );
head = head -> next;
}
printf("---------------------------------------------\n");
}
void create_txt(Node*head)
{
FILE *file = fopen ("tel.txt","w+");
if(NULL == file)
{
perror(" open file error.");
exit(1);
}
while (head -> next != NULL)
{
fprintf(file,"%s\t%s\n",head -> next -> name ,head ->next -> tel);
head = head -> next ;
}
fclose(file);
printf("--------------------------\n");
printf("create_tel.txt successful.\n");
printf("--------------------------\n");
}
/*
void scanf_txt(Node*head)
{
FILE *file = fopen ("tel.txt","w+");
if(NULL == file)
{
perror(" open file.");
exit(1);
}
fread(stdout,sizeof(char),100,file);
}
*/
void queryname(Node*head)
{
char name_1[20];
printf("please input name you want to query !\n");
scanf("%s",&name_1);
int flag = 0;
int index = 1;
while (head -> next != NULL)
{
int a = strcmp(head -> next -> name ,name_1);
if(a == 0)
{ printf("------------------------------------------------\n");
printf(" name = %s ---- index = %d\n",name_1, index);
printf("------------------------------------------------\n");
flag ++;
}
index++;
head = head -> next;
}
if(flag == 0)
{
printf("----------------------------------------------------\n");
printf(" No name here , you can input agian !\n" );
printf("----------------------------------------------------\n");
}
}
Status updatetel(Node *head)
{
char te1_1[15];
char tel_2[15];
printf("Please input your original number. \n");
scanf("%s", &te1_1);
int i;
//for(i = 0;i < length(head);i++)
while(head -> next != NULL)
{
if( strcmp(te1_1, head -> next -> tel) == 0 )
{
printf("please input the tel you want to change .\n");
scanf("%s",&tel_2);
strcpy(head -> next-> tel ,tel_2);
// scanf("%s", head -> next -> tel);
}
head = head -> next;
}
return OK;
}
Status updatename(Node*head)
{
char name1[10];
char name2[10];
printf("Please input your original name. \n");
scanf("%s", &name1);
//printf("%s\n", name1);
int i;
//for(i = 0;i < length(head);i++)
while(head -> next != NULL)
{
if( strcmp(name1, head -> next -> name) == 0)
{
printf("please input the name you want to change !\n");
scanf("%s", &name2);
strcpy(head -> next -> name, name2);
}
head = head -> next;
}
return OK;
}
int length(Node*head)
{
int len = 0;
while(head -> next != NULL)
{
head = head -> next;
len++;
}
return len ;
}
/*
Status wipedata(Node *head)
{
Node *temp_1 ;
while(head)
{
temp_1 = head -> next;
free(head);
temp_1 = head;
}
return OK;
} */
/*
Status changelist(Node*head)
{
Node *temp;
char flag_1[100];
char flag_2[100];
while(head -> next != NULL)
{
temp = head -> next;
while(temp -> next != NULL)
{
if( strcmp(head-> next -> name , temp -> next -> name) == 1)
{
strcpy(flag_1 , head -> next -> name);
strcpy(head -> next -> name , temp -> next -> name);
strcpy(temp -> next -> name , flag_1);
strcpy(flag_2 , head -> next -> tel);
strcpy(head -> next -> tel , temp -> next -> tel);
strcpy(temp -> next -> tel , flag_2);
}
temp = temp -> next;
}
head = head -> next;
}
free(temp);
temp = NULL;
}
*/