1. 程式人生 > >[C++]類xml資料格式解析

[C++]類xml資料格式解析

#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <assert.h>
#include <ctype.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
//#include <stdlib.h>
//#include <cstring>

const int MAXLENGTH = 100;

typedef struct tagNode
{
 int data;
 struct tagNode *pLeftChild;
 struct tagNode *pRightBrother;
 struct tagNode *pParent;
}*pNode,Node;

typedef struct Queue
{
 pNode baseData[MAXLENGTH];
 int front;
 int rear; 
}Queue;

bool InitQueue(Queue &queueNodes);//初始化佇列
bool InQueue(Queue &queueNodes,pNode data);//入佇列
bool OutQueque(Queue &queueNodes,pNode &data);//出佇列
int PrintLength(Queue queueNodes);//返回佇列長度
pNode initNodes(pNode pInsertNode,int data);//返回下一個要插入的位置
void printNodesByDepth(pNode root,int height);//深度遍歷
void printNodesByWidth(pNode root);//廣度遍歷

int PrintLength(Queue queueNodes)
{
 if (queueNodes.rear < queueNodes.front)
  return queueNodes.rear + MAXLENGTH - queueNodes.front;
 else
  return queueNodes.rear - queueNodes.front;
}

bool InitQueue(Queue &queueNodes)//初始化佇列
{
 queueNodes.front = 0;
 queueNodes.rear = 0;
 return true;
}
bool InQueue(Queue &queueNodes,pNode data)//入佇列
{
 if ((queueNodes.rear+1) % MAXLENGTH == queueNodes.front)
 {
  cout<<"Queue is full!";
  return false;
 }
 else
 {
  queueNodes.baseData[queueNodes.rear] = data;
  queueNodes.rear = (queueNodes.rear+1) % MAXLENGTH;
  return true;
 }

}
bool OutQueque(Queue &queueNodes,pNode &data)//出佇列
{
 if (queueNodes.front == queueNodes.rear)
 {
  cout<<"Queue is empty!";
  return false;
 }
 else
 {
  data = queueNodes.baseData[queueNodes.front];
  queueNodes.front = (queueNodes.front + 1) % MAXLENGTH;
  return true;
 }
}
//////////////////////// 佇列操作完成//////////////////////////////////////////////////
pNode initNodes(pNode pInsertNode,int data)//返回下一個要插入的位置
{
 if (data>0)
 {
  pNode pNewNode = new Node;
  pNewNode->data = data;
  pNewNode->pParent = pInsertNode;
  pNewNode->pLeftChild = NULL;
  pNewNode->pRightBrother = NULL;

  pNode p = pInsertNode;//遊標

  if (p->pLeftChild == NULL)//插入點沒有左孩子,新節點作為左孩子
  {
   p->pLeftChild = pNewNode;
  }
  else//有左孩子,插入到最後一個孩子節點的右邊
  { 
   p = pInsertNode->pLeftChild;
   while (p->pRightBrother!=NULL)
   {
    p = p->pRightBrother;
   }
   p->pRightBrother = pNewNode;
  }
  return pNewNode;//返回下一個插入點指標
 }
 else//小於零返回該節點的母節點
 {
  return pInsertNode->pParent;
 } 
}
void printNodesByDepth(pNode root,int height)//深度遍歷
{
 if (root != NULL)
 {
  if (0 == height)
  {
   cout<<root->data<<endl;
  }
  else
  {
   cout<<setw(height)<<"+"<<root->data<<endl;
  }
  
  printNodesByDepth(root->pLeftChild,height+1);
  printNodesByDepth(root->pRightBrother,height);
 } 
}
void printNodesByWidth(pNode root)//廣度遍歷
{
 Queue myQueue;
 pNode pTemp = NULL;
 InitQueue(myQueue);//初始化佇列
 InQueue(myQueue,root);// 根節點入佇列

 while (myQueue.front != myQueue.rear)//將隊首出佇列,並將其孩子鏈插入隊尾
 {
  OutQueque(myQueue,pTemp);
  cout<<"current length:"<<PrintLength(myQueue)<<endl;
  cout<<pTemp->data<<" ";
  if (pTemp->pLeftChild != NULL)
  {
   pTemp = pTemp->pLeftChild;

   InQueue(myQueue,pTemp);
  
   pTemp = pTemp->pRightBrother;

   while(pTemp != NULL)
   {
    InQueue(myQueue,pTemp);
    pTemp = pTemp->pRightBrother;
   }
  }
 }

}
void main()
{
 int source[] = {4,11,-11,3,2,-2,8,-8,19,-19,-3,5,6,-6,7,-7,10,-10,-5,-4};
// int source[] = {4,3,5,6,-6,-5,-3,2,-2,-4};
 int lenth = sizeof(source)/sizeof(int);
 pNode root = new Node;
 root->pLeftChild = NULL;
 root->pRightBrother = NULL;
 root->pParent = NULL;
 root->data = source[0];
 pNode InsertNode = root;

 for (int i = 1; i<lenth; i++)
 {
  InsertNode = initNodes(InsertNode,source[i]);
 }
// printNodesByDepth(root,0); //深度遍歷
 printNodesByWidth(root);//廣度遍歷

}