#include <iostream> using namespace std; #define Maxsize 200 #define ERROR 0 typedef struct Node { char data; struct Node* next; int length; } Linklist; Linklist* Initlist() { Linklist* T; T = new Linklist; T->next = NULL; T->length = 0; return T; } void StrAssign(Linklist*& T, char str[]) { T = Initlist(); Linklist* p = T; for (int i = 0; str[i] != '\0'; i++) { Linklist* s = new Linklist; s->data = str[i]; p->next = s; p = s; T->length++; } p->next = NULL; } Linklist* Concat(Linklist* S1, Linklist* S2) { Linklist* p1, * p2, * L, * p, * r; int len1 = 0, len2 = 0; L = Initlist(); r = L; p1 = S1->next; while (p1) { p = new Linklist; p->data = p1->data; p1 = p1->next; p->next = NULL; r->next = p; r = p; len1++; } r->next = NULL; p2 = S2->next; while (p2) { p = new Linklist; p->data = p2->data; p2 = p2->next; p->next = NULL; r->next = p; r = p; len2++; } L->length = len1 + len2; return L; } Linklist* Substr(Linklist* L, int pos, int len) { if (pos > L->length || pos < 1 || pos + len > L->length || len < 0) return ERROR; Linklist* p, * r, * l, * rr; l = Initlist(); r = L->next; rr = l; for (int i = 1; i < pos; i++) r = r->next; for (int i = 0; i < len; i++) { p = new Linklist; p->data = r->data; r = r->next; p->next = NULL; rr->next = p; rr = p; } l->length = len; return l; } void Replace(Linklist* L, Linklist T, Linklist V) { L = L->next; Linklist* headT = T.next; while (L) { if (L->data == headT->data) { Linklist* t = headT->next; Linklist* l = L->next; for () } } } void print(Linklist* L) { Linklist* p = L->next; while (p) { cout << p->data; p = p->next; }
时间: 2024-01-10 10:03:25 浏览: 80
#include_iostream.h_
4星 · 用户满意度95%
这段代码是关于单链表的字符串操作,包括字符串初始化、拼接、子串截取和替换。其中,Initlist()函数用于初始化一个链表,StrAssign()函数用于将一个字符数组转化成链表存储,Concat()函数用于将两个链表拼接成一个新的链表,Substr()函数用于截取一个链表的子串,Replace()函数用于将链表中的某个子串替换成另一个链表。print()函数用于输出链表的内容。
阅读全文