//头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
typedef struct node
{
int data;
struct node *next;
}linknode, *linklist;
linklist Create_Headnode();
void Create_Node(linklist Headnode);
void Inverse_List(linklist Headnode);
void Sort_List(linklist Headnode);
void menu();
int Length_List(linklist Headnode);
void Print_List(linklist Headnode);
void Save_List(linklist Headnode);
void Read_List(linklist Headnode);
void Read(linklist Headnode);
void Insert_List(linklist Headnode, int num);
//单链表各功能实现
#include "linklist.h"
linklist Create_Headnode()
{
linklist Headnode;
if((Headnode = (linklist)malloc(sizeof(linknode))) == NULL)
{
printf("malloc failed\n");
return Headnode;
}
Headnode->next = NULL;
return Headnode;
}
void Create_Node(linklist Headnode)
{
linklist p;
int num;
while(1)
{
printf("please input a num:\n");
scanf("%d",&num);
if(num == -1)
break;
if((p = (linklist)malloc(sizeof(linknode))) == NULL)
{
printf("malloc failed\n");
return;
}
p->data = num;
Headnode->next = p;
Headnode = p;
}
}
void Inverse_List(linklist Headnode)
{
linklist p, q;
p = Headnode->next;
Headnode->next = NULL;
while(p != NULL)
{
q = p->next;
p->next = Headnode->next;
Headnode->next = p;
p = q;
}
}
int Length_List(linklist Headnode)
{
int i;
Headnode = Headnode->next;
while(Headnode != NULL)
{
i++;
Headnode = Headnode->next;
}
return i;
}
void Print_List(linklist Headnode)
{
Headnode = Headnode->next;
while(Headnode != NULL)
{
printf("%d\t",Headnode->data);
Headnode = Headnode->next;
}
puts("");
}
void Sort_List(linklist Headnode)
{
linklist p;
int tmp;
int len = Length_List(Headnode);
int i, j;
Headnode = Headnode->next;
p = Headnode;
for(i = 0; i < len-1; i++)
{
Headnode = p;
for(j = 0; j < len-i-1; j++)
{
if(Headnode->data > Headnode->next->data)
{
tmp = Headnode->data;
Headnode->data = Headnode->next->data;
Headnode->next->data = tmp;
}
Headnode = Headnode->next;
}
}
}
void menu()
{
printf("0->menu\n");
printf("1->Create_Node\n");
printf("2->Inverse_Node\n");
printf("3->Sort_Node\n");
printf("4->Print_Node\n");
printf("5->Len_Node\n");
printf("6->Save_Node\n");
printf("7->Read_Node\n");
printf("8->Insert_Node\n");
}
void Save(linklist Headnode) //这里是保存到文件中,保存到数据库中的在sqlite.c中
{
FILE *fp;
fp = fopen("./save.txt","w");
if(fp == NULL)
{
printf("fopen failed!");
return;
}
Headnode = Headnode->next;
while(Headnode)
{
fprintf(fp,"%d\n",Headnode->data);
Headnode = Headnode->next;
}
printf("save success\n");
fclose(fp);
}
void Insert_List(linklist Headnode, int num)
{
linklist p;
p = (linklist)malloc(sizeof(linknode));
p->data = num;
p->next = Headnode->next;
Headnode->next = p;
return ;
}
//将数据保存到数据库中
#include "linklist.h"
int callback(void *arg, int num, char *column_values[], char *column_name[])
{
int i;
linklist Headnode;
linklist p;
Headnode = (linklist)arg;
p = (linklist)malloc(sizeof(linknode));
p->data = atoi(column_values[0]);
p->next = NULL;
while(Headnode->next != NULL)
{
Headnode = Headnode->next;
}
Headnode->next = p;
return 0;
}
void Read_List(linklist Headnode)
{
char buf[128] = {0};
sqlite3 *db;
int ret = 0;
ret = sqlite3_open("./linklist.db",&db);
if(ret == -1)
{
printf("open failed\n");
return;
}
sprintf(buf,"select * from base");
sqlite3_exec(db, buf, callback, (void *)Headnode, NULL);
sqlite3_close(db);
return ;
}
void Save_List(linklist Headnode)
{
system("rm linklist.db");
sqlite3 *db;
char buf[128] = {0};
int ret;
Headnode = Headnode->next;
ret = sqlite3_open("./linklist.db",&db);
if(ret == -1)
{
printf("open failed\n");
return;
}
sprintf(buf, "create table base (ID Integer)");
sqlite3_exec(db, buf, NULL, NULL, NULL);
while(Headnode != NULL)
{
memset(buf,0,128);
sprintf(buf,"insert into base values (%d)",Headnode->data);
sqlite3_exec(db, buf, NULL, NULL, NULL);
Headnode = Headnode->next;
}
sqlite3_close(db);
return;
}
//测试函数
#include "linklist.h"
void Read(linklist Headnode) //这里是从文件中读取,从数据库中读取在sqlite.c中的Read_List函数
{
FILE *fp;
int data;
int ret;
linklist p;
fp = fopen("./save.txt","r");
if(fp == NULL)
{
return ;
}
while(1)
{
ret = fscanf(fp,"%d",&data);
if(ret != 1)
{
break;
}
p = (linklist)malloc(sizeof(linknode));
p->data = data;
p->next = NULL;
Headnode->next = p;
Headnode = p;
}
}
int main(void)
{
linklist Headnode;
int choice;
int len;
int num;
int i = 0;
menu();
Headnode = Create_Headnode();
while(1)
{
printf("please input a choice(-1 to quit):\n");
scanf("%d",&choice);
switch(choice)
{
case 0:
menu();
break;
case 1:
printf("-1 to quit\n");
Create_Node(Headnode);
break;
case 2:
Inverse_List(Headnode);
Print_List(Headnode);
break;
case 3:
Sort_List(Headnode);
break;
case 4:
Print_List(Headnode);
break;
case 5:
len = Length_List(Headnode);
printf("len = %d\n",len);
case 6:
Save_List(Headnode);
break;
case 7:
Read_List(Headnode);
break;
case 8:
printf("please input a num:\n");
scanf("%d",&num);
if(num == -1)
{
break;
}
Insert_List(Headnode, num);
break;
default:
break;
}
if(choice == -1)
{
break;
}
}
return 0;
}
使用sqlite3保存单链表的数据库以及单链表功能函数
最新推荐文章于 2024-06-22 17:16:17 发布