能判断标签的对称(栈) 以及嵌套关系(树)
HtmlDoc.h
#ifndef HTMLDOC_H_
#define HTMLDOC_H_
#include<iostream>
#include<string>
#include"list.h"
#include"stack.h"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct TreeNode{
string nodeName;//node name
List<TreeNode*> *childList;//child list node
};
class HtmlDocument {
private:
TreeNode *treeRoot;//html节点 对象
Stack<TreeNode*>* stack;
bool checkAndPushStack(string headNodeName);///遍历树看是否合法如果合法放进栈里
bool checkStackTop(string tailNodeName);///和栈顶元素比较如果相等就移除栈顶元素,否则返回false
TreeNode * traverseTree(TreeNode* treeNode, string nodeName);//遍历整个树结构
public:
HtmlDocument();
~HtmlDocument();
void release(TreeNode*);
bool isLegal(string path);
};
#endif /* HTMLDOC_H_ */
HtmlDoc.cpp
#include "HtmlDoc.h"
HtmlDocument::HtmlDocument(){
this->stack = new Stack<TreeNode*>();//建一个存放树节点的栈
//---------以下代码是够造一个树结构 目地--------------------//
TreeNode *html = new TreeNode();
this->treeRoot = html;
html->nodeName = "html";
List<TreeNode*> *htmlChildList= new List<TreeNode*>();
html->childList = htmlChildList;
TreeNode *head = new TreeNode();
head->nodeName = "head";
List<TreeNode*> *headChildList= new List<TreeNode*>();
head->childList = headChildList;
htmlChildList->add(head);
TreeNode *body = new TreeNode();
body->nodeName = "body";
List<TreeNode*> *bodyChildList= new List<TreeNode*>();
body->childList = bodyChildList;
htmlChildList->add(body);
TreeNode *title = new TreeNode();
title->nodeName = "title";
List<TreeNode*> *titleChildList= new List<TreeNode*>();
title->childList = titleChildList;
headChildList->add(title);
TreeNode *h1 = new TreeNode();
h1->nodeName = "h1";
h1->childList = NULL;
bodyChildList->add(h1);
TreeNode *p = new TreeNode();
p->nodeName = "p";
p->childList = NULL;
bodyChildList->add(p);
TreeNode *center = new TreeNode();
center->nodeName = "center";
List<TreeNode*> *centerChildList= new List<TreeNode*>();
center->childList = centerChildList;
bodyChildList->add(center);//left
TreeNode *left = new TreeNode();
left->nodeName = "left";
left->childList = NULL;
bodyChildList->add(left);
TreeNode *form = new TreeNode();
form->nodeName = "form";
form->childList = new List<TreeNode*>();
bodyChildList->add(form);//left
TreeNode *table = new TreeNode();
table->nodeName = "table";
List<TreeNode*> *tableChildList= new List<TreeNode*>();
table->childList = tableChildList;
bodyChildList->add(table);//left
TreeNode *tr = new TreeNode();
tr->nodeName = "tr";
List<TreeNode*> *trChildList= new List<TreeNode*>();
tr->childList = trCh