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

编程入门 行业动态 更新时间:2024-10-28 11:32:26

链块串的实现(无功能<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数实现)"/>

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

String.h

#pragma once
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <iostream>
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);

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;
}

更多推荐

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

本文发布于:2023-12-06 04:14:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1666402.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   功能   链块串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!