• 链块串的实现(无功能函数实现)


    String.h

    #pragma once
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    #define SIZE 5
    #define EFF_SIZE (SIZE -1)//插入的有效字符
    typedef char elemType;
    
    typedef struct node
    {
    	elemType str[SIZE];
    	struct node* next;
    }node;
    
    typedef struct String
    {
    	node* head;
    	node* tail;
    	int length;
    }String;
    
    //初始化串
    void initString(String* ps);
    
    //创建新节点
    node* newNode(int size, char* str);//参数1:传的字符个数
    
    //打印串
    void printString(const String* s);
    
    //输入数据
    void enterData(String* ps, char* ch);
    
    //销毁串
    void destroyString(String* ps);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    String.cpp

    #include "String.h"
    
    //初始化串
    void initString(String* ps)
    {
    	//创建一个带头的串
    	node* p = (node*)malloc(sizeof(node));
    	if (NULL == p)//申请内存是否失败
    	{
    		cout << strerror(errno) << endl;
    		exit(-1);
    	}
    	p->next = NULL;
    	ps->head = ps->tail = p;
    	ps->length = 0;
    }
    
    //创建新节点
    node* newNode(int size, char* str1)//参数1:传的字符个数
    {
    	node* p = (node*)malloc(sizeof(node));
    	if (NULL == p)//分配内存失败
    	{
    		cout << strerror(errno) << endl;
    		exit(-1);
    	}
    	for (int i = 0; i < EFF_SIZE; i++)
    	{
    		if (size <= i)
    		{
    			p->str[i] = '#';
    		}
    		else 
    		{
    			p->str[i] = str1[i];
    		}
    	}
    	p->str[EFF_SIZE] = '\0';
    	p->next = NULL;
    	return p;
    }
    
    //打印串
    void printString(const String* ps)
    {
    	assert(ps);
    	node* p = ps->head->next;
    	if (NULL == p)
    	{
    		cout << "串无数据" << endl;
    		return;
    	}
    	while (p)
    	{
    		cout << "[ ";
    		if (ps->tail == p)//判断是否为最后一个数据
    		{
    			for (int i = 0; i < SIZE; i++)
    			{
    				if ('#' == p->str[i])
    				{
    					break;
    				}
    				cout << p->str[i];
    			}
    		}
    		else
    		{
    			cout << p->str;
    		}
    		cout << " ]->";
    		p = p->next;
    	}
    	cout << "NULL" << endl;
    }
    
    //输入数据
    void enterData(String* ps, char* ch)
    {
    	assert(ps);
    	node* cur = ps->head;
    	int leng = strlen(ch);
    	ps->length = leng;
    	int n = leng / EFF_SIZE;
    	int count = 0;
    	if (0 != leng % EFF_SIZE)
    	{
    		count = leng - n * EFF_SIZE;
    		n++;
    	}
    	for (int i = 1; i <= n; i++)
    	{
    		node* p = NULL;
    		if (i == n && 0 != count)
    		{
    			p = newNode(count, ch + (i - 1) * EFF_SIZE);
    		}
    		else
    		{
    			p = newNode(EFF_SIZE, ch + (i - 1) * EFF_SIZE);
    		}
    		ps->tail = cur->next = p;
    		cur = p;
    	}
    }
    
    //销毁串
    void destroyString(String* ps)
    {
    	node* pcur = ps->head;
    	while (pcur)
    	{
    		node* pnext = pcur->next;
    		free(pcur);
    		pcur = pnext;
    	}
    	ps->head = ps->tail = NULL;
    	ps->length = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
  • 相关阅读:
    变年轻特效怎么制作?这三个方法你值得收藏
    pytest合集(8)— API Functions
    02-Java流程控制
    部署Prometheus
    AntDesignVue之a-table中key不唯一问题处理的多种方式
    【前端设计模式】之状态模式
    BLE学习(1):蓝牙协议栈的介绍
    解析csv文件 流数据问题
    springcloud-eureka快速入门
    HCIA VLAN间通信 多臂路由与单臂路由
  • 原文地址:https://blog.csdn.net/weixin_74814064/article/details/133958980