avatar
文章
23
标签
8
分类
0
首页
音乐
照片
友链
说说
关于
LogoLucky数据结构:块链(C语言)
搜索
首页
音乐
照片
友链
说说
关于

数据结构:块链(C语言)

发表于2019-11-25|更新于2025-03-03
|总字数:1.3k|阅读时长:7分钟

经过了一些修改,已经能完成基本操作了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define chunksize 8
#define null 0


typedef struct chunk{
char ch[chunksize];
struct chunk *next;
}chunk;


typedef struct{
chunk *head,*tail;
int curlen;
}lstring;


//清空
void clear(lstring *s){
chunk *t,*p;
int i = 0;
t = s->head;

printf("当前块链长度为:%d\n",s->curlen);

for( ; s->head != s->tail ; i++){
p = t->next;
s->head = p;
free(t);
t = p;
}

free(s->head);
s->head = s->tail = null;
s->curlen = 0;

printf("清空成功,释放了%d个节点。\n",i+1);
printf("清空后块链长度为:%d\n",s->curlen);
}


//压缩清空
void clear1(lstring *s){
chunk *t,*p;
t = s->head;

for( ; s->head != s->tail ; ){
p = t->next;
s->head = p;
free(t);
t = p;
}

free(s->head);
s->head = s->tail = null;
s->curlen = 0;

}


//插入节点
void add0(lstring *s,char *a){
chunk *p;
p = (chunk *)malloc(sizeof(chunk));

s->head = s->tail = p;
for(int i = 0 ; i<chunksize ; i++){
p->ch[i] = a[i];
}
}


void add1(lstring *s,char *a){
chunk *p;
p = (chunk *)malloc(sizeof(chunk));

for(int i = 0 ; i<chunksize ; i++){
p->ch[i] = a[i];
}

s->tail->next = p;
s->tail = p;
}


//插入s块链
void strinsert(lstring *t,lstring s){
int a,long1,long2,aaa=0,cc,hpu,ruler;
char a1[] = {"########"},a2[] = {"########"};

chunk *lc,*ly,*lz,*lw,*lm;
lc = (chunk *)malloc(sizeof(chunk));
ly = (chunk *)malloc(sizeof(chunk));
lz = (chunk *)malloc(sizeof(chunk));
lm = lw = t->head ;
ruler = t->curlen +1;
printf("请输入您想插入到第几个字符前:(1~%d)\n",t->curlen+1);
scanf("%d",&a);
hpu = a;
if(a>0&&a<t->curlen+2)
{t->curlen += s.curlen;
a -= 1;
long1 = a/chunksize+1;
long2 = a%chunksize;
for(int i = 0 ;long1>2&&i < long1 - 2 ; i++){
lz = lm->next;
lm = lm->next;
}
for(int i = 0 ; i < long1 - 1 ; i++){

lc = lw->next;
lw = lw->next ;
}
//for(int i=0;i<8;i++)
//printf("%c\n",lz->ch[i]);
//for(int i=0;i<8;i++)
//printf("%c\n",lc->ch[i]);
if(!(long1-1))
lc = t->head;
for(int j = 0 ; j < long2 ; j++)
{a1[j] = lc->ch[j];
aaa++;}

cc = aaa;
for(int j = 0 ; j < 8-cc ; j++)
{a2[j] = lc->ch[aaa];
aaa++;}

for(int w = 0 ; w<8 ; w++)
lc->ch[w] = a1[w];

ly->next = lc->next;
lc->next = ly;

for(int w = 0 ; w<8 ; w++)
ly->ch[w] = a2[w];

if(t->head == t->tail)
t->tail=ly;
if(lc == t->tail)
t->tail = ly;
if(hpu==1){
s.tail->next = ly;
t->head = s.head;
}
else if(hpu==9){
s.tail ->next = ly;
t->head->next =s.head ;
}

else if(hpu%8==1){
s.tail->next = ly ;
lz->next = s.head ;
}
else{
s.tail->next= lc->next;
lc->next=s.head;
}
if(hpu==ruler){
t->tail = s.tail ;
s.tail->next = null;
free(ly);
}
}
else
{printf("超范围了,请重新输入\n");
strinsert(t,s); }
}


//将数组中的元素插入块链
void strassign(lstring *s,char *str){
int width,long1,long2,aaa=0;
chunk *t,*p;
char a[chunksize];

t=(chunk *)malloc(sizeof(chunk));
p=(chunk *)malloc(sizeof(chunk));

width=strlen(str);
s->curlen=width;
long1=width/chunksize;
long2=width%chunksize;

if(long1!=0){
for(int i=0;i<long1;i++){
for(int j=0;j<chunksize;j++){
a[j]=str[aaa];
aaa++;
}
if(i==0)
add0(s,a);
else
add1(s,a);
}

if(long2){
for(int i=0;i<chunksize;i++){
if(i<long2)
{a[i]=str[aaa];
aaa++;}
else
a[i]='#';
}
add1(s,a);
}
}
else
{for(int i=0;i<chunksize;i++){
if(i<long2)
{a[i]=str[aaa];
aaa++;}
else
a[i]='#';
}
add0(s,a);}
}


//删除#号
void zip(lstring *t){
chunk *p;
char bbbb[100];
int i=0;
p=t->head;
while(!(p==t->tail)){
for(int j=0;j<8;j++){
if(p->ch[j]!='#')
{bbbb[i]=p->ch[j];
i++;}
}
p=p->next;
}
for(int j=0;j<8;j++){
if(p->ch[j]!='#')
{bbbb[i]=p->ch[j];i++;}}
bbbb[i]='\0';
clear1(t);
strassign(t,bbbb);
}


//显示块链
void appear(lstring s){
chunk *t;
t=s.head;
while(!(t==s.tail)){
for(int jj=0;jj<8;jj++){
printf("%c",t->ch[jj]);
}
printf(" ");
t=t->next;
}
if(s.curlen){
for(int jj=0;jj<8;jj++){
printf("%c",t->ch[jj]);
}}
else
printf("块链内无元素");
printf("\n");
}


int main(){
char str[100],str1[30];
int ruler;
lstring t,s;

printf("1:显示块链\n2:清空块链\n3:插入块链\n4:压缩块链\n5:清空原块链后,创建一个新块链\n\n");
printf("\n----------------------------------------\n\n");

printf("请输入想创建串内的元素(不包括#):\n");
gets(str);
strassign(&t,str);
printf("\n创建成功,创建后的块链为:\n");
appear(t);
printf("\n----------------------------------------\n\n");
do{
printf("请输入你想操作的数字:");
scanf("%d",&ruler);

if(ruler==1)
appear(t);

else if(ruler==2)
clear(&t);

else if(ruler==3)
{if(t.curlen){
printf("输入您想插入的字符串:\n");
fflush(stdin);
gets(str1);
strassign(&s,str1);
strinsert(&t,s);
printf("\n插入成功,插入后的块链为:\n");
appear(t);
}
else
printf("主串内无元素,无法插入\n");
}

else if(ruler==4)
{zip(&t);
printf("\n压缩成功,压缩后的块链为:\n");
appear(t);}

else
{printf("请输入想新创建串内的元素(不包括#):\n");
fflush(stdin);
gets(str);
strassign(&t,str);
printf("\n新创建成功,创建后的块链为:\n");
appear(t);}
printf("\n----------------------------------------\n\n");
}while(ruler);
}
文章作者: 刘同学
文章链接: https://mouhorse.github.io/2019-11-25/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%9A%E5%9D%97%E9%93%BE%EF%BC%88C%E8%AF%AD%E8%A8%80%EF%BC%89/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Lucky!
数据结构
赞助
  • wechat
    wechat
  • alipay
    alipay
cover of previous post
上一篇
数据结构:链表(C语言)
...
cover of next post
下一篇
数据结构:图(C语言)
...
相关推荐
cover
2019-12-12
数据结构:图(C语言)
...
cover
2019-10-26
数据结构:顺序表(c语言)
...
cover
2019-10-29
数据结构:链表(C语言)
...
avatar
刘同学
欢迎光临我的博客
文章
23
标签
8
分类
0
Follow Me
公告
欢迎来到我的博客!
可以交换友链
联系方式:485182274@qq.com
最新文章
Hexo本地与云端布局不同处理办法
Hexo本地与云端布局不同处理办法2025-02-25
解决 Hexo 部署到 GitHub Pages 自定义域名失效
解决 Hexo 部署到 GitHub Pages 自定义域名失效2025-02-24
Butterfly 个性化配置教程
Butterfly 个性化配置教程2025-02-23
Hexo安装并修改主题
Hexo安装并修改主题2025-02-23
MNE脑电预处理
MNE脑电预处理2024-10-07
©2018 - 2025 By 刘同学
框架 Hexo 7.3.0|主题 Butterfly 5.3.3
活出个样子给自己看
搜索
数据加载中