C++queue Container Introduction

Source: Internet
Author: User

In the process of preparing for the data structure unit test, you inadvertently learned the posture in C + +. A big surprise. The original title is titled Swust OJ 972.

Statistics the width of a two-fork tree created by using the first order traversal (0972)Time limit (ms): 1000Memory Limit (KB): 10000submission:1154accepted:653accepted Description

The first order recursive traversal algorithm is used to create a two-fork tree and calculate the width of the two-fork tree. The method of constructing the two-fork tree by the first order recursive traversal is as follows: The abstract access to the two-fork tree node is materialized according to the thought of the first order recursive traversal to determine whether the node is generated based on the received data and realizes the two-fork list storage structure of the two-fork tree. The Contract binary tree node data is a single uppercase English character. The node is created when the received data is a character "#" that indicates that the node does not need to be created. Finally, the width of the created two-fork tree (which is the maximum number of nodes per layer of the two-fork tree) is counted. Note the sequence and number of "#" characters and Non-"#" characters in the input data series, which ultimately determine the shape of the two-fork tree that you are creating.

Input

Enter a string (used to create the corresponding two-fork tree) that is the uppercase English character and "#" characters entered for the keyboard input.

Output

Output the width of the two-fork tree corresponding to the use case.

Sample Input
123456
Sample Output
123456 11231

AC Source code:

#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <iostream>
using namespace Std;
#define MAX (A, b) a>b?a:b

int i;

typedef struct NODE
{
char data;
struct node *lchild;
struct node *rchild;
}btnode; int main ()

{
void Creattree (Btnode *&t,char str[]); When defining the data type of the formal parameter, the call can be made directly at the appropriate location with the name
int width (btnode *t);
Btnode *t;
Char str[100];
while (scanf ("%s", str)!=eof)
{
i=0;
Creattree (T,STR);
printf ("%d", Width (t));
}
return 0;
}
void Creattree (Btnode *&t,char str[])
{
if (str[i]!= ' # ')
{
t= (Btnode *) malloc (sizeof (Btnode));
t->data=str[i];
i++;
Creattree (T-&GT;LCHILD,STR);
Creattree (T-&GT;RCHILD,STR);
}
Else
{
T=null;
i++;
}
}
int width (btnode *t)
{
if (t==null)
return 0;
int mx=0,cnt;
Btnode *p;
Queue<btnode *>quea,queb; Definition of the Queue template class
Quea.push (t); Queue's basic operation, the t element is connected to the end of
while (!quea.empty ())
{
cnt=0;
while (!quea.empty ())
{
cnt++;
P=quea.front ();
if (p->lchild!=null)
Queb.push (P->lchild);
if (p->rchild!=null)
Queb.push (P->rchild);
Quea.pop (); Outbound: The first element of the popup queue, such as Q.pop (), does not return the value of the element;
}
Mx=max (MX,CNT);
Quea=queb;
while (!queb.empty ())
Queb.pop ();
}
return MX;
}

The knowledge involved:

The definition of the queue template class is in the <queue> header file.

The queue is very similar to the stack template, and the queue template also needs to define two template parameters, one element type, one container type, the element type is necessary, the container type is optional, and the default is the Dqueue type.

The sample code that defines the queue object is as follows:

queue<int>q1;

queue<double>q2;

The basic operations of the queue are:

1. Queue: As Q.push (x): The x element is connected to the end of the line;

2. Out of line: The first element of the popup queue, such as Q.pop (), does not return the value of the element;

3, access to the first element of the team: such as Q.front ()

4, access to the tail elements, such as q.back ();

5, the number of elements in the access team, such as Q.size ();

C++queue Container Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.