成功实现多字符节点的串的合并操作

来源:互联网 发布:线切割锥度3b编程实例 编辑:程序博客网 时间:2024/04/28 23:06

#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;//p记录dst的最后一个节点  tmp是新申请


    int i,k,lena,lenb;
    lena=length(a);
    lenb=length(b);


    for(i=0,k=0;i<lena;i++,k++)              //i用来记录a或b的节点内位置。k对应dst
    {
        if(i>0&&i%size==0) a=a->next;//a节点位置后移
        if(k%size==0)//需要申请新节点
        {
            k%=size;
            tmp=(str)malloc(sizeof(struct node));
            tmp->data[k]=a->data[i%size];//i没有进行%操作
            tmp->next=NULL;


            if(!dst){dst=tmp;p=dst;}
            else {p->next=tmp;p=p->next;}
        }
        else
        {
            p->data[k]=a->data[i%size];
        }
    }






    for(i=0;i<lenb;i++,k++)              //i记录b的节点内位置。k对应dst
    {
        if(i>0&&i%size==0) b=b->next;//a节点位置后移
        if(k%size==0)//需要申请新节点
        {
            k%=size;
            tmp=(str)malloc(sizeof(struct node));
            tmp->data[k]=b->data[i%size];
            tmp->next=NULL;


            if(!dst){dst=tmp;p=dst;}
            else {p->next=tmp;p=p->next;}
        }
        else
        {
            p->data[k]=b->data[i%size];
        }
    }


    if(i%size) p->data[i]='#';//此处有错误!i对应的是a或者b  你用i??是k!
    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);
        b=getstr(b);


        t=concat(t,a,b);
        Print(t);


        t=clear(t);
        a=clear(a);
        b=clear(b);
    }
    return 0;
}

原创粉丝点击