2021-10-5【数据结构/严蔚敏】【串的堆分配存储表示】【代码实现算法4.4实现】

线性表

栈&队列

二叉树

持续更新中,尽请期待!!!

#include<bits/stdc++.h>
using namespace std;
#define Status int
//---------串的堆分配存储表示--------------------
typedef struct{
	char *ch;//按照需要分配内存
	int length;//串长度
}HString;
//---------基本操作的函数原型说明-----------------
Status StrAssign(HString &T,char *chars){
	int i;				//i为chars的长度
	char *c;
    for (i = 0, c = chars; *c; i++, c++);
    if (!i){
        T.ch = NULL;
        T.length = 0;
    }
	else{
        T.ch = (char *)malloc(sizeof(char) * i);
        if (!T.ch)
            exit(_OVERFLOW);
        int tempi = 0;
        char *head = T.ch;
        do
        {
            tempi++;
            *T.ch = *chars;
            T.ch++;
            chars++;

        } while (tempi != i);
        T.ch = head;
        T.length = i;
        return 1;
	}
}

Status StrLength(HString S){
	return S.length;
}

Status StrCompare(HString S,HString T){
    for (int i = 0; i < S.length && i < T.length; i++)
        if (S.ch[i] != T.ch[i])
            return S.ch[i] - T.ch[i];
    return S.length - T.length;
}

Status ClearString(HString &S){
	if(S.ch){
        free(S.ch);
        S.ch = NULL;
    }
    S.length = 0;
    return 1;
}

Status Concat(HString &T,HString S1,HString S2)
{
	if (T.ch){
		free(T.ch);
	}
    T.length = StrLength(S1) + StrLength(S2);
    T.ch = (char *)malloc(sizeof(char) * T.length);
    if (!T.ch){
        exit(_OVERFLOW);
    }
    char *head = T.ch;
    for (int i = 0; i < StrLength(S1); i++){
        *T.ch = *S1.ch;
        T.ch++;
        S1.ch++;
	}
    for (int i = StrLength(S1) - 1; i < T.length; i++){
        *T.ch = *S2.ch;
        T.ch++;
		S2.ch++;
	}
    T.ch = head;
    return 1;
}

Status SubString(HString &Sub,HString S,int pos,int len){
    if (pos < 1 || pos > S.length || len < 0 || len > S.length - pos + 1)
        return 0;
    if (Sub.ch)
        free(Sub.ch);
    if (!len){
        Sub.ch = NULL;
        Sub.length = 0;
        return 1;
    }else{
        Sub.ch = (char *)malloc(sizeof(char) * len);
        Sub.length = len;
        char *head = Sub.ch;
        S.ch += pos - 1;
        for (int i=0;i<len;i++){
            *Sub.ch = *S.ch;
            Sub.ch++;
			S.ch++;
        }
        Sub.ch = head;
        return 1;
    }
}
//算法4.4
Status StrInsert(HString &S,int pos,HString T){
    int i;
    if (pos < 1 || pos > S.length + 1)
        return 0;
    if(T.length){
        if(!(S.ch=(char*)realloc(S.ch,(S.length+T.length)*sizeof(char))))
            exit(_OVERFLOW);
        for (i = S.length - 1; i >= pos - 1;i--)
            S.ch[i + T.length] = S.ch[i];
        for (i = 0; i <= T.length - 1; i++)
            S.ch[i + pos - 1] = T.ch[i];
        S.length += T.length;
    }
    return 1;
}

void prinfStr(HString S)
{
	for (int i=0;i<S.length;i++)
	{
		printf("%c",*S.ch);
		S.ch++;
	}
	printf("\n");
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
清华大学出版社出版的《数据结构(C语言版)》是清华大学计算机科学与技术系教材严蔚敏编写的经典教材。全书包含了数据结构的基本概念和常用算法的详细介绍,并提供了相应的代码实现。 《数据结构(C语言版)》的代码实现主要示例采用了C语言。全书共有12章内容,包括线性表、栈和队列、、树与二叉树、图、查找、排序等。每一章节都配备了大量的算法示例和相应的C语言代码实现。下面以线性表为例简要介绍一下书中代码实现: 在第2章“线性表”的内容中,书中详细介绍了线性表的定义、基本操作以及线性表的顺序存储结构和链式存储结构的实现方法。 在顺序存储结构部分,书中给出了线性表的初始化、插入、删除、查找等基本操作的代码实现。例如,线性表的初始化操作可以通过以下C语言代码实现: ```c #define MAXSIZE 100 // 定义线性表的最大长度 typedef struct { int data[MAXSIZE]; // 存储数据元素的数组 int length; // 线性表的当前长度 } SqList; void InitList(SqList *L) { L->length = 0; // 初始化线性表长度为0 } ``` 在链式存储结构部分,书中介绍了线性表的链式存储结构以及常见的链表类型,如单链表、静态链表和循环链表。对于单链表,书中给出了插入、删除、查找等操作的代码实现。例如,线性表的插入操作可以通过以下C语言代码实现: ```c typedef struct Node { int data; // 数据域 struct Node *next; // 指针域 } Node; // 在第i个位置插入元素 int InsertList(Node *head, int i, int x) { Node *pre = head; // pre指向头结点 int j = 0; while (pre != NULL && j < i - 1) { // 遍历找到第i-1个结点 pre = pre->next; j++; } if (pre == NULL || j > i - 1) { return 0; // 位置错误返回0 } Node *new_node = (Node *)malloc(sizeof(Node)); new_node->data = x; new_node->next = pre->next; pre->next = new_node; return 1; } ``` 以上只是《数据结构(C语言版)》中部分代码实现的简单例子,全书还包含很多其他章节的代码实现。读者可以通过阅读这本教材更全面地了解数据结构的概念和常用算法,并借助书中提供的代码实现进行实际操作和学习。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eternity_GQM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值