多字符节点的串的链式实现 concat不知道错误在哪
来源:互联网 发布:linux 内核启动顺序 编辑:程序博客网 时间:2024/04/30 01:16
#ifndef STR_H_INCLUDED
#define STR_H_INCLUDED
#define size 1
struct node
{
char data[size]; // data[0..size-1]
struct node*next;
};
typedef struct node * str;
str Initial(str t)//初始化 即滞空
{
return NULL;
}
str getstr(str t)//输入一个串
{
int i=0;//i<size用于记录当前字符在当前节点中的位置
char ch;//依次读取
str p,tmp;//p用来记录最后一个节点
printf("intput the string\n");
while ((ch=getchar())!='\n')
{
if (i%size!=0) //当i%size!=0即i为1..size-1时 直接在当前节点写入即可
{
p->data[i]=ch;
}
else
{
i=i%size; //当i到达size时,尾节点满了。需要申请新结点。i复原到0
tmp = (str)malloc(sizeof(struct node)); //新节点的赋值
tmp->data[i]=ch;
tmp->next=NULL;
if (!t) //第一次 t为空 直接赋值。p记录最后一个节点 即为t
{
t=tmp;
p=t;
}
else //t不为空时 从队尾加入新节点。同时 p保持记录队尾节点。
{
p->next = tmp;
p=p->next;
}
}
i++;//无论怎样 每次处理完i都向后移动
}
if (i%size) p->data[i]='#';//在最后加入标记
return t;
}
int length(str t)//返回串的长度
{
int len;
str p;
for (p=t,len=0;p;)
{
if (p->data[len%size]=='#') break;
len++;
if (len%size==0) p=p->next;
}
return len;
}
void Print(str t)//输出串
{
printf("string has %d elements.\n",length(t));
str p;
int i;
for (p=t,i=0;p;) //
{
if (p->data[i]=='#') break;
printf("%c",p->data[i]);
i++;
if (i%size==0)
{
i=i%size;
p=p->next;
}
}
printf("\n\n");
}
str clear(str t) //清除串的内容
{
str p=t;
while (t)
{
p=t;
t=t->next;
free(p);
}
return t;
}
str concat(str dst,str a,str b) //dst=a+b
{
if (!dst) dst=clear(dst);
str p,tmp;
int i;
int flag=0;
for (i=0,p=dst;a;)
{
if (a->data[i]=='#')break;
if (i%size==0)//需要申请新节点 同时如果!(a==a&&i==0) a=a->next
{
if (flag==1) a=a->next;
flag=1;
i=i%size;
tmp=(str)malloc(sizeof(struct node));
tmp->data[i]=a->data[i];
tmp->next = NULL;
if (!dst)
{
dst=tmp;
p=dst;
}
else
{
p->next=tmp;
p=p->next;
}
}
else
{
p->data[i]=a->data[i];
}
i++;
}
int bi;
flag=0;
for (bi=0;b;)
{
if (b->data[bi]=='#')break;
if (bi%size==0)
{
bi%=size;
if (flag==1) b=b->next;
flag=1;
}
if (i%size==0)//需要申请新节点 同时如果!(a==a&&i==0) a=a->next
{
i=i%size;
tmp=(str)malloc(sizeof(struct node));
tmp->data[i]=b->data[bi];
tmp->next = NULL;
if (!dst)
{
dst=tmp;
p=dst;
}
else
{
p->next=tmp;
p=p->next;
}
}
else
{
p->data[i]=b->data[i];
}
i++;
bi++;
}
if(i%size) p->data[i]='#';
return dst;
}
#define STR_H_INCLUDED
#define size 1
struct node
{
char data[size]; // data[0..size-1]
struct node*next;
};
typedef struct node * str;
str Initial(str t)//初始化 即滞空
{
return NULL;
}
str getstr(str t)//输入一个串
{
int i=0;//i<size用于记录当前字符在当前节点中的位置
char ch;//依次读取
str p,tmp;//p用来记录最后一个节点
printf("intput the string\n");
while ((ch=getchar())!='\n')
{
if (i%size!=0) //当i%size!=0即i为1..size-1时 直接在当前节点写入即可
{
p->data[i]=ch;
}
else
{
i=i%size; //当i到达size时,尾节点满了。需要申请新结点。i复原到0
tmp = (str)malloc(sizeof(struct node)); //新节点的赋值
tmp->data[i]=ch;
tmp->next=NULL;
if (!t) //第一次 t为空 直接赋值。p记录最后一个节点 即为t
{
t=tmp;
p=t;
}
else //t不为空时 从队尾加入新节点。同时 p保持记录队尾节点。
{
p->next = tmp;
p=p->next;
}
}
i++;//无论怎样 每次处理完i都向后移动
}
if (i%size) p->data[i]='#';//在最后加入标记
return t;
}
int length(str t)//返回串的长度
{
int len;
str p;
for (p=t,len=0;p;)
{
if (p->data[len%size]=='#') break;
len++;
if (len%size==0) p=p->next;
}
return len;
}
void Print(str t)//输出串
{
printf("string has %d elements.\n",length(t));
str p;
int i;
for (p=t,i=0;p;) //
{
if (p->data[i]=='#') break;
printf("%c",p->data[i]);
i++;
if (i%size==0)
{
i=i%size;
p=p->next;
}
}
printf("\n\n");
}
str clear(str t) //清除串的内容
{
str p=t;
while (t)
{
p=t;
t=t->next;
free(p);
}
return t;
}
str concat(str dst,str a,str b) //dst=a+b
{
if (!dst) dst=clear(dst);
str p,tmp;
int i;
int flag=0;
for (i=0,p=dst;a;)
{
if (a->data[i]=='#')break;
if (i%size==0)//需要申请新节点 同时如果!(a==a&&i==0) a=a->next
{
if (flag==1) a=a->next;
flag=1;
i=i%size;
tmp=(str)malloc(sizeof(struct node));
tmp->data[i]=a->data[i];
tmp->next = NULL;
if (!dst)
{
dst=tmp;
p=dst;
}
else
{
p->next=tmp;
p=p->next;
}
}
else
{
p->data[i]=a->data[i];
}
i++;
}
int bi;
flag=0;
for (bi=0;b;)
{
if (b->data[bi]=='#')break;
if (bi%size==0)
{
bi%=size;
if (flag==1) b=b->next;
flag=1;
}
if (i%size==0)//需要申请新节点 同时如果!(a==a&&i==0) a=a->next
{
i=i%size;
tmp=(str)malloc(sizeof(struct node));
tmp->data[i]=b->data[bi];
tmp->next = NULL;
if (!dst)
{
dst=tmp;
p=dst;
}
else
{
p->next=tmp;
p=p->next;
}
}
else
{
p->data[i]=b->data[i];
}
i++;
bi++;
}
if(i%size) p->data[i]='#';
return dst;
}
#endif // STR_H_INCLUDED
测试程序
#include <stdio.h>
#include <stdlib.h>
#include "str.h"
int main()
{
str t,a,b;
a=b=t=NULL;
while (1)
{
a=getstr(a);
Print(a);
b=getstr(b);
Print(b);
t=concat(t,a,b);
printf("000");
Print(t);
clear(t);
}
return 0;
}
- 多字符节点的串的链式实现 concat不知道错误在哪
- 成功实现多字符节点的串的合并操作
- 不知道在哪里下载的点我!
- 多字符串的实现
- struts中actionservlet有错误,不知道是哪的?
- VC6.0下一个不知道的错误
- Open Inventor_分隔符节点
- 不知道的不知道
- 在编译VS2005时出现的错误 不知道怎么解决 求解 谢谢
- Windows 使用技巧. (不知道在哪裡找到的)
- TensorFlow版本带来的concat错误
- 你不知道的TextView实现原理
- 10件你不知道的apt那点事儿
- 关于面试你不知道的几点
- 关于面试你不知道的几点
- 关于点分治的一些不知道算什么的东西
- 你可能不知道的RoR 5点技巧
- 链式实现的堆栈
- Unity3d动态数据管理(1)Export AssetBundles
- 强大的EA该让我如何说起
- 常用容器API
- Python 简明教程
- C语言的一些误用和知识总结
- 多字符节点的串的链式实现 concat不知道错误在哪
- unity 3d快速使用KGFMapSystem开发小地图
- 遥感知识
- poj 3349 hash
- Dinic模板
- [Boost基础]内存管理——内存池pool库
- Android Application 对象介绍
- java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class serv
- 黑马程序员-------银行业务调度系统