<ElemType>用法
时间: 2023-10-22 20:10:58 浏览: 76
<ElemType>是一种通用的占位符类型,常用于泛型编程中。在定义一个泛型数据结构或函数时,可以使用<ElemType>来表示该数据结构或函数可以处理的数据类型,这样就可以在不同的场景中使用相同的代码。
例如,定义一个泛型链表结构时,可以使用<ElemType>来表示链表中存储的元素类型:
```
template <typename ElemType>
struct ListNode {
ElemType val;
ListNode<ElemType> *next;
ListNode(ElemType x) : val(x), next(NULL) {}
};
```
在这个例子中,ListNode<ElemType>表示了一个包含元素类型为ElemType的节点的链表。这样,在使用这个链表结构时,只需要指定ElemType的具体类型即可:
```
ListNode<int> *head = new ListNode<int>(1);
```
这样就创建了一个存储int类型元素的链表。同样的,如果想创建一个存储string类型元素的链表,只需要将int替换成string即可:
```
ListNode<string> *head = new ListNode<string>("hello");
```
相关问题
//建立二叉树的存储结构 (外壳) template<class ElemType> void CreateTree(BinaryTree<ElemType> &T, ElemType &str, ElemType &empty){ ElemType tmp; vector<ElemType> t; stringstream input_T(str); while(input_T >> tmp){ t.push_back(tmp); } BinaryTreeNode<ElemType> *root; int num = 0; root = T.CreateBinaryTree(t, empty, num); T.SetRoot(root); }解读一下这个程序
这段程序是一个建立二叉树的存储结构的外壳,使用了模板类的方式来支持不同类型的元素。具体解释如下:
- `template<class ElemType>`:定义了一个模板类,其中 `ElemType` 表示元素的类型。
- `void CreateTree(BinaryTree<ElemType> &T, ElemType &str, ElemType &empty)`:定义了一个名为 `CreateTree` 的函数,它需要三个参数。第一个参数是一个二叉树对象 `T` 的引用,表示要构建的二叉树;第二个参数是一个字符串 `str` 的引用,表示二叉树的元素序列;第三个参数是一个空元素 `empty` 的引用,表示二叉树中的空节点。
- `ElemType tmp;`:定义了一个临时变量 `tmp`,用于将输入的字符串逐个读入到其中。
- `vector<ElemType> t;`:定义了一个向量 `t`,用于存储输入的元素序列。
- `stringstream input_T(str);`:定义了一个字符串输入流 `input_T`,用于将输入的字符串 `str` 转化为输入流,方便逐个读取其中的元素。
- `while(input_T >> tmp){t.push_back(tmp);}`:使用 `while` 循环逐个读取输入流中的元素,并将其存储到向量 `t` 中。
- `BinaryTreeNode<ElemType> *root;`:定义了一个指向二叉树节点的指针 `root`,用于存储二叉树的根节点。
- `int num = 0;`:定义了一个整型变量 `num`,用于记录当前正在处理的元素在向量 `t` 中的索引。
- `root = T.CreateBinaryTree(t, empty, num);`:调用二叉树对象 `T` 的 `CreateBinaryTree` 方法创建二叉树,并将返回的根节点指针存储到 `root` 变量中。
- `T.SetRoot(root);`:将二叉树的根节点设置为 `root`。
#include <iostream> #include <string> using namespace std; typedef string ElemType; typedef struct BitNode { ElemType data; struct BitNode *lchild, *rchild; } BitNode, *BiTree; void CreateBiTree(BiTree &t) { string ch; cin >> ch; if (ch[0] == '#') { t = NULL; } else { t = new BitNode; t->data = ch; CreateBiTree(t->lchild); CreateBiTree(t->rchild); } } void InOrderTraverse(BiTree t) { if (t) { string op = t->data; if (op[0] < '0' || op[0] > '9') cout << '('; InOrderTraverse(t->lchild); cout << t->data; InOrderTraverse(t->rchild); if (op[0] < '0' || op[0] > '9') cout << ')'; } } int main() { BiTree tree = new BitNode; string ch; while (cin >> ch) { tree->data = ch; CreateBiTree(tree->lchild); CreateBiTree(tree->rchild); InOrderTraverse(tree); printf("\n"); } return 0; }换一个算法实现这个代码
这段代码实现了对二叉树的创建和中序遍历,并且输出的遍历结果包含了括号,用于表示运算符的优先级。如果需要换一个算法实现这个代码,可以使用栈来实现二叉树的创建和中序遍历。
具体实现方法如下:
1. 定义一个栈,用于存放节点;
2. 从输入中读取每一个元素,然后创建一个节点;
3. 如果当前元素为操作数,则将其压入栈中;
4. 如果当前元素为操作符,则将其作为根节点,将栈顶的两个节点分别作为左右子节点,并将根节点压入栈中;
5. 当所有元素都处理完毕后,栈中剩余的节点即为二叉树的根节点。
具体代码实现如下:
```C++
#include <iostream>
#include <string>
#include <stack>
using namespace std;
typedef string ElemType;
typedef struct BitNode {
ElemType data;
struct BitNode* lchild, * rchild;
} BitNode, * BiTree;
void CreateBiTree(BiTree& t) {
stack<BitNode*> s;
BitNode* p, * q;
string ch;
cin >> ch;
while (ch != "#") {
p = new BitNode;
p->data = ch;
p->lchild = p->rchild = NULL;
if (!t) {
t = p;
}
if (!s.empty()) {
q = s.top();
if (!q->lchild) {
q->lchild = p;
}
else {
q->rchild = p;
s.pop();
}
}
if (ch[0] < '0' || ch[0] > '9') {
s.push(p);
}
cin >> ch;
}
}
void InOrderTraverse(BiTree t) {
if (t) {
string op = t->data;
if (op[0] < '0' || op[0] > '9') {
cout << '(';
}
InOrderTraverse(t->lchild);
cout << t->data;
InOrderTraverse(t->rchild);
if (op[0] < '0' || op[0] > '9') {
cout << ')';
}
}
}
int main() {
BiTree tree = NULL;
string ch;
while (cin >> ch) {
CreateBiTree(tree);
InOrderTraverse(tree);
printf("\n");
tree = NULL;
}
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)