基本链表的应用
增 删 查 找 排 模糊查找
核心代码如下:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <pthread.h>
#include <windows.h>
#define len sizeof(stu)
typedef struct student//定义一个结构体
{
char name[100];
int num;
double value;
double length;
double time;
int hour;
int minu;
int sec;
} ;
typedef struct node//创建一个指针域,结构域
{
struct student date ;
struct node *next;
} stu;
int z=1;
int n;
int Tips()//菜单
{
int p;
printf("<----------请选择需要的功能---------->\n");
printf("<----------1.输入一个新的购物表-------->\n");
printf("<----------2.按时间删除一个新的纪录-->\n");
printf("<----------3.按名称删除一个新的纪录-->\n");
printf("<----------4.显示全部信息------------>\n");
printf("<----------5.添加一个新的记录-------->\n");
printf("<----------6.根据时间搜索------------>\n");
printf("<----------7.姓名模糊搜索------------>\n");
printf("<----------8.修改信息---------------->\n");
printf("<----------9.距离排序---------------->\n");
printf("<----------10.名称排序---------------->\n");
printf("<----------0.退出------------------->\n");
if(z==1)
printf("<------30秒内未应答将自动关闭------------>");
z--;
printf("\n");
scanf("%d",&p);
return p;
}
stu*head=(stu *)malloc(len);
stu * creat()//创建链表
{
stu *p1,*p2;
int i,t,k=1;
n=0;
printf("请输入输入第一个物品信息\n");
p1=head;
printf("输入第%d个信息数目\n请输入物品的时间和内容格式如\n 物品名:pen(笔) \n 数量:1\n 单价2.0\n 距离5.0\n 日期:6.3\n 时10\n 分30\n 秒10\n",k++);
printf("物品名称:");
scanf("%s",p1->date.name);
printf("数量:");
scanf("%d",&p1->date.num);
printf("单价:");
scanf("%lf",&p1->date.value);
printf("距离:");
scanf("%lf",&p1->date.length);
printf("日期月.日:");
scanf("%lf",&p1->date.time);
printf("时:");
scanf("%d",&p1->date.hour);
printf("分:");
scanf("%d",&p1->date.minu);
printf("秒:");
scanf("%d",&p1->date.sec);
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p2->next=0;
return head;
}
void print(stu * head)//显示
{
stu * p;
printf("此时的%d个物品信息是:\n",n);
p=head;
if(head!=0)
{
while(p!=0)
{
printf("物品名称%s\n数量%d\n单价%.2lf元\n距离%.2lf公里\n日期%.2lf\n时:%d h\n分:%d m\n秒:%d s\n",
p->date.name,p->date.num,p->date.value,p->date.length,p->date.time,p->date.hour,p->date.minu,p->date.sec);
p=p->next;
}
}
}
stu * del(stu * head,int hour,int minu,int sec)//删除(通过时间来删除)
{
stu *p1,*p2;
if(head==0)
printf("表空!\n");
else
{
p1=head;
while(p1->date.hour!=hour && p1->date.minu!=minu&&p1->date.sec!=sec && p1->next!=0)
{
p2=p1;
p1=p1->next;
}
if(p1->date.hour==hour&&p1->date.minu==minu&&p1->date.sec==sec)
{
if(p1==head)
{
head=p1->next;
p1->date.sec=61;
free(head);
}
else
{
p2->next=p1->next;
free(p1);
}
printf("删除成功\n");
n=n-1;
free(p1);
}
else
printf("没有这个物品!\n");
}
return head;
}
stu * dels(stu * head,char name[])//删除(通过名称删除)
{
stu *p1,*p2;
if(head==0)
printf("表空!\n");
else
{
p1=head;
while(strcmp(p1->date.name,name)!=0 && p1->next!=0)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->date.name,name)==0)
{
if(p1==head)
{
head=p1->next;
p1->date.sec=61;
// printf("%d",head);
free(p1);
}
else
{
p2->next=p1->next;
free(p1);
}
printf("删除成功\n");
n=n-1;
}
else
printf("没有这个物品!\n");
}
return head;
}
stu * search(stu * head,int hour,int minu)//搜索(时间搜索)
{
stu *p1,*p2;
if(head==0)
printf("表空!\n");
else
{
p1=head;
while(p1->date.hour!=hour && p1->date.minu!=minu&&p1->next!=0 )
{
p2=p1;
p1=p1->next;
}
if(p1->date.hour==hour&&p1->date.minu==minu)
{
printf("物品名称%s\n数量%d\n单价%.2lf元\n距离%.2lf公里\n日期%.2lf\n时:%d h\n分:%d m\n秒:%d s\n",
p1->date.name,p1->date.num,p1->date.value,p1->date.length,p1->date.time,p1->date.hour,p1->date.minu,p1->date.sec);
printf("查询成功\n");
}
else
printf("没有这个物品!\n");
}
return head;
}
stu * seach(stu * head,char name[])//模糊查找(姓名模糊查找)
{
stu *p1;
int number=0;
if(head==0)
printf("表空!\n");
else
{
for(p1=head; p1!=NULL; p1=p1->next)
{
if(strstr(p1->date.name,name)!=0)
{
printf("物品名称%s\n数量%d\n单价%.2lf元\n距离%.2lf公里\n日期%.2lf\n时:%d h\n分:%d m\n秒:%d s\n",
p1->date.name,p1->date.num,p1->date.value,p1->date.length,p1->date.time,p1->date.hour,p1->date.minu,p1->date.sec);
printf("查询成功\n");
number++;
}
}
if(number==0)
printf("没有这个物品!\n");
}
return head;
}
stu * change(stu * head,char name[])//修改(按名称)
{
stu *p1,*p2;
if(head==0)
printf("表空!\n");
else
{
p1=head;
while(strcmp(p1->date.name,name)!=0 && p1->next!=0)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->date.name,name)==0)
{
printf("物品名称 数量 单价 距离 日期 时 分 秒\n");
scanf("%s%d%lf%lf%lf%d%d%d",
p1->date.name,&p1->date.num,&p1->date.value,&p1->date.length,&p1->date.time,&p1->date.hour,&p1->date.minu,&p1->date.sec);
printf("修改成功\n");
printf("物品名称%s\n数量%d\n单价%.2lf元\n距离%.2lf公里\n日期%.2lf\n时:%d h\n分:%d m\n秒:%d s\n",
p1->date.name,p1->date.num,p1->date.value,p1->date.length,p1->date.time,p1->date.hour,p1->date.minu,p1->date.sec);
}
else
printf("没有这个物品!\n");
}
return head;
}
stu *insert(stu * head, stu * stud)//添加
{
stu *p1,*p2,*p0;
p0=stud;
p1=head;
if(head==0)
{
head=p0;
stud->next=0;
}
else
{
while(p1->date.num<p0->date.num && p1->next!=0)
{
p2=p1;
p1=p1->next;
}
if(p1->date.num>=p0->date.num)
{
if(p1==head)
{
head=p0;
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0,p0->next=0;
}
}
n=n+1;
return head;
}
stu * pall(stu * head)//排序(距离)
{
stu *p1,*p2;
int j=0;
int i=0;
if(head==0)
printf("表空!\n");
{
struct student temp;
for(p1=head; p1!=NULL; p1=p1->next)
{
for(p2=p1->next; p2!=NULL; p2=p2->next)
{
if(p1->date.length>p2->date.length)
{
temp = p2->date;
p2->date = p1->date;
p1->date = temp;
}
}
}
printf("排序成功\n");
print(head);
}
return head;
}
stu * palls(stu * head)//排序(名字)
{
stu *p1,*p2;
int j=0;
int i=0;
if(head==0)
printf("表空!\n");
{
struct student temp;
for(p1=head; p1!=NULL; p1=p1->next)
{
for(p2=p1->next; p2!=NULL; p2=p2->next)
{
if(strcmp(p1->date.name,p2->date.name)>0)
{
temp = p2->date;
p2->date = p1->date;
p1->date = temp;
}
}
}
printf("排序成功\n");
print(head);
}
return head;
}
stu* maining()
{
stu * head=0,*stud;
int m;
int hour,minu,sec;
int i=1;
char name[100];
while(i>0)
{
system("cls");
i=Tips();
switch(i)
{
case 1:
{
system("cls");
printf("开始输入信息:\n");
head=creat();
system("cls");
print(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 2:
{
system("cls");
printf("输入要删除的时间:");
printf("时: 分: 秒:\n ");
scanf("%d%d%d",&hour,&minu,&sec);
head=del(head,hour,minu,sec);
print(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 3://名字删除
{
system("cls");
printf("输入要删除的名称:");
scanf("%s",name);
head=dels(head,name);
print(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 5:
{
system("cls");
stud=(stu *)malloc(len);
printf("物品名称:");
scanf("%s",&stud->date.name);
printf("数量:");
scanf("%d",&stud->date.num);
printf("单价:");
scanf("%lf",&stud->date.value);
printf("距离:");
scanf("%lf",&stud->date.length);
printf("日期:");
scanf("%lf",&stud->date.time);
printf("时:");
scanf("%d",&stud->date.hour);
printf("分:");
scanf("%d",&stud->date.minu);
printf("秒:");
scanf("%d",&stud->date.sec);
head=insert(head,stud);
print(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 4:
{
system("cls");
print(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 6:
{
system("cls");
printf("输入要搜索的时间:\n");
printf("时: 分:\n");
scanf("%d%d",&hour,&minu);
head=search(head,hour,minu);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 7:
{
system("cls");
printf("输入要搜索的名字:");
scanf("%s",name);
head=seach(head,name);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 8:
{
system("cls");
printf("输入要修改的名称:");
scanf("%s",name);
head=change(head,name);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 9:
{
system("cls");
printf("排序:");
head=pall(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
case 10:
{
system("cls");
printf("排序:");
head=palls(head);
printf("请输入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("请输入0返回\n");
scanf("%d",&m);
}
break;
}
}
}
}
stu* time()//(闹钟)
{
stu *p1;
SYSTEMTIME time;
SYSTEMTIME clock;
GetLocalTime(&time);
while(1)
{
Sleep(1000);
GetLocalTime(&time);
for(p1=head; p1!=NULL; p1=p1->next)
{
if(p1->date.hour==time.wHour&&p1->date.minu==time.wMinute&&p1->date.sec==time.wSecond)
{
printf("该买%s了!要购买%d个噢!\a\a\a\n",p1->date.name,p1->date.num);
break;
}
// printf("%d\n",p1);
}
// printf("%d",head);
}
}
void* tprocess1(void* args)//线程1
{
maining();
return NULL;
}
void* tprocess2(void* args)线程2
{
Sleep(35000);
time() ;
return NULL;
}
int main()//多线程
{
pthread_t t1;
pthread_t t2;
pthread_create(&t1,NULL,tprocess1,NULL);
pthread_create(&t2,NULL,tprocess2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
free(head);
return 0;
}
```
总结:动态链表与多线程的运用需要十分注意。