字符串块链的实现

字符串块链的实现

实验要求:
需要实现的算法及功能
 void strassign(lstring &T,char *str)
     将一个不包含’#’字符串str赋值到块链类型变量T中 
void clrstring(lstring &T)
     将一个块链T中的所有结点释放,最终使得T.head=T.tail=null     T.curlen=0 
void strprint(lstring T)
     打印一个存放在块链T中的字符串
void strinsert(lstring &T,int pos,lstring S)
   将块链S插入到块链T中第pos个字符之前
void zip(listrng &T)
   将块链T中间的’#’去掉,重新放入块链T中

刚开始拿到这个实验,感觉是真的难!!!话不多说,直接上程序

#define chunksize 8					//定义块的大小 
#include "stdlib.h"
#include "stdio.h"
#include "string.h"

typedef struct chunk {
	char ch[chunksize];				//块的数据域
	struct chunk *next;				//块的指针域
} chunk; 							//块链结点类型
typedef struct {
	chunk *head,*tail; 				//块链指向头尾结点的指针
	int curlen; 					//块链中串的长度
} lstring; 							//块链类型

void strassign(lstring &T,char *str) {	//将一个不包含’#’字符串str赋值到块链类型变量T中
	chunk *aa;
	int n1, n2;
	int len;
	aa = new chunk;
	T.tail = T.head = aa;
	T.head->next = T.tail->next = NULL;
	len = strlen(str);					//定义串的长度
	T.curlen = len;
	n1 = len / 8 ;
	int o;
	o = n1;
	n2 = len % 8;
	int cn = 0;
	int y;
	y = 0;
	int ff;
	if(o != 0) {
		while(n1--) {
			if(cn == 0) {
				for(int i = 0; i < 8; i++) {
					T.tail->ch[i] = str[y];
					y++;
				}
				cn = 1;
			} else {
				aa = new chunk;
				T.tail->next = aa;
				T.tail = aa;
				for(int i = 0; i < 8; i++) {
					T.tail->ch[i] = str[y];
					y++;
				}
			}
		}
		if(n2 != 0) {						//不是8的整数倍
			aa = new chunk;
			T.tail->next = aa;
			T.tail = aa;
			for(int i = 0; i < 8; i++) {
				if(i < n2) {
					T.tail->ch[i] = str[y];
					y++;
				} else {
					T.tail->ch[i] = '#';
				}
			}
		}
		T.tail->next = NULL;
	} else {
		for(int i = 0; i < 8; i++) {
			if(i < n2) {
				T.tail->ch[i] = str[y];
				y++;
			} else {
				T.tail->ch[i] = '#';
			}
		}
		T.tail->next = NULL;
	}
}

void clrstring(lstring &T) {		//将一个块链T中的所有结点释放
	chunk *aa;
	while(T.head != 0) {
		aa = T.head;
		free(T.head);
		T.head = aa;
	}
	T.tail = T.head = NULL;
	T.curlen = 0;
}

void strprint(lstring T) { //输出
	chunk *bb;
	int yy = 0;
	bb = T.head;
	while(bb != NULL) {
		if(yy == 0) {
			for(int i = 0; i < 8; i++) {
				printf("%c",bb->ch[i]);
			}
			yy = 1;
		} else {
			printf("->");
			for(int i = 0; i < 8; i++) {
				printf("%c",bb->ch[i]);
			}
		}
		bb = bb->next;
	}
}

void strinsert(lstring &T,int pos,lstring S) {	//将块链S插入到块链T中第pos个字符之前
	chunk *pp;
	pp = T.head;
	int qyx = 0;
	while(pp != NULL) {	//设置qyx
		pp = pp->next;
		qyx++;
	}
	if(pos == 1) {		//考虑边界
		S.tail->next = T.head;
		T.head = S.head;
		return;
	} else if(pos == qyx*8 + 1) {
		T.tail->next = S.head;
		T.tail = S.tail;
		return;
	} else {
		int z = 0;
		chunk *aa;
		aa = T.head;
		while(aa != NULL) {	//设置z
			aa = aa->next;
			z++;
		}
		if(pos <= 8) {
			chunk *c1,*c2;
			c1 = new chunk;
			c2 = new chunk;
			chunk *c11,*c22;
			c11 = c1;
			c22 = c2;
			for(int i =0; i < 8; i++) {
				c1->ch[i] = '#';
			}
			for(int i =0; i < 8; i++) {
				c2->ch[i] = '#';
			}
			int x = 0;
			int i;
			for(i = 0; i < pos -1; i++) {
				c11->ch[i] = T.head->ch[i];
			}
			for(; i<=7; i++) {
				c22->ch[x] = T.head->ch[i];
				x++;
			}
			chunk *rn;
			rn = T.head->next;
			T.head = c11;
			c11->next = S.head;
			S.tail->next = c22;
			c22->next = rn;
		} else if(pos >= (z-1)*8 +1 && pos <= z * 8) {
			int rr;
			rr = pos % 8;
			if(rr == 0) {
				rr = 8;
			}
			if(pos == (z-1) * 8 + 1) {
				chunk *op;
				op = T.head;
				while(op->next != T.tail) {
					op = op->next;
				}
				op->next = S.head;
				S.tail->next = T.tail;
				return;
			} else {
				chunk *op;
				op = T.head;
				while(op->next != T.tail) {
					op = op->next;
				}
				chunk *c1,*c2;
				c1 = new chunk;
				c2 = new chunk;
				chunk *c11,*c22;
				c11 = c1;
				c22 = c2;
				for(int i = 0; i < 8; i++) {
					c1->ch[i] = '#';
				}
				for(int i = 0; i < 8; i++) {
					c2->ch[i] = '#';
				}
				int x = 0;
				int i;
				for(i = 0; i < 8; i++) {
					c11->ch[i] = T.tail->ch[i];
				}
				for(; i<=7; i++) {
					c22->ch[x] = T.tail->ch[i];
					x++;
				}
				op->next = c11;
				c11->next = S.head;
				S.tail->next = c22;
				T.tail = c22;
				T.tail->next = NULL;
				return;
			}
		} else {
			int rr;
			rr = pos % 8;
			if(rr == 0) {
				rr = 8;
			}
			int ee = 0;
			chunk *w1,*w2;
			w2 = w1 = T.head;
			while(pos-8 > ee) {
				ee += 8;
				w2 = w1;
				w1 = w1->next;
			}
			if(rr == 1) {
				w2->next = S.head;
				S.tail->next = w1;
			} else {
				chunk *c1,*c2;
				c1 = new chunk;
				c2 = new chunk;
				chunk *c11,*c22;
				c11 = c1;
				c22 = c2;
				for(int i = 0; i < 8; i++) {
					c1->ch[i]='#';
				}
				for(int i=0; i<8; i++) {
					c2->ch[i]='#';
				}
				int x= 0;
				int i;
				for(i = 0; i < rr-1; i++) {
					c11->ch[i] = w1->ch[i];
				}
				for(; i <= 7; i++) {
					c22->ch[x] = w1->ch[i];
					x++;
				}
				chunk *opop;
				opop = w1->next;
				w2->next = c11;
				c11->next = S.head;
				S.tail->next = c22;
				c22->next = opop;
				return;
			}
		}

	}
}

void zip(lstring T,char *map) {
	memset(map,'\0',sizeof(map));
	chunk *oo;
	oo=T.head;
	int you=0;
	while(oo!=NULL) {
		for(int i=0; i<8; i++) {
			if(oo->ch[i]>='a'&& oo->ch[i]<='z') {
				map[you++]=oo->ch[i];
				printf("%c",oo->ch[i]);
			}
		}
		oo=oo->next;
	}
}

int main() {
	char s[100];
	lstring T1,T2,T3;
	int len;
	printf("\n请输入第一个长度不超过100个字符的字符串(不包含字符'#'):\n");
	gets(s);
	len = strlen(s);
	/*if(len%8 != 0) {
		len=(len/8 + 1) * 8;
	}*/
	strassign(T1,s);
	printf("\n第一次输入时形成的链表,表示如下:\n");
	strprint(T1);
	printf("\n请输入第二个长度不超过100个字符的字符串(不包含字符'#'):\n");
	gets(s);
	strassign(T2,s);
	printf("\n第二次输入时形成的链表,表示如下:\n");
	strprint(T2);
	int k;
	printf("\n请输入您需要插入的位置(在第几位之前插入取值范围在1~%d):",len+1);
	scanf("%d",&k);
	while(1) {
		if(k<1 || k > len+1) {
			printf("插入错误,请重新输入插入位置\n");
			scanf("%d",&k);
		} else {
			break;
		}
	}
	strinsert(T1,k,T2);
	printf("\n输出压缩后的字符串:\n");
	char map[200];
	zip(T1,map);
	strassign(T3,map);
	printf("\n压缩形成的链表,表示如下:\n");
	strprint(T3);
}

运行结果如下:
在这里插入图片描述输入的字符串只要保证输入的都是字符就行了,随便输入,这个实验是真难!真难!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值