队列基本操作—出队与入队

#include<stdio.h>
#include<stdlib.h>
typedef struct QNode
{	//构造结点类型
	int data;
	struct QNode *next;
}*QueuePtr;
typedef struct
{	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
void CreateQueue(LinkQueue &Q);//创建队列
void EnQueue(LinkQueue &Q,int e);//插入元素进队
void DeQueue(LinkQueue &Q);//删除队头元素
void PrintfQueue(LinkQueue &Q);//输出队列
void DestroyQueue(LinkQueue &Q);//销毁队列
void main()
{	LinkQueue Qa;
	int m,n;
	CreateQueue(Qa);
	printf("Please input the total of inserting number:\n");
	scanf("%d",&m);
	while(m--)
	{	printf("Please input a number to insert:");
		scanf("%d",&n);
		EnQueue(Qa,n);
	}
	PrintfQueue(Qa);
	DeQueue(Qa);
	PrintfQueue(Qa);
	DestroyQueue(Qa);
}
void CreateQueue(LinkQueue &Q)
{	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front)
	{	printf("Fail to create queue!\n");
		return;
	}
	Q.front->next=NULL;
	printf("Success to create queue!\n");
}
void EnQueue(LinkQueue &Q,int e)
{	QueuePtr p;
	if(!(p=(QueuePtr)malloc(sizeof(QNode))))
	{	printf("Fail to insert element!\n");
		return;
	}
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	printf("Success to insert element:%d\n",e);
}
void DeQueue(LinkQueue &Q)
{	QueuePtr q;
	int x;
	if(Q.rear==Q.front)
	{	printf("the queue is empty!\n");
		return;
	}
	q=Q.front->next;
	x=q->data;
	Q.front->next=q->next;
	if(Q.rear==q)
		Q.rear=Q.front;
	free(q);
	printf("Success to delete element:%d\n",x);
}
void DestroyQueue(LinkQueue &Q)
{	while(Q.front)
	{	Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
	}
	printf("Success to destroy queue!\n");
}
void PrintfQueue(LinkQueue &Q)
{	QueuePtr p;
	if(Q.rear==Q.front)
	{	printf("The queue is empty!\n");
		return;
	}
	printf("The queue is:");
	p=Q.front->next;
	while(p)
	{	printf("%d",p->data);
			p=p->next;
	}
	printf("\n");
}

队列基本操作—出队与入队

时间: 2024-08-09 08:58:34

队列基本操作—出队与入队的相关文章

循环队列的实现(出队,入队,遍历等)

队列的抽象数据类型定义为: 类型名称:队列. 数据对象集:一个有0个或多个元素的有穷线性表. 操作集:对于一个长度为正整数MaxSize的队列Q∈Queue, 记队列中的任一元素item∈ElementType,有: (1)Queue CreateQueue(int MaxSize):创建一个长度为MaxSize的空队列: (2)bool isEmpty(Queue Q):判断队列是否为空,若不空返回true(1),否则返回false(0): (3)void AddQ(Queue Q, Elem

循环队列的初始化、入队、出队等基本操作

循环队列的初始化.入队.出队等基本操作,实现代码如下: #include<iostream> using namespace std; #define TRUE 1 #define FALSE 0 //循环队列的类型定义 #define MAXSIZE 50//队列的最大长度 typedef struct { int element[MAXSIZE];//队列的元素空间 int front;//头指针指示器 int rear;//尾指针指示器 }SeqQueue; //循环队列初始化 void

链队列的初始化、入队、出队等操作实现

链队列的初始化.入队.出队等基本操作实现代码如下: #include<iostream> using namespace std; #define  TRUE 1 #define  FALSE 0 //链队列定义 typedef struct Node { int data;//数据域 struct Node *next;//指针域 }LinkQueueNode; typedef struct { LinkQueueNode *front;//队头指针front LinkQueueNode *

【C++】容器适配器实现队列Queue的各种功能(入队、出队、判空、大小、访问所有元素等)

适配器: 将一个通用的容器转换为另外的容器,所谓的容器,指的是存放数据的器具,像我们知道的顺序表和链表都是容器Container.举个例子解释一下吧,我们的电压都是220v,而像充电线就起到转换到合适的电压的作用.而这里,我们的主角就是将通用的链表结构转换为来实现队列Queue这一数据结构,(意思就是,链表还可以去实现其他的数据结构). 在线性表中,分为链表和顺序表,我们知道其中的差别: 链表:节点灵活,使得插入删除元素方便灵活,但是对于单链表若有节点指针_head._tail,查找元素较为麻烦

队列的入队和出队操作

#include<iostream> #include<stdio.h> #include<string.h> #include<conio.h> using namespace std; typedef struct student{ int data; struct student *next; }node; typedef struct linkqueue { node *first, *rear; }queue; //队列的入队 queue *ins

编程实现队列的入队/出队操作

思路:队列其实也是一个链表,只是队列还有两个特殊的结点,一个指向队头,一个指向队尾.先设计数据结构,如下 typedef struct student * PNode; typedef struct linkqueue * Pqueue; typedef struct student { int data; PNode next; }Node; typedef struct linkqueue { PNode first; PNode rear; }queue; 1.入队操作其实是指向队尾的指针

循环队列的顺序存储和入队出队操作

今天看图的广度优先遍历的时候,发现用到了循环队列,补一下循环队列的知识,参考<大话数据结构>的P116~117,自己写了一个简单的测试例子便于理解. 首先需要理解以下三条公式. front是队头元素的下标,rear是队尾元素后一位的下标.(书上用头指针和尾指针,front和rear并不是指针,个人觉得不太好) 1.队列空的条件 显然front==rear 注意:如果队列不保留任何元素空间 满足front==rear的情况下,可能是队列空,也可能是队列满.所以为了方便,本文讨论的是采用保留一个元

顺序结构循环队列的基本操作(一)(进队,出队)待优化

#include<stdio.h>#define ElemType int#include<malloc.h>#define MAXSIZE 10typedef struct{ ElemType *data; int front,rear;}Queue;typedef struct BitNode{ ElemType data; struct Bitree *Lchild,*Rchild;}BitNode;Queue Init_queue(Queue Q){ Q.data=(Ele

JQuery源码分析 --- 运动animate 入队出队

我们都知道,所谓的运动就是操作定时器,但是如果我同时写3个运动,比如下面这样,效果会怎样呢? <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #div1{ width: 300px; height: 300px; background: red