• [置顶] 数据结构之 队列的操作与实现


    // 队列.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "malloc.h"
    #define maxSize 10
    //循环队列
    typedef struct
    {
    	int data[maxSize];
    	int front;
    	int rear;
    }SqQueue;
    void initQueue(SqQueue &qu)//初始队
    {
    	qu.front=qu.rear=0;
    }
    int isEmpty(SqQueue qu)//判断队是否为空
    {
    	if(qu.front==qu.rear)
    		return 1;
    	else
    		return 0;
    }
    void enQueue(SqQueue &qu,int x)//进队
    {
    	if((qu.rear+1)%maxSize==qu.front)
    		return;
    	else
    	qu.rear=(qu.rear+1)%maxSize;
    	qu.data[qu.rear]=x;
    }
    void delQueue(SqQueue &qu,int &x)//出队
    {
    	if(qu.front==qu.rear)
    		return;
    	qu.front=(qu.front+1)%maxSize;
    	x=qu.data[qu.front];
    	
    }
    
    void show(SqQueue &qu)
    {
    	int x;
    	while(qu.front!=qu.rear)
    	{
    		delQueue(qu,x);
    		printf("%d",x);
    	}
    
    }
    
    //链队
    typedef struct QNode
    {
    	int data;
    	struct QNode *next;
    }QNode;
    typedef struct
    {
    	QNode *front;
    	QNode *rear;
    }LiQueue;
    void Li_initQueue(LiQueue *&L)
    {
    	L=(LiQueue*)malloc(sizeof(LiQueue));
    	L->front=L->rear=NULL;
    }
    void Li_enQueue(LiQueue *&L,int x)
    {
    	QNode *p;
    	p=(QNode*)malloc(sizeof(QNode));
    	p->data=x;
    	p->next=NULL;
    	if(L->rear==NULL)
    		L->front=L->rear=p;
    	else
    	{
    		L->rear->next=p;
    		L->rear=p;
    	}
    }
    void Li_delQueue(LiQueue *&L,int &x)
    {
    	QNode *p;
    	if(L->front==NULL)
    		return;
    	else
    	{
    		p=L->front;
    		if(L->front==L->rear)
    		{
    			L->front=L->rear=NULL;
    		}
    		else
    		{
    			L->front=L->front->next;
    		}
    		x=p->data;
    		free(p);
    	}
    }
    
    void Li_show(LiQueue *&L)
    {
    	int x;
    	while(L->front!=NULL)
    	{
    		Li_delQueue(L,x);
    		printf("%d",x);
    	}
    
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    	/*
    	//顺序队
    	SqQueue qu;
    	initQueue(qu);
    	for(int i=1;i<10;i++)
    	{
    		enQueue(qu,i);	
    	}
    	show(qu);
    	*/
    	//链队
    	LiQueue *Li;
    	Li_initQueue(Li);
    	for(int j=1;j<5;j++)
    	{
    		Li_enQueue(Li,j);	
    	}
    	Li_show(Li);
    }
    
    

  • 相关阅读:
    Extjs4.0中清空filefield已选文件
    .net操作读取word中的图像并保存
    WebForm_PostBackOptions未定义 错误排查
    数据库关键字
    VS2008生成WebSite和WebApplication的区别(转载)
    安装天乙论坛(SSH架构的开源项目)时遇到的问题
    Hibernate与Oracle char类型的列之间的兼容问题
    关于spring3使用AOP编程时需要引入哪些jar包的问题
    让IE支持HTML5的Canvas
    IIS + TOMCAT 注意事项
  • 原文地址:https://www.cnblogs.com/zhujunxxxxx/p/3344853.html
Copyright © 2020-2023  润新知