C++串-链式存储

LinkString.h

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
#pragma once
#include<iostream>
using namespace std;

struct Node
{
char data;
Node* next;
};

class LinkString
{
private:
Node* head;
public:
LinkString(const char *s);
Node* IndexOf(LinkString& s, Node* p = NULL);


// 将指字符串连接到原字符串的后面
void ConCat(LinkString& s);

bool Equals(LinkString& s);

// 返回第一个出现指定字符的节点
Node* IndexOf(const char &c);

// 替换所有出现过的字符串
void replace(const char& oldChar, const char& newChar);

void ToString();

};

LinkString.cpp

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
120
121
#include "LinkString.h"

LinkString::LinkString(const char* s)
{
head = NULL;
Node* tail = head;

for (int i = 0; s[i] != 0; i++)
{
Node* newNode = new Node();
newNode->data = s[i]; newNode->next = NULL;
if (head == NULL)
head = newNode;
else
tail->next = newNode;
tail = newNode;
}
}

Node* LinkString::IndexOf(LinkString& s, Node* sp)
{
Node* startP;
if (sp != NULL)
startP = sp;
else
startP = head;

Node* p = startP, * q = s.head;

while (p!=NULL && q!=NULL)
{
if (p->data == q->data)
p = p->next, q = q->next;
else
{
startP = startP->next;
p = startP, q = s.head;
}
}
if (q == NULL)
return startP;
else
return NULL;
}


bool LinkString::Equals(LinkString& s)
{
Node* p = head, * q = s.head;
while (p!=NULL && q!=NULL)
{
if (p->data != q->data)
break;
p = p->next, q = q->next;
}
if (p == NULL && q == NULL)
return true;
return false;
}

Node* LinkString::IndexOf(const char& c)
{
Node* p = head;
while (p!=NULL)
{
if (p->data == c)
return p;
p = p->next;
}
return NULL;
}

// 连接字符串
void LinkString::ConCat(LinkString& s)
{
Node* p = head, * q = s.head;
if (q == NULL) return;
Node* tail = p;
// 获取最后一个节点
while (p!=NULL)
{
tail = p;
p = p->next;
}

while (q!=NULL)
{
Node* newNode = new Node();
newNode->data = q->data;
if (head == NULL)
tail = newNode;
else
tail->next = newNode, tail = tail->next;

q = q->next;
}

}

void LinkString::replace(const char &oldChar, const char & newChar)
{
Node* p = head;
while (p!=NULL)
{
if (p->data == oldChar)
p->data = newChar;
p = p->next;
}
}

void LinkString::ToString()
{
Node* p = head;
while (p!=NULL)
{
cout << p->data;
p = p->next;
}
cout << endl;

}

main.cpp

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
#include<iostream>
using namespace std;
#include"LinkString.h"
int main()
{
LinkString str("I Ilke c++ I Ilke C++ I Ilke C++");
LinkString s("Ilke");

Node *p = str.IndexOf(s);
while (p!=NULL)
{
cout <<p<<":"<< p->data << endl;
p = str.IndexOf(s,p->next);
}

LinkString s1("hello");
LinkString s2("hello");

s1.ConCat(s2);
s1.ToString();

cout <<"s1 equals s2:"<< s1.Equals(s2) << endl;

Node* indexP = s1.IndexOf('e');
cout << "ָ֨ؖػޡ֣ú" << indexP << ":" << indexP->data << endl;

// ͦۻؖػ
LinkString str2("eeeeeeeeeeeeeeeee");
str2.replace('e', 'd');
str2.ToString();
return 0;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!