c++实现链串的完整代码
时间: 2023-06-01 21:01:26 浏览: 38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100
typedef struct node {
char data[MAX_LENGTH];
struct node *next;
} Node, *LinkStr;
// 初始化链串
void init(LinkStr *str) {
*str = (LinkStr) malloc(sizeof(Node));
(*str)->next = NULL;
}
// 判断链串是否为空
int is_empty(LinkStr str) {
return str->next == NULL;
}
// 插入操作,将字符串s插入到链串str的第i个位置
int insert(LinkStr str, int i, char *s) {
int j = 0, k;
LinkStr p, q;
p = str;
while (p && j < i - 1) {
p = p->next;
j++;
}
if (!p || j > i - 1) {
printf("插入位置有误!\n");
return 0;
}
k = strlen(s);
q = (LinkStr) malloc(sizeof(Node));
strncpy(q->data, s, k);
q->data[k] = '\0';
q->next = p->next;
p->next = q;
return 1;
}
// 删除操作,删除链串str的第i个位置开始的长度为len的子串
int delete(LinkStr str, int i, int len) {
int j = 0;
LinkStr p, q;
p = str;
while (p && j < i - 1) {
p = p->next;
j++;
}
if (!p || j > i - 1 || !p->next) {
printf("删除位置有误!\n");
return 0;
}
q = p->next;
j = 0;
while (q && j < len) {
q = q->next;
j++;
}
if (!q) {
printf("删除长度有误!\n");
return 0;
}
p->next = q->next;
free(q);
return 1;
}
// 求子串操作,将链串str中从第i个位置开始长度为len的子串赋值给sub
int substring(LinkStr str, int i, int len, LinkStr *sub) {
int j = 0;
LinkStr p, q, r;
init(sub);
p = str;
while (p && j < i - 1) {
p = p->next;
j++;
}
if (!p || j > i - 1) {
printf("求子串位置有误!\n");
return 0;
}
q = (*sub);
r = p->next;
j = 0;
while (r && j < len) {
q->next = (LinkStr) malloc(sizeof(Node));
strncpy(q->next->data, r->data, strlen(r->data));
q->next->data[strlen(r->data)] = '\0';
q->next->next = NULL;
q = q->next;
r = r->next;
j++;
}
return 1;
}
// 求链串长度
int length(LinkStr str) {
int len = 0;
LinkStr p = str->next;
while (p) {
len += strlen(p->data);
p = p->next;
}
return len;
}
// 比较操作,比较链串str1和str2的大小
int compare(LinkStr str1, LinkStr str2) {
LinkStr p = str1->next, q = str2->next;
while (p && q) {
if (strcmp(p->data, q->data) != 0) {
return strcmp(p->data, q->data);
}
p = p->next;
q = q->next;
}
if (p) {
return 1;
} else if (q) {
return -1;
} else {
return 0;
}
}
// 输出链串
void print(LinkStr str) {
LinkStr p = str->next;
while (p) {
printf("%s", p->data);
p = p->next;
}
printf("\n");
}
// 销毁链串
void destroy(LinkStr *str) {
LinkStr p = *str, q;
while (p) {
q = p;
p = p->next;
free(q);
}
*str = NULL;
}
int main() {
LinkStr str1, str2, sub;
init(&str1);
init(&str2);
init(&sub);
insert(str1, 1, "hello");
insert(str1, 2, "world");
insert(str2, 1, "hello");
insert(str2, 2, "world");
printf("str1: ");
print(str1);
printf("str2: ");
print(str2);
printf("str1和str2比较结果为:%d\n", compare(str1, str2));
printf("str1的长度为:%d\n", length(str1));
substring(str1, 2, 3, &sub);
printf("str1的第2个字符开始长度为3的子串为:");
print(sub);
delete(str1, 2, 3);
printf("删除str1的第2个字符开始长度为3的子串后,str1为:");
print(str1);
destroy(&str1);
destroy(&str2);
destroy(&sub);
return 0;
}
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)