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

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 MaxSize;
12
13 };
14 typedef PtrToNode Queue;
15
16
17 //创建一个空队列
18 Queue CreateQueue(int MaxSize)
19 {
20     Queue Q = (Queue)malloc(sizeof(struct QNode));
21     Q->Data = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
22     Q->Front = Q->Rear = 0;
23     Q->MaxSize =  MaxSize;
24     return Q;
25 }
26
27 //判断队列是否已满
28 bool IsFull(Queue Q)
29 {
30     return ((Q->Rear + 1) % Q->MaxSize == Q->Front);
31 }
32
33 bool AddQ(Queue Q, ElementType X)
34 {
35     if (IsFull(Q))
36     {
37         printf("The queue is full!\n");
38         return false;
39     }
40     else
41     {
42         Q->Data[Q->Rear] = X;
43         Q->Rear = (Q->Rear + 1) % Q->MaxSize;
44
45         return true;
46     }
47 }
48
49 //判断队列是否为空
50 bool IsEmpty(Queue Q)
51 {
52     return Q->Front == Q->Rear;
53 }
54
55 //删除一个元素
56 ElementType DeleteQ(Queue Q)
57 {
58     ElementType elem;
59     if (IsEmpty(Q))
60     {
61         printf("The queue is empty!\n");
62         return -999;
63     }
64     else
65     {
66         elem = Q->Data[Q->Front];
67         Q->Front = (Q->Front + 1) % Q->MaxSize;
68         return elem;
69     }
70 }
71
72 int main()
73 {
74     Queue Q = CreateQueue(10);
75
76     for (int i = 0; i < 10; i++)
77         AddQ(Q, i);
78     for (int i = 0; i < 10; i++)
79         printf("%d ", DeleteQ(Q));
80     AddQ(Q, 10);
81     printf("%d ", DeleteQ(Q));
82     return 0;
83 }

2.不带头结点的链式存储队列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4
 5 typedef int ElementType;
 6 typedef struct Node* PtrToNode;
 7 struct Node {        //队列中的结点
 8     ElementType Data;
 9     PtrToNode Next;
10 };
11
12 typedef struct QNode* PtrToQNode;
13 typedef struct Node* Position;
14 struct QNode {
15     Position Front, Rear;    //队列的头尾指针
16     //int MaxSize;            //队列的最大容量
17 };
18
19 typedef PtrToQNode Queue;
20
21 Queue CreateQueue()
22 {
23     Queue Q = (Queue)malloc(sizeof(struct QNode));
24     Q->Front = Q->Rear = NULL;
25     //Q->MaxSize = MaxSize;
26     return Q;
27 }
28
29 void InsertQ(Queue Q, ElementType X)
30 {
31     PtrToNode cell = (PtrToNode)malloc(sizeof(struct Node));
32     cell->Data = X;
33     cell->Next = NULL;
34     if (!Q->Front)
35     {
36         Q->Front = Q->Rear = cell;
37     }
38     else
39     {
40         Q->Rear->Next = cell;
41         Q->Rear = cell;
42     }
43 }
44
45 bool IsEmpty(Queue Q)
46 {
47     return (Q->Front == NULL);
48 }
49
50 ElementType DeleteQ(Queue Q)
51 {
52     Position FrontCell;
53     ElementType FrontElem;
54     if (IsEmpty(Q))
55     {
56         printf("The queue is empty!\n");
57         return -999;
58     }
59     else
60     {
61         FrontCell = Q->Front;
62         if (Q->Front == Q->Rear)
63             Q->Front = Q->Rear = NULL;
64         else
65         Q->Front = Q->Front->Next;
66
67
68         FrontElem = FrontCell->Data;
69         free(FrontCell);
70         return FrontElem;
71     }
72 }
73
74 int main()
75 {
76     Queue Q = CreateQueue();
77     for (int i = 0; i < 10; i++)
78         InsertQ(Q, i);
79     for (int i = 0; i < 10; i++)
80         printf("%d ", DeleteQ(Q));
81     DeleteQ(Q);
82
83     return 0;
84 }

3.带头结点的链式存储队列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4
 5 typedef int ElementType;
 6 typedef struct Node* PtrToNode;
 7 struct Node {        //队列中的结点
 8     ElementType Data;
 9     PtrToNode Next;
10 };
11
12 typedef struct QNode* PtrToQNode;
13 typedef struct Node* Position;
14 struct QNode {
15     Position Front, Rear;    //队列的头尾指针
16     //int MaxSize;            //队列的最大容量
17 };
18
19 typedef PtrToQNode Queue;
20
21 Queue CreateQueue()
22 {
23     Queue Q = (Queue)malloc(sizeof(struct QNode));
24     Q->Front = Q->Rear = (PtrToNode)malloc(sizeof(struct Node));
25     Q->Front->Next = NULL;
26     //Q->MaxSize = MaxSize;
27     return Q;
28 }
29
30 void InsertQ(Queue Q, ElementType X)
31 {
32     PtrToNode cell = (PtrToNode)malloc(sizeof(struct Node));
33     cell->Data = X;
34     cell->Next = NULL;
35     Q->Rear->Next = cell;
36     Q->Rear = cell;
37 }
38
39 bool IsEmpty(Queue Q)
40 {
41     return (Q->Front->Next == NULL);
42 }
43
44 ElementType DeleteQ(Queue Q)
45 {
46     Position FrontCell;
47     ElementType FrontElem;
48     if (IsEmpty(Q))
49     {
50         printf("The queue is empty!\n");
51         return -999;
52     }
53     else
54     {
55         FrontCell = Q->Front->Next;
56         if (Q->Rear == Q->Front->Next)
57         {
58             Q->Rear = Q->Front;
59             Q->Front->Next = NULL;
60         }
61         else
62             Q->Front->Next = FrontCell->Next;
63
64
65         FrontElem = FrontCell->Data;
66         free(FrontCell);
67         return FrontElem;
68     }
69 }
70
71 int main()
72 {
73     Queue Q = CreateQueue();
74     for (int i = 0; i < 10; i++)
75         InsertQ(Q, i);
76     for (int i = 0; i < 10; i++)
77         printf("%d ", DeleteQ(Q));
78     DeleteQ(Q);
79
80     return 0;
81 }

原文地址:https://www.cnblogs.com/hi3254014978/p/9743377.html

时间: 2024-08-29 09:12:00

顺序循环队列和链式存储队列(带头结点和不带头结点)的相关文章

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

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

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

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

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

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

队列的链式存储结构(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_

[转载]队列的链式存储

申明:转自    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

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

/* 队列的链式存储 */ /* 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

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

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

队列的链式存储结构

1 链队列的存储结构 将对头指针front指向链队列的头结点,队尾指针rear指向终端结点. 空队列时,头指针front和尾指针rear都指向头结点. 链队列的存储结构为: typedef int QElemType; typedef struct QNode { //结点结构 QElemType data; struct QNode *next; }QNode; typedef struct QNode * QueuePtr; typedef struct { //队列的链表结构 QueueP