queue.cpp

来自「提供队列数据结构的相关操作」· C++ 代码 · 共 99 行

CPP
99
字号
// queue.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

typedef int content;

typedef  struct _node
{
	content stcon;
	struct  _node* pnext;
} node;
typedef struct _queue 
{
	node *pfirst;
	node *plast;
	int nlenth;
} queue;

int  Init_queue( queue *pQu)
{
	pQu->pfirst = NULL;
	pQu->plast = NULL;
	pQu->nlenth = 0;
	return 1;
}
int Get_queue(queue *pQu)
{
	node * pexplore = pQu->pfirst;
	if ( pQu->pfirst == NULL )
		return 0;
	for ( ;pexplore != NULL ;)
	{
		printf("%d " , pexplore->stcon);
		pexplore = pexplore->pnext;
	}
	return 1;
}
int Out_queue(queue *pQu)
{
	node * del = pQu->pfirst;
	if ( pQu->nlenth == 0)
		return 0;

	pQu->pfirst = pQu->pfirst->pnext;
	pQu->nlenth--;
	delete (del);
	return 1;
}

int In_queue(queue *pQu,content stin)
{	
	if ( pQu->nlenth == 0)
	{
		pQu->plast = new (node);
		pQu->pfirst = pQu->plast;
	}
	else
	{
		pQu->plast->pnext = new (node );
		pQu->plast = pQu->plast->pnext;
	}
	pQu->plast->stcon = stin;
	pQu->plast->pnext = NULL;
	pQu->nlenth++;
	return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int i = 0;
	int j = 0;
	content nn = 1;
	content get = 0;
	queue Qu;
	Init_queue(&Qu);
	if (0 == Get_queue(&Qu))
		printf( "empty queue! \n");
	if (0 == Out_queue(&Qu))
		printf( " queue is empty! \n");

	In_queue(&Qu,nn); printf( "%d \n" ,Qu.nlenth);
	nn = 2;

	In_queue(&Qu,nn);printf( "%d \n" ,Qu.nlenth);
	nn = 3;
	Out_queue(&Qu);printf( "%d \n" ,Qu.nlenth);
	In_queue(&Qu,nn);printf( "%d \n" ,Qu.nlenth);
	Out_queue(&Qu);printf( "%d \n" ,Qu.nlenth);
	Out_queue(&Qu);printf( "%d \n" ,Qu.nlenth);

	if ( 0 == Get_queue(&Qu))
		printf( " queue is empty! \n");


	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?