链表加文件格式化存储
main.c
#include"link.h"
int main(int argc,char *argv[])
{
int num = 0;
person head;
head.next = NULL;
read_line(&head);
while(1)
{
viewhome();
printf("input num:\n");
scanf("%d",&num);
choose_num(&head,num);
sleep(2);
printf("sa\n");
}
return 0;
}
link.c
#include"link.h"
void viewhome()
{
system("clear");
printf("\t\t1.录入信息 2.查询所有\n");
printf("\t\t3.查找用户 4.删除用户\n");
printf("\t\t5.修改用户 6.退出系统\n");
}
int choose_num(person *head,int num)
{
if(head == NULL)
{
return L_FAIL;
}
switch(num)
{
case 1:
{
head_insert(head);
break;
}
case 2:
{
queryall(head);
break;
}
case 3:
{
//queryone(head);
display_one(queryone(head));
break;
}
case 4:
{
delete_person(queryone(head));
break;
}
case 5:
{
revise_user(queryone(head));
break;
}
case 6:
{
write_line(head);
if(L_OK == desptroy(head))
{
exit(1);
}
}
default:
{
printf("dasd\n");
printf("erorr!");
exit(1);
}
}
}
int head_insert(person *head)
{
if(head == NULL)
{
return L_FAIL;
}
char ch;
person *pNode;
do
{
pNode = (person *)malloc(sizeof(person));
if(pNode == NULL)
{
return L_FAIL;
}
printf("input id:\n");
scanf("%s",pNode->id);
printf("input name:\n");
scanf("%s",pNode->name);
printf("input ipone:\n");
scanf("%s",pNode->ipone);
pNode->next = head->next;
if(head->next != NULL)
{
head->next->per = pNode;
}
pNode->per = head;
head->next = pNode;
printf("continue y/n:\n");
scanf("%*c%c",&ch);
}while ('y' == ch);
return L_OK;
}
int display(person *head)
{
if(head == NULL)
{
return L_FAIL;
}
person *pNode = head->next;
while(pNode != NULL)
{
printf("%s:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
pNode = pNode->next;
}
}
int mystrcmpy(char *dest,char *src)
{
if(dest == NULL || src == NULL)
{
return L_FAIL;
}
int i = 0;
while(dest[i] != '\0')
{
if(dest[i] != src[i])
{
return L_FAIL;
}
i++;
}
return L_OK;
}
person *queryone(person *head)
{
if(head == NULL)
{
return NULL;
}
char src[MAX_SIEZ];
person *pNode = head->next;
printf("query candition:\n");
scanf("%s",src);
if(src[0] >= '0' && src[0] <= '9')
{
while (pNode != NULL)
{
if(L_OK == mystrcmpy(pNode->id,src))
{
// printf("%d:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
return pNode;
}
pNode = pNode->next;
}
}
else
{
while(pNode != NULL)
{
if(L_OK == mystrcmpy(pNode->name,src))
{
// printf("%d:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
return pNode;
}
pNode = pNode->next;
}
}
return NULL;
}
int display_one(person *pNode)
{
if(pNode == NULL)
{
printf("no user!\n");
return L_FAIL;
}
printf("%s:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
return L_OK;
}
int delete_person(person *pNode)
{
if(pNode == NULL)
{
printf("no user!\n");
return L_OK;
}
if(pNode->per != NULL)
{
pNode->per->next = pNode->next;
}
if(pNode->next != NULL)
{
pNode->next->per = pNode->per;
}
free(pNode);
pNode = NULL;
return L_OK;
}
int revise_user(person *pNode)
{
if(pNode == NULL)
{
printf("no user!\n");
return L_FAIL;
}
printf("input id:\n");
scanf("%s",pNode->id);
printf("input name:\n");
scanf("%s",pNode->name);
printf("input ipone:\n");
scanf("%s",pNode->ipone);
return L_OK;
}
int desptroy(person *head)
{
if(head == NULL )
{
return L_FAIL;
}
person *pNode = head->next;
person *Temp = NULL;
while(pNode != NULL)
{
Temp = pNode;
if(pNode->per != NULL)
{
pNode->per->next = pNode->next;
}
if(pNode->next != NULL)
{
pNode->next->per = pNode->per;
}
pNode = pNode->next;
if(Temp != NULL)
{
Temp->next = Temp;
free(Temp);
Temp = NULL;
}
}
return L_OK;
}
link.h
#ifndef _LINK_
#define _LINK_
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
enum recrod
{
MAX_SIEZ = 12,
L_OK,
L_FAIL
};
typedef struct Person
{
char id[MAX_SIEZ];
char name[MAX_SIEZ];
char ipone[MAX_SIEZ];
struct Person *per;
struct Person *next;
}person;
void viewhome();
int choose_num(person *, int );
int head_insert(person *);
int display(person *);
person *queryone(person *);
int display_one(person *);
int delete_person(person *);
int revise_user(person *);
int desptroy(person *);
int write_line(person *);
int read_line(person *);
int queryall(person *);
#endif
queryall.c
#include"link.h"
int mystrcmpy1(char *dest,char *src)
{
int i = 0;
while(dest[i] == src[i])
{
i++;
if(dest[i] == '\0')
{
return L_FAIL;
}
else if (src[i] == '\0')
{
return L_OK;
}
}
if(dest[i]>src[i])
{
return L_OK;
}
else
{
return L_FAIL;
}
}
int head1(person *Target,person *pNode)
{
pNode->next = Target;
if(Target->per != NULL)
{
Target->per->next = pNode;
}
pNode->per = Target->per;
Target->per = pNode;
}
int tail(person *Target,person *pNode)
{
if(Target->next != NULL)
{
Target->next->per = pNode;
}
pNode->next = Target->next;
Target->next = pNode;
pNode->per = Target;
}
int queryall(person *head)
{
if(head == NULL)
{
return L_FAIL;
}
if(head->next == NULL)
{
printf("on user!\n");
return L_OK;
}
person *pNode = head->next;
head->next = NULL;
person *p = pNode;
pNode = pNode->next;
if(head->next != NULL)
{
head->next->per = p;
}
p->next = head->next;
p->per = head;
head->next = p;
person *Temp;
while(pNode != NULL)
{
p = pNode;
pNode = pNode->next;
Temp = head->next;
while(Temp != NULL)
{
if(mystrcmpy1(Temp->name,p->name) == L_OK)
{
head1(Temp,p);
break;
}
else if (Temp->next == NULL)
{
tail(Temp,p);
break;
}
else
{
Temp = Temp->next;
}
}
}
pNode = head->next;
while(pNode != NULL)
{
printf("%s:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
pNode = pNode->next;
}
return L_OK;
}
read_line.c
#include"link.h"
int read_line(person *head)
{
if(head == NULL)
{
return L_FAIL;
}
FILE *fp;
fp = fopen("ln.txt","a+");
getc(fp);
if(feof(fp) != 0)
{
return L_FAIL;
}
int i = 0;
char ch = '0';
char src[MAX_SIEZ];
person *pNode;
fread(&ch,1,1,fp);
fseek(fp,0,SEEK_SET);
do
{
pNode = (person *)malloc(sizeof(person));
if(pNode == NULL)
{
return L_FAIL;
}
// fread(&ch,1,1,fp);
// while((ch != '\n') && (feof(fp) == 0))
// {
// while(ch != ':')
// {
// src[i] = ch;
// i++;
// fread(&ch,1,1,fp);
// }
// src[i] = '\0';
// strcpy(pNode->id,src);
// i = 0;
// fread(&ch,1,1,fp);
// while(ch != ':')
// {
// src[i] = ch;
// i++;
// fread(&ch,1,1,fp);
// }
// src[i] = '\0';
// strcpy(pNode->name,src);
// i = 0;
// fread(&ch,1,1,fp);
// while((ch != '\n') && (feof(fp) == 0))
// {
// src[i] = ch;
// i++;
// fread(&ch,1,1,fp);
// }
// src[i] = '\0';
// strcpy(pNode->ipone,src);
// i = 0;
// }
fscanf(fp,"%s%s%s",pNode->id,pNode->name,pNode->ipone);
pNode->next = head->next;
if(head->next != NULL)
{
head->next->per = pNode;
}
pNode->per = head;
head->next = pNode;
}while(feof(fp) == 0);
fclose(fp);
return L_OK;
}
write_line.c
#include"link.h"
int read_line(person *head)
{
if(head == NULL)
{
return L_FAIL;
}
FILE *fp;
fp = fopen("ln.txt","a+");
getc(fp);
if(feof(fp) != 0)
{
return L_FAIL;
}
int i = 0;
char ch = '0';
char src[MAX_SIEZ];
person *pNode;
fread(&ch,1,1,fp);
fseek(fp,0,SEEK_SET);
do
{
pNode = (person *)malloc(sizeof(person));
if(pNode == NULL)
{
return L_FAIL;
}
// fread(&ch,1,1,fp);
// while((ch != '\n') && (feof(fp) == 0))
// {
// while(ch != ':')
// {
// src[i] = ch;
// i++;
// fread(&ch,1,1,fp);
// }
// src[i] = '\0';
// strcpy(pNode->id,src);
// i = 0;
// fread(&ch,1,1,fp);
// while(ch != ':')
// {
// src[i] = ch;
// i++;
// fread(&ch,1,1,fp);
// }
// src[i] = '\0';
// strcpy(pNode->name,src);
// i = 0;
// fread(&ch,1,1,fp);
// while((ch != '\n') && (feof(fp) == 0))
// {
// src[i] = ch;
// i++;
// fread(&ch,1,1,fp);
// }
// src[i] = '\0';
// strcpy(pNode->ipone,src);
// i = 0;
// }
fscanf(fp,"%s%s%s",pNode->id,pNode->name,pNode->ipone);
pNode->next = head->next;
if(head->next != NULL)
{
head->next->per = pNode;
}
pNode->per = head;
head->next = pNode;
}while(feof(fp) == 0);
fclose(fp);
return L_OK;
}
makefile
obj := main.o link.o write_line.o read_line.o queryall.o
list:$(obj)
gcc $(obj) -o list
%.o:%.c
gcc $< -c -o $@
.PHONY:clean
clean:
rm -rf *.o list
纯数据库sqlite3
main.c
#include "list.h"
int main(int argc,char *argv[])
{
int num;
// scanf("%d",num);
createlist();
while(1)
{
viewhome();
scanf("%d",&num);
choose_num(num);
sleep(2);
}
return 0;
}
list.c
#include "list.h"
sqlite3 *db;
char *errmsg;
void print_error(int ret,char *err,sqlite3 *db)
{
if(ret != SQLITE_OK)
{
printf("%s:%s\n",err,sqlite3_errmsg(db));
exit(1);
}
}
void viewhome()
{
system("clear");
printf("\t\t1.录入信息 2.查询所有\n");
printf("\t\t3.查找用户 4.删除用户\n");
printf("\t\t5.修改用户 6.退出系统\n");
printf("input num:\n");
}
void createlist()
{
char sql[1024] = {0};
int ret = sqlite3_open("test.db",&db);
print_error(ret,"open",db);
memset(sql,0,sizeof(sql));
strcpy(sql,"create table if not exists person(id integer primary key,name text,ipone text)");
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
print_error(ret,"exec",db);
return;
}
void choose_num(int num)
{
switch(num)
{
case 1:
{
insert_data();
break;
}
case 2:
{
queryall();
break;
}
case 3:
{
queryone();
break;
}
case 4:
{
delete_list();
break;
}
case 5:
{
revise_user();
break;
}
case 6:
{
my_exit();
break;
}
}
}
void insert_data()
{
char sql[1024]={0};
int id;
char name[20];
char ipone[20];
char rc = '0';
do
{
printf("id:\n");
scanf("%d",&id);
printf("name:\n");
scanf("%s",name);
printf("ipone:\n");
scanf("%s",ipone);
sprintf(sql,"insert into person(id,name,ipone)values(%d,'%s',%s);",id,name,ipone);
int ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
print_error(ret,"insert",db);
printf("is continue y/n\n");
scanf("%*c%c",&rc);
}while(rc == 'y');
return;
}
void queryall()
{
char sql[1024];
char **result;
int nrow;
int ncolumn;
memset(sql,0,sizeof(sql));
strcpy(sql,"select *from person order by name asc");
int ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg);
for(int i=1; i<=nrow; i++)
{
for(int j=0; j<ncolumn; j++)
{
printf("%s|",result[i*ncolumn + j]);
}
printf("\n");
}
sqlite3_free_table(result);
}
int my_sqlite_callback(void *para,int contun,char **columvalue,char **columnName)
{
int i;
for(i=0; i<contun; i++)
{
printf("%s:%s|",columnName[i],columvalue[i]);
}
printf("\n");
return 0;
}
void queryone()
{
int i;
char sql[1024] ={0};
char ch[20];
printf("input :\n");
scanf("%s",ch);
if(ch[0] > '0' && ch[0] < '9')
{
sprintf(sql,"select *from person where id = %s;",ch);
}
else
{
sprintf(sql,"select *from person where name = '%s';",ch);
}
int ret = sqlite3_exec(db,sql,my_sqlite_callback,NULL,&errmsg);
print_error(ret,"select one",db);
return;
}
void delete_list()
{
char sql[1024]={0};
char ch[20];
printf("input :\n");
scanf("%s",ch);
if(ch[0] > '0' && ch[0] < '9')
{
sprintf(sql,"delete from person where id = %s",ch);
}
else
{
sprintf(sql,"delete from person where name = '%s';",ch);
}
int ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
print_error(ret,"select",db);
return;
}
void revise_user()
{
char sql[1024]={0};
int id;
char name[20];
char ipone[20];
char ch[20]={0};
do
{
printf("input revise:\n");
scanf("%s",ch);
printf("id:\n");
scanf("%d",&id);
printf("name:\n");
scanf("%s",name);
printf("ipone:\n");
scanf("%s",ipone);
if(ch[0] > '0' && ch[0] < '9')
{
sprintf(sql,"update person set id = %d,name = '%s',ipone = %s where id = %s;",id,name,ipone,ch);
}
else
{
sprintf(sql,"update person set id = %d,name = '%s',ipone = %s where name = '%s';",id,name,ipone,ch);
}
int ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
print_error(ret,"insert",db);
}while(0);
return;
}
void my_exit()
{
exit(1);
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <unistd.h>
void createlist();
void viewhome();
void queryall();
void choose_num(int num);
void insert_data();
void queryall();
void queryone();
void delete_list();
void revise_user();
void my_exit();
#endif