1 #include "list.h"
2
3 //头部插入
4 PINFO addback(PINFO phead, char password[20])
5 {
6 //开辟内存
7 PINFO pnew = calloc(1, sizeof(INFO));
8 //拷贝密码
9 strcpy(pnew->password, password);
10 //初始化
11 pnew->ci = 1;
12 pnew->pNext = NULL;
13 //如果头结点为空直接插入
14 if (phead ==NULL)
15 {
16 phead = pnew;
17 }
18 //否则头部插入
19 else
20 {
21 pnew->pNext = phead;
22 phead = pnew;
23 }
24 return phead;
25 }
26
27 //按照密码次数排序
28 PINFO sortbyci(PINFO phead)
29 {
30 //冒泡排序法,一共冒泡这么多次
31 for (PINFO p1 = phead; p1 != NULL;p1=p1->pNext)
32 {
33 for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext)
34 {
35 if (p2->pNext!=NULL)
36 {
37 //如果次数小则进行交换
38 if (p2->ci < p2->pNext->ci)
39 {
40 //交换数据
41 int citemp = p2->ci;
42 p2->ci = p2->pNext->ci;
43 p2->pNext->ci = citemp;
44
45 char passtemp[100];
46 strcpy(passtemp, p2->password);
47 strcpy(p2->password, p2->pNext->password);
48 strcpy(p2->pNext->password, passtemp);
49 }
50 }
51 }
52 }
53 }
54
55 //按照密码排序
56 PINFO sortbypass(PINFO phead)
57 {
58 //冒泡排序
59 for (PINFO p1 = phead; p1 != NULL; p1 = p1->pNext)
60 {
61 for (PINFO p2 = phead; p2 != NULL; p2 = p2->pNext)
62 {
63 if (p2->pNext != NULL)
64 {
65 if (strcmp(p2->password, p2->pNext->password) < 0)
66 {
67 //交换数据
68 int citemp = p2->ci;
69 p2->ci = p2->pNext->ci;
70 p2->pNext->ci = citemp;
71
72 char passtemp[100];
73 strcpy(passtemp, p2->password);
74 strcpy(p2->password, p2->pNext->password);
75 strcpy(p2->pNext->password, passtemp);
76
77 }
78 }
79 }
80 }
81 }
82
83 //显示数据
84 PINFO show(PINFO phead)
85 {
86 if (phead==NULL)
87 {
88 return;
89 }
90 else
91 {
92 printf("%s,%d
", phead->password, phead->ci);
93 show(phead->pNext);
94 }
95 return phead;
96 }
97
98 //判断密码是否存在
99 int isin(PINFO phead, char password[20])
100 {
101 PINFO p = phead;
102 while (p!=NULL)
103 {
104 if (strcmp(p->password,password)==0)
105 {
106 p->ci += 1;
107 return 1;
108 }
109 p = p->pNext;
110 }
111
112 return 0;//不在链表内部
113 }
114
115 //写入到文件
116 void writetofile(PINFO phead, char path[100])
117 {
118 FILE *pf = fopen(path, "w");
119 PINFO p = phead;
120 while (p != NULL)
121 {
122 //格式化打印到文件
123 fprintf(pf, "%s %d
", p->password, p->ci);
124 p = p->pNext;
125 }
126 fclose(pf);
127 }