队列的链式存储(时间复杂度最小)

/*
 * 2015年4月17日14:04:56
 * 目的:用链式存储来实现队列
 * 这里我最开始想使用单链表来实现
 * 大家来想一下啊,其实单链表实现不是特别好
 * 因为虽然出队列的时间复杂度就是O(1),但是
 * 入队列的时间复杂度却是O(n),因为每次都是从末尾进行插入
 * 从末尾插入你首先就要找到当前指向尾指针的结点,由于链表是单向的
 * 所以必须从front开始进行遍历才能找到rear前一个结点。
 * 为了解决入队列这个时间复杂度的问题,我觉得可以使用双向链表来解决
 * 虽然这样会浪费一些内存空间,但是可以解决时间复杂度的问题。
*/

# include <stdio.h>

struct LinkNode{
	int data;
	LinkNode *next;
	LinkNode *prior;
};

struct LinkQueue{
	LinkNode *front;//队头指针
	LinkNode *rear; //队尾指针
};

//定义一个int变量,表示队列的长度
int length = 0;

void initQueue(LinkQueue *queue)
{
	queue->front = new LinkNode; //这个相当于链表的头指针。
	queue->rear = new LinkNode; //相当于链表的尾指针。
	queue->front->next = queue->rear;
	queue->front->prior = NULL;
	queue->rear->next = NULL;
	queue->rear->prior = queue->front;
}

//判断队列是否为空
bool isEmpty(LinkQueue *queue)
{
	/*
	 * 其实判断为空还可以用queue->front == queue->rear == NULL
	 * 来判断,但是既然我这里设置了length全局变量,就想要好好利用
	*/

	if(length == 0)
		return true;
	else
		return false;
}

//进队列
void enterQueue(LinkQueue *queue,int value)
{
	LinkNode *temp = new LinkNode;
	temp->data = value; //把想要插入的值赋给一个新的结点

	/*将结点插入到尾结点和尾结点前面的元素之间,时间复杂度是O(1)*/

	temp->next = queue->rear;
	temp->prior = queue->rear->prior;
	queue->rear->prior->next = temp;
	queue->rear->prior = temp;

	length++;

}

//出队列
void deleteQueue(LinkQueue *queue,int *value)
{
	*value = queue->front->next->data;

	//出队列是从头部开始出
	queue->front->next = queue->front->next->next;
	queue->front->next->prior = queue->front;

	length--;
}

//打印出队列中所有的元素
void printQueue(LinkQueue *queue)
{
	if(isEmpty(queue))
	{
		printf("队列中无元素。\n");
	}

	else
	{
		printf("队列中元素为:\n");
		LinkNode *temp = queue->front->next;
		for(int i = 1;i <= length;i++)
		{
			printf("%4d ",temp->data);
			if(i % 10 == 0)
			{
				printf("\n");
			}
			temp = temp->next;
		}
		printf("\n");
	}

}

int main(void)
{
	LinkQueue queue;
	initQueue(&queue);
	for(int i = 1;i <= 20;i++)
		enterQueue(&queue,i);
	printQueue(&queue);

	int value = 0;
	deleteQueue(&queue,&value);
	deleteQueue(&queue,&value);
	printQueue(&queue);
	return 0;
}

时间: 2024-08-06 12:53:52

队列的链式存储(时间复杂度最小)的相关文章

[转载]队列的链式存储

申明:转自    http://www.cnblogs.com/Romi/archive/2012/08/28/2660954.html 1 //队列的链式存储.C 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<malloc.h> 5 6 //定义队列 7 typedef struct node { 8 int data; 9 struct node *next; 10 }Queue; 11 12 //定义对手指

队列的链式存储---链表实现

/* 队列的链式存储 */ /* with no header */ struct Node; struct LinkQueue; typedef struct Node *PtrToNode; typedef struct LinkQueue *PtrTorf; struct Node{ ElementType X; PtrToNode Next; }; struct LinkQueue{ PtrToNode rear; PtrToNode front; }; int IsEmpty(PtrT

数据结构-队列-顺序链式存储

定义 队列(Queue):队列简称队,也是一种操作受限的线性表,只允许在表的一端进行插入,而在表的另一端进行删除.向队列中插入元素称为入队或进队:删除元素称为出队或离队. 队列的操作 队列不可以读取对中间的元素. 队列的存储结构 顺序存储 链式存储 顺序存储 队列的顺序实现是指分配一块连续的存储单元存放队列中的元素,并附设两个指针front 和rear分别指示队头元素和队尾元素的位置. 设队头指针指向队头元素,队尾指针指向队尾 元素的下一个位置(也可以让rear指向队尾元素,front指向队头元

数据结构:队列的链式存储结构

链队列的实现方法: 队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已,简称为链队列.为了操作上的方便,我们将队头指针指向链队列的头节点,而队尾指针指向终端节点.空队列时,front和rear都指向头节点. 注意:这里的实现是有头结点的,在队列的初始化函数中要为头结点开辟空间. 链队列的实现代码: #include <iostream> #include <stdlib.h> using namespace std; /**********************

队列的链式存储---链表实现(有头结点)

/* 队列的链式存储 */ /* with header */ /* with no typedef */ struct Node{ ElementType Ele; struct Node *Next; }; struct LinQ{ struct Node *rear; struct Node *front; }; struct LinQ * CreateQ( void ) { struct LinQ *Ptr; struct Node *header; Ptr = malloc(sizeo

队列的链式存储结构及实现

ref : https://blog.csdn.net/qq_29542611/article/details/78907339 队列的链式存储结构,其实就是线性表的单链表,只不过它只是尾进头出而已,我们把它简称为链队列.为了操作上的方便,我们将队头指针指向链队列的头结点,而队尾指针指向终端节点.如果 空队列时,front和rear都指向头结点. 入队操作: 在队尾添加元素,先将队尾元素的next指向添加的元素,然后将队尾指针重新指向新的队尾即可. 出队操作: 头结结点指向的结点即为队头结点,出

队列(FIFO)—循环队列、队列的链式存储

1 队列的定义 队列是只允许在一端(队尾)进行插入操作,而在另一端(队头)进行删除操作的线性表. 2 队列的特点 1)先进先出是队列最大的特点,是应用中非常常见的模型,例如排队: 2)队列也属于线性表,线性表的特性队列都拥有. 3 循环队列的实现及关键点 3.1 关键点 1)队列为空的条件:队头指针等于队尾指针,即head == tial: 2)队列中保留一个元素空间,当队列满时,尾指针和头指针之间还剩一个元素空间.队列为满的条件:(tial + 1) % quenceSize == head:

队列的链式存储结构(C语言实现)

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define OK 1 5 #define ERR 2 6 #define TRUE 1 7 #define FALSE 0 8 9 typedef int status; //定义函数返回的状态,OK & ERR 10 typedef char datatype; //定义队列中每个元素的数据类型,这里暂定为字符型 11 12 typedef struct LinkQueue_

顺序循环队列和链式存储队列(带头结点和不带头结点)

1.顺序存储的循环队列 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdbool.h> 4 5 typedef int ElementType; 6 typedef int Position; 7 typedef struct QNode* PtrToNode; 8 struct QNode { 9 ElementType *Data; 10 Position Front, Rear; 11 int M