Exemplo n.º 1
0
int main(int argc, char* argv[])
{
    bool ok;

    Queue queue;
    queue.EnQueue(1);
    queue.EnQueue(2);
    queue.EnQueue(3);


    // queue.ClearQueue();
    // queue.IsEmpty();


    while (!queue.IsEmpty())
    {
        cout << queue.DeQueue(ok) << endl;
    }

    queue.EnQueue(5);
    queue.EnQueue(6);

    while (!queue.IsEmpty())
    {
        cout << queue.DeQueue(ok) << endl;
    }

    return 0;
}
Exemplo n.º 2
0
int main(){

    Queue q;
    q.EnQueue(5);
    q.EnQueue(2);
    q.EnQueue(6);
    q.EnQueue(8);
    cout << "Size: " << q.Size() << endl;
    cout << q.DeQueue() << endl;
    cout << q.DeQueue() << endl;
    cout << q.DeQueue() << endl;
    cout << q.DeQueue() << endl;

    Stack s;
    s.Push(8);
    s.Push(6);
    s.Push(2);
    s.Push(5);
    cout << s.Pop() << endl;
    cout << s.Pop() << endl;
    cout << s.Pop() << endl;
    cout << s.Pop() << endl;

    return 0;
}
Exemplo n.º 3
0
void BFS::Run(int** matrix, int size, int start)
{
	int index = -1;
	Queue* queue = new Queue();
	SNode* node = new SNode();
	node->value = start;
	queue->EnQueue(node);
	while(queue->GetLength() != 0)
	{
		node = queue->DeQueue();
		index = node->value;
		this->labels[index] = 1;
		delete node;
		for(int i=0; i<size; i++)
		{
			if(matrix[index][i] == 1 && this->labels[i] == 0)
			{
				node = new SNode();
				node->value = i;
				queue->EnQueue(node);
				this->labels[i] = 1;
				this->previous[i] = index;
			}
		}
		node = NULL;
	}
	delete queue;
}
Exemplo n.º 4
0
void Prim::Run(int** matrix, int size, int start)
{
	bool isStop = false;
	SEdge* edgeMin = NULL;
	this->label[start] = 1;
	Queue* queue = new Queue();
	SNode* node = new SNode();
	node->value = start;
	queue->EnQueue(node);
	while(this->result->GetLength() != this->size - 1 && isStop == false)
	{
		edgeMin = NULL;
		for(int i=0; i<queue->GetLength(); i++)
		{
			node = queue->GetIndex(i);
			for(int j=0; j < size; j++)
			{
				if(matrix[node->value][j] != 0 && this->label[j] == 0)
				{
					if(edgeMin == NULL)
					{
						edgeMin = new SEdge();
						edgeMin->x = node->value;
						edgeMin->y = j;
						edgeMin->value = matrix[node->value][j];
					}
					else
					{
						if(matrix[node->value][j] < edgeMin->value)
						{
							edgeMin->x = node->value;
							edgeMin->y = j;
							edgeMin->value = matrix[node->value][j];
						}
					}
				}
			}
		}
		if(edgeMin != NULL)
		{
			this->result->Add(edgeMin);
			this->sum += edgeMin->value;
			this->label[edgeMin->x] = 1;
			this->label[edgeMin->y] = 1;
			node = new SNode();
			node->value = edgeMin->y;
			queue->EnQueue(node);
		}
		else
		{
			isStop = true;
		}
	}
	delete queue;
}
Exemplo n.º 5
0
int main(){
	Queue q;
	for (int i = 0; i < 10; i++)
		q.EnQueue(i);
	cout << q.DeQueue() << '\n';
	cout << q.DeQueue() << '\n';
	for (int i = 10; i < 20; i++){
		q.EnQueue(i);
	}
	cout << q.DeQueue() << " ";
	cout << q.DeQueue() << '\n';

}
Exemplo n.º 6
0
int main()
{
	Queue a;
	a.EnQueue(1);
	a.EnQueue(2);
	a.EnQueue(3);
	int t = a.DeQueue();
	int k = a.DeQueue();

	cout << t << k << a.checkFront() << endl;//

	system("pause");
	return 0;
}
Exemplo n.º 7
0
int main()
{
	Queue Q;

	for(int i = 0; i < 10; ++i)
	{
		int* p = new int(i);
		Q.EnQueue(p);
		cout << "Size = " << Q.Size() << endl;
	}

	for(QueueNode* node = Q.Head(); node != 0; node = node->next)
	{
		int* p =(int*)node->data;
		cout << *p<< " ";
	}
	cout << endl;


	while(Q.Empty() == false)
	{
		int* p = (int*)Q.DeQueue();
		cout << *p << " ";
		cout << "Size = " << Q.Size() << endl;
		
	}
	cout << endl;
	return 0;
}
Exemplo n.º 8
0
bool Spread::ModelSpread(Queue<long> &lqueue,map<long,UserStatus> &userSMap)
{
    map<long,UserStatus>::iterator iter2;
    map<long,float> userSMapDcache;
    map<long,float>::iterator iter3;
    map<long int,UserLink>::iterator iter4;
    while(!lqueue.IsEmpty()){
            long userid = lqueue.GetQueue();
            iter4 = userGraphMap->find(userid);
            //免疫因子 /mu
            if(userSMap[userid].tfIdf<mu){
                      continue;
            }
            if(iter4 != userGraphMap->end())
            {
                UserLink head = iter4->second;
                float srcWeight = head->weight;
                float dstWeight;
                int connect;
                UserNode *g = head->next;
                while(g!=NULL){
                    iter2 = userSMap.find(g->uid);
                    if(iter2 == userSMap.end())
                    {
                        dstWeight = g->weight;
                        connect = g->connect;
                        float diffuseRate = DiffuseRate(srcWeight,dstWeight,connect);
                        float tfIdf = userSMap[userid].tfIdf;
                        iter3 = userSMapDcache.find(g->uid);
                        if(iter3 != userSMapDcache.end())
                            userSMapDcache[g->uid] += tfIdf*diffuseRate;
                        userSMapDcache[g->uid] = tfIdf*diffuseRate;
                    }
                    g = g->next;
                }
            }
            if(lqueue.IsEmpty())
            {
               for(iter3 = userSMapDcache.begin();iter3 != userSMapDcache.end();iter3++)
               {
                   lqueue.EnQueue(iter3->first);
                   UserStatus userStatus;
                   userStatus.infected =true;
                   userStatus.tfIdf = iter3->second;
                   userSMap[iter3->first] = userStatus;
               }
               userSMapDcache.clear();
            }
        }
    return true;
}
Exemplo n.º 9
0
bool Spread::BuildQueue(Queue<long> &lqueue,map<long,UserStatus> &userSMap,StatusTagLink p,const int &wid)
{
    map<long,WordTfIdfLink>::iterator iter;
    map<long,UserStatus>::iterator iter2;
    while(p!=NULL)
    {
        long userid = p->userid;
        float tfIdf;
        iter = sidWTIMap->find(p->sid);
        if(iter != sidWTIMap->end())
        {
            WordTfIdfNode *q = iter->second->next;
            while(q!=NULL)
            {
                if(q->wid == wid)
                {
                    tfIdf = q->tfIdf;
                    break;
                }
                q = q->next;
            }
            if(q==NULL)
                tfIdf = 0.0;
        }
        else
        {
                tfIdf = 0.0;
        }
        p = p->next;
        iter2 = userSMap.find(userid);
        if(iter2 != userSMap.end())
        {
            iter2->second.tfIdf += tfIdf;
        }
        else
        {
            UserStatus userStatus;
            userStatus.tfIdf = tfIdf;
            userStatus.infected = true;
            userSMap[userid] = userStatus;
            lqueue.EnQueue(userid);
        }

    }
    return true;
}
Exemplo n.º 10
0
int main()
{
	Queue<int> queue;	 
    cout<<"******************************"<<endl;   
	cout<<"*selet an item        "<<endl;
	cout<<"*establish a quue,enter'1' "   <<endl;
	cout<<"*output enter           '4'  "<<endl;
	cout<<"*quit,enter             '0'          "<<endl;
	cout<<"******************************"<<endl;
	int i;
    do{  
		cout<<"ÇëÊäÈëÄãµÄÑ¡Ôñ£º";
		cout<<"1---> establish¡¢2----game(int)¡¢3----game()¡¢4--->output¡¢0--->quit¡¢"<<endl;
		cin>>i;  
	   switch(i)
	   { 
	      case 1:
			  	cout<<"create a queue"<<endl;
	            cout<<"please enter the number of the card:"<<endl;
				int s,j,Flag ;
				Flag=1;
				cin>>s;
		        for(j=1;j<=s;j++){
			       queue.EnQueue(j,Flag);
				}
				cout<<"OKey"<<endl;
			break;
		  case 2:
			   queue.Game(s);
			    break;
		   case 3:
			   cout<<"the result:"<<endl;
			   queue.Game();
			    break;
		   case 4:
			   cout<<"the members in the queue"<<endl; 
               queue.Output();
			   break;
	   }		   
	 }while(i!=0);	
  
	return 0;
}
Exemplo n.º 11
0
template <typename T> void test(Queue<T> testobj/*,bool type*/)
{
	
	default_random_engine e;
/*
	if(type)
		goto inttype;
	else
		goto floattype;
inttype:
		uniform_int_distribution<unsigned> u(0,9);
floattype:
		uniform_real_distribution<double> u(0,9);
*/
	uniform_real_distribution<double> u(0,9);


	try{
		cout<<"Now testing Queue Enqueue and DeQueue function:"<<endl;
		char ch;
		while(1)
		{
			try{
			bool quit=false;
			cout<<"EnQueue or DeQueue? press \"E or e\" for EnQueue, press \"D or d\" for DeQueue, press \"Q or q\" for quit: ";
			cin>>ch;
			switch(ch)
			{
				case 'e':
				case 'E':
					{
						T temp=u(e);
						testobj.EnQueue(temp);
						cout<<"Generated random number: "<<temp<<" put it into the queue."<<endl;
						cout<<"Now the queue is:"<<endl;
						testobj.print();
						break;
					}
				case 'D':
				case 'd':
					{
						cout<<"Number: "<<testobj.DeQueue()<<" is DeQueued."<<endl;
						cout<<"Now the queue is:"<<endl;
						testobj.print();
						break;
					}
				case 'q':
				case 'Q':
					{
					quit=true;
					break;
					}
				default:
					throw runtime_error("Invalid input, try again!");
					break;
			}//end switch

			if(quit)
				break;

		  }//end inner try
			catch(runtime_error err)
			{
				cerr<<err.what()<<endl;
			}
		}//end while
		
	   }
	catch(overflow_error over)
		{
			cerr<<over.what()<<endl;
		}
	catch(underflow_error under)
		{
			cerr<<under.what()<<endl;
		}

	Queue<T> q2(testobj);
	Queue<T> q3;
	q3=q2;
	cout<<"Now testing the copy assignement and assignment operator!"<<endl;
	cout<<"The copy constructor assigned q2 from q, the q2 is:"<<endl;
	q2.print();
	cout<<"The assignment operator: q3=q2. the q3 is:"<<endl;
	q3.print();

} 
Exemplo n.º 12
0
int main()
{
	Queue<int> queue;	 
    cout<<"******************************"<<endl;   
	cout<<"*selet an item        "<<endl;
	cout<<"*establish a quue,enter'1' "   <<endl;
	cout<<"*Enqueue,enter '2'  "<<endl;
	cout<<"*Dequeue enter '3'    "<<endl;
	cout<<"*quit,enter  '0'          "<<endl;
	cout<<"******************************"<<endl;
	int i;
    do{  
		cout<<"ÇëÊäÈëÄãµÄÑ¡Ôñ£º";
		cout<<"1---> establish¡¢2----Enqueue¡¢3----Dequeue¡¢4--->output¡¢0--->quit¡¢"<<endl;
		cin>>i;  
	   switch(i)
	   { 
	      case 1:
			  	cout<<"create a queue"<<endl;
	            cout<<"please enter the number of the queue"<<endl;
				int j,s;
				cin>>s;
		        for(j=0;j<s;j++){
				   cout<<"there is "<<s<<" numbers"<<endl;
			       cout<<"this is the "<<j+1<<"th number"<<endl;
			       int m;
		           cin>>m;
			       queue.EnQueue(m);
				}
				cout<<"OKey"<<endl;
			break;
		   case 2: 
			   cout<<"push members into queue:"<<endl;
			   int n,l;
			   cout<<"How many  member you want to enter:"<<endl;
			   cin>>n;
			   for(i=0;i<n;i++){
               cout<<"please enter the "<<i+1 <<"th member you want to push:"<<endl;
               cin>>l;
			   queue.EnQueue(l);
			   }
               cout<<"OK"<<endl;
			   break;
		   case 3:
			   cout<<"Dequeue"<<endl;
			   cout<<"How many  member you want to pop"<<endl;
				   int m;
			   cin>>m;
			   for(i=0;i<m;i++){
				   queue.DeQueue();
			   } 
			   cout<<"OK";
			    break;
		   case 4:
			   cout<<"the members in the queue"<<endl; 
               queue.Output();
			   break;
	   }		   
	 }while(i!=0);	
  
	return 0;
}
void CMFC_CreatBinaryTreeView::OnDraw(CDC* /*pDC*/)
{
	CMFC_CreatBinaryTreeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	// TODO:  在此处为本机数据添加绘制代码

	CreatBinaryTreeDlg dlg;	//创建对话框类对象
	dlg.DoModal();
	//CString转换为wchar_t*
	wchar_t *VLR = dlg.PreBinTree.GetBuffer();
	dlg.PreBinTree.ReleaseBuffer();
	wchar_t *LVR = dlg.MidBinTree.GetBuffer();
	dlg.MidBinTree.ReleaseBuffer();


	int N = dlg.PreBinTree.GetLength();	  //前序序列的长度,二叉树结点个数
	int len = dlg.MidBinTree.GetLength();	//中序序列的长度
	if (N != len)
	{
		INT_PTR Respon;
		Respon = MessageBox(_T("前序和中序序列输入错误,退出!"), _T("错误提示"), MB_OK | MB_ICONSTOP);
		return;
	}

	//将对话框中的输入数据传入创建二叉树的模板类的构造函数中构造二叉树
	CBinaryTree<wchar_t> BinTree(VLR, LVR, N);
	BinTreeNode<wchar_t> *Root = BinTree.GetRoot();		//获取二叉树的根结点
	int depth = BinTree.Depth(Root);	//二叉树的深度
	Queue<BinTreeNode<wchar_t>*, int, int> Q;	//添加队列,用来存储二叉树的结点,以及结点所显示位置的圆的圆心坐标

	//获取菜单文本的坐标
	CRect Re;
	this->GetWindowRect(Re);
	auto w = Re.Width();
	auto h = Re.Height();
	CClientDC Dc(this);	

	//结点的坐标分布
	int Height = 1;	//结点的高度每增加一层点,高度就加一
	int HeightLeft;	//左孩子结点的高度
	int HeightRight; //右孩子结点的高度
	int R = h / (4 * (depth + 1));	//圆的半径

	int Molecule = 1;	//圆心坐标的分子
	int Denominator;	//圆心坐标的分母
	int X0, Y0;		//包装结点数据的圆的圆心坐标
	int XA, YA, XD, YD;		//圆外接正方形的左上角和右下角端点的坐标
	int XE, YE, XF, YF;		//连接两个圆的线段的端点坐标,点E在点F的上方
	Q.EnQueue(Root, Molecule, Height);

	while (!Q.IsEmpty())
	{
		//将当前结点的相关数据弹出
		Q.DeQueue(Root, Molecule, Height);
		//圆心坐标
		Denominator = (int)pow(2, Height);
		X0 = w*Molecule / Denominator;		//圆心横坐标
		Y0 = h*Height / (depth + 1);		//圆心纵坐标
		
		//正方形的两端点的坐标
		XA = X0 - R;	//正方形左上角端点坐标
		YA = Y0 - R;
		XD = X0 + R;	//正方形右下角端点坐标
		YD = Y0 + R;
		//画圆
		Dc.Ellipse(XA, YA, XD, YD);

		//在圆心显示二叉树的根结点数据
		CString da;
		da += Root->data;   
		Dc.SetTextColor(RGB(255, 0, 0));	//设置输出文本颜色为红色
		Dc.TextOutW(X0, Y0, da);

		if (Root->LeftChild)
		{
			//如果有左孩子,就画左线段
			//线段的上端点坐标
			XE = (int)(X0 - R / sqrt(2));		//XO为当前结点的圆心横坐标
			YE = (int)(Y0 + R / sqrt(2));		//YO为当前结点的圆心纵坐标

			HeightLeft = Height + 1;	//高度加一
			Molecule = 2 * Molecule - 1;	//左为减,左子女的圆心横坐标的分子
			//线段的下端点坐标
			XF = w*Molecule / (2 * Denominator);
			YF = h*HeightLeft / (depth + 1) -R;		
			//画左线段
			Dc.MoveTo(CPoint(XE, YE));
			Dc.LineTo(CPoint(XF, YF));
			
			//将访问左子女所需要的数据压入队列
			Q.EnQueue(Root->LeftChild, Molecule, HeightLeft);
			Molecule = (Molecule + 1) / 2;	//将跟结点圆心的分子还原
		}
		if (Root->RightChild)
		{
			//如果有右孩子,就画右线段
			//线段的上端点坐标
			XE = (int)(X0 + R / sqrt(2));		//XO为当前结点的圆心横坐标
			YE = (int)(Y0 + R / sqrt(2));		//YO为当前结点的圆心纵坐标

			HeightRight = Height + 1;	//高度加一
			Molecule = 2 * Molecule + 1;	//右为加,右子女的圆心横坐标的分子
			//线段的下端点坐标
			XF = w*Molecule / (2 * Denominator);
			YF = h*HeightRight / (depth + 1) - R;	
			//画右线段
			Dc.MoveTo(CPoint(XE, YE));
			Dc.LineTo(CPoint(XF, YF));

			//将访问右子女所需要的数据压入队列
			Q.EnQueue(Root->RightChild, Molecule, HeightRight);
		}
	}
	ReleaseDC(&Dc);
}