线性表作业-字符串

1. 写在前面

这里的代码不是源代码。

2. 题目要求

给定字符串用数组链表表示,每个链表节点有4个字符的空间。求:Index():确定模式串在第几块的第几个位置;Substr(m,n):求从m位开始n长度的字符串。

3. 代码

3.1 结构体以及函数声明

//用#表示空元素
typedef struct node
{
    char data[4];
    struct node *next;
} Node;
Node *Create(Node *head, char str[], int lens); //创建串
void Display(Node *head); //打印块链形式的串
void Index(Node *head, char dst[]);
void Substr(Node *head, int m, int n);

3.2 主函数

void main()
{
    //char str[];
    //char dst[];
    int lens=strlen(str);
    Node *head=Create(head, str, lens);
    Display(head);
    Index(head, dst);
    Substr(head, m, n);
}

3.3 将字符串储存在数组链表中

Node *Create(Node *head, char str[], int lens)
{
    //num为链表节点个数
    if(lens%4!=0)
        num=lens/4+1;
    else
        num=lens/4;
    head=(Node *)malloc(sizeof(Node));
    head->next=NULL;
    Node *temp=head;
    for(i=0; i<num; i++)
    {
        for(j=0; j<4; j++)
        {
            if(4*i+j<lens)
                temp->data[j]=str[4*i+j];
            else
                temp->data[j]='#'; //不足4个的块里的空元素用#
        }
        if(4*i+j<lens) //新建链表节点
        {
            Node *just=(Node*)malloc(sizeof(Node));
            just->next=NULL;
            temp->next=just;
            temp=just;
        }
    }
    return head;
}

3.4 打印

void Display(Node *head)
{
    Node *temp=head;
    while(temp)
    {
        for(i=0; i<4; i++)
            printf(temp->data[i]);
        temp=temp->next;
    }
}

3.5 Index()

​
void Index(Node *head, char dst[])
{
    int lend=strlen(dst);
    Node *temp=head;
    Node *tar=temp; //初始位置的标志
    int tsite=1;
    while(j<lend)
    {
        if(temp->data[i]==dst[j])
        {
            j++;
            i++;
        }
        //失败则回退到初始位置的下一个字符
        else
        {
            temp=tar;
            tsite=tsite+1;
            k=k+1;
            i=k;
            j=0;
        }
        if(i>3) //移位
        {
            temp=temp->next;
            i=0;
        }
        if(k>3) //回退位置变为下一个链表节点
        {
            tar=tar->next;
            k=0;
        }
    }
    //确定lump和site的值
    if(tsite%4==0)
    {
        lump=tsite/4;
        site=3;
    }
    else
    {
        lump=tsite/4+1;
        site=tsite%4-1;
    }
    printf("首位置在第%d块(从1开始)中,下标(从0开始)为%d处\n", lump, site);
}

​

3.6 Substr()

void Substr(Node *head, int m, int n)
{
    Node *temp=head;
    char clu[];
    if((m+1)%4==0)
        cou=(m+1)/4;
    else
        cou=(m+1)/4+1;
    for(k=1; k<cou; k++) //移动到cou对应的块
        temp=temp->next;
    i=m%4;
    for(k=0; k<n; k++)
    {
        if(temp->data[i]!='#')
        {
            clu[j]=temp->data[i];
            i++;
            j++;
            if(i==4)
            {
                temp=temp->next;
                i=0;
            }
        }
    }
    puts(clu);
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值