[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);//广度遍历

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值