⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 队列.cpp

📁 数据结构中队列的一些操作
💻 CPP
字号:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

typedef struct	QNode{
		int      data;
		struct	QNode *next;
}QNode,*Queueptr;

typedef struct{
  Queueptr    front;
  Queueptr     rear;
}Linkqueue;
//创建一个空队列
void Initqueue(Linkqueue &Q)
{
	Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
	  if(!Q.front)
		 exit(0);
		  Q.front->next=NULL;
	printf("成功创建队列。\n");
}

//销毁一个队列
void Destroyqueue(Linkqueue &Q)
{
	while(Q.front)
		{
		   Q.rear=Q.rear->next;
			free (Q.front);
			Q.front=Q.rear;
		}
}
//清空一个队列
void Clearqueue(Linkqueue &Q)
{
  Q.front=Q.rear;
  Q.front->next=NULL;
}
//创建一个队列
void Createqueue(Linkqueue &Q)
{
   QNode* r;
    int a;
    Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
       if(!Q.front)
		    exit(0);
	Q.rear->next=NULL;
    r=(QNode*)malloc(sizeof(QNode));
	printf("input a munber (if(0) stop):\n");
	scanf("%d",&a);
		while(a!=0)
		{
		   r->data=a;
		   Q.rear->next=r;
		   Q.rear=r;
           r=(QNode*)malloc(sizeof(QNode));
		   scanf("%d",&a);
		}
	Q.rear->next=NULL;
}
//插入e作为队列的新的队尾元素
void Enqueue(Linkqueue &Q,int e)
{
   Queueptr p;
   p=(Queueptr)malloc(sizeof(QNode));
   if(!p)
        exit(0);
   p->data=e;
   Q.rear->next=p;
   Q.rear=p;
   Q.rear->next=NULL;
}
//输出队列的所有元素
void printqueue(Linkqueue Q)
{
	 QNode *p;
	 p=Q.front->next;
     while(p!=NULL)
	 {
		 printf("%d\t",p->data);
		 p=p->next;
	 }

}
//删除队列的头元素
void Dequeue(Linkqueue &Q,int &e)
{
	QNode *p;
	p=(QNode*)malloc(sizeof(QNode));
  if(Q.front==Q.rear)
	  printf("队列是空的!\n");
  else
	  p=Q.front->next;
      e=p->data;
      Q.front->next=p->next;
	  if(Q.rear==p)
		  Q.rear=Q.front ;
	  free(p);
}
//获得队列的头元素
void GetHead(Linkqueue Q, int &e)
{
  if(Q.front==Q.rear)
	 printf("队列是空的!\n");
  else
	  e=Q.front->next->data;
}
//获得队列的长度
int Queuelength(Linkqueue Q)
{
   QNode *p;
	int  count=0;
   p=Q.front->next;
   while(p)
	   {
		   count++;
		   p=p->next;
	   }
   return count;
}

⌨️ 快捷键说明

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