|
#include
#include
#include
using namespace std;
/* Define class A */
class A{
private:
struct Node{
char data;
Node *child; // Mang con tro
};
struct Node *root;
public:
A();
void Add(char);
void Print() const;
};
/* Define method A */
A::A():root(NULL){}
void A::Print() const{
struct Node *cur = new Node;
for(cur = root; cur != NULL; cur = cur->child){
cout<<cur->data<<" ";
}
cout<<endl;
}
void A::Add(char data){
if(root == NULL){
root = new Node;
root->child = NULL;
root->data = 0;
}
struct Node *newPtr = new Node;
newPtr->data = data;
newPtr->child = NULL;
Node *cur = root;
while(cur) {
if(cur->child == NULL) {
cur->child = newPtr;
return;
}
cur = cur->child;
}
}
/** Main **/
int main(){
A a;
a.Add('M');
a.Add('a');
a.Add('t');
a.Add('h');
a.Add('H');
a.Add('o');
a.Add('a');
a.Add('n');
a.Add('g');
a.Print();
return 0;
}
|
Linked list cơ bản sử dụng C++.
Output:
--
Mathhoang
vietnam_hoangminhnguyen
No comments:
Post a Comment