C++ 字符串问题
说明一个string类的字符串,如何把每个字符作为一个节点以链表形式存储起来?请大侠指点,最好是有结构说明和一些简单的实现代码,3q3q3q··
说明一个string类的字符串,如何把每个字符作为一个节点以链表形式存储起来?请大侠指点,最好是有结构说明和一些简单的实现代码,3q3q3q··
答案://////////////////////////////////////////////////////////////////////////
//实现链表,单节点存储的是字符串类型的数据
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
struct NODE
{
string Name;
int Distance;
struct NODE* next;
};
void Init(NODE* L)
{
L->Name = "China";
L->Distance = 0;
L->next = NULL;
}
bool InsListHead(NODE* L, string name, int distance)
{
if (!L)
{
return false;
}
NODE* node = new NODE;
node->Name = name;
node->Distance = distance;
node->next = L->next;
L->next = node;
return true;
}
bool InsListTail(NODE* L, string name, int distance)
{
if (!L)
{
return false;
}
NODE* node = new NODE;
node->Name = name;
node->Distance = distance;
NODE* tmp = L;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
if (!tmp)
{
cout << "Insert Erorr position." << endl;
return false;
}
else
{
node->next = tmp->next;
tmp->next = node;
}
return true;
}
// Insert to the destination position
bool InsList(NODE* L, int i, string name, int diatance)
{
if (!L)
{
cout << "The List is NULL" << endl;
return false;
}
NODE* Pos = L;
int k = 0;
while (Pos->next != NULL && k < i - 1)
{
Pos = Pos->next;
k++;
}
if (!Pos)
{
cout << "Insert Erorr Position." << endl;
return false;
}
NODE* node = new NODE;
node->Name = name;
node->next = Pos->next;
Pos->next = node;
return true;
}
// Delete the node of destination
bool DelList(NODE* L, int i)
{
if (!L)
{
cout << "The list is NULL." << endl;
return false;
}
NODE* Pos = L;
int k = 0;
while (Pos->next != NULL && k < i-1)
{
Pos = Pos->next;
++k;
}
if (!(Pos->next))
{
cout << "Delete the erorr position." << endl;
return false;
}
NODE* tmp = Pos->next;
Pos->next = Pos->next->next;
delete tmp;
return true;
}
int ListLength(NODE* L)
{
if (!L)
{
cout << "The NODE is NULL!" << endl;
return 0;
}
int i = 1;
NODE* tmp = L;
while (tmp->next != NULL)
{
tmp = tmp->next;
++i;
}
return i;
}
NODE* Locate(NODE* L, string name)
{
NODE* node;
node = L->next;
int pos = 0;
while (node != NULL)
{
if (node->Name == name)
{
break;
}
else
{
node = node->next;
}
}
return node;
}
int main()
{
NODE* head = new NODE;
Init(head);
InsListTail(head, "Beijing", 10);
InsListTail(head, "ShangHai", 20);
InsListTail(head, "XiaMen", 30);
// 插入 XX 市到第二个位置上,删除第二个节点
InsList(head, 2, "XX", 10);
DelList(head, 2);
// 查找北京市,并且输出距离
string str = "Beijing";
if (!Locate(head, str))
{
cout << "can't the city of " << str << endl;
}
else
{
cout << "find successfully! the distance is " << Locate(head, str)->Distance << endl;
}
// 输出链表的长度
cout << "The length of head is " <<ListLength(head) << "." << endl;
system("pause");
return 0;
}
// 你可以运行试下,没有错误。struct Node{
char data;
Node* next;
};
Node* List(string str){
first->data=str.at(0);
Node* stateNode=first;
for(int i=1;i<str.length;i++){
temp=new Node;
temp->data=str.at(i);
stateNode->next=temp;
stateNode=stateNode->next;
}
stateNode->next=NULL;
return first;
}
Node* first;
Node* temp;
int main(void){
first=new Node;
first=List("hello,World1");
while(first->next!=NULL)
{
Node* temp=first;
cout<<temp->data;
first=first->next;
delete temp;
}
delete first;
}
专业人士呀 都看不懂哦