数据结构与算法——队列(顺序存储)
队列 (Queue)概念
它是一种运算受限的线性表,先进先出(FIFO First In First Out),它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。
采用数组来保存队列的元素,设立一个队首指针 front ,一个队尾指针 rear,分别指向队首和队尾元素。则 rear-front 即为存储的元素个数!
1 #define MaxSize 5 //队列的最大容量 2 typedef int DataType; //队列中元素类型 3 4 typedef struct Queue 5 { 6 DataType queue[MaxSize]; 7 int front; //队头指针 8 int rear; //队尾指针 9 }SeqQueue;
队列的初始化
1 //队列初始化,将队列初始化为空队列 2 void InitQueue(SeqQueue* SQ) 3 { 4 if (!SQ) return; 5 SQ->front = SQ->rear = NULL; //把对头和队尾指针同时置0 6 }
队列为空
1 //判断队列为空 2 int IsEmpty(SeqQueue* SQ) 3 { 4 if (!SQ) return 0; 5 if (SQ->front == SQ->rear) 6 { 7 return 1; 8 } return 0; 9 }
队列为满
1 //判断队列是否为满 2 int IsFull(SeqQueue *SQ) 3 { if(!SQ) return 0; 4 if (SQ->rear == MaxSize) 5 { return 1; 6 } return 0; 7 }
入队
将新元素插入 rear 所指的位置,然后 rear 加 1。
1 //入队,将元素data插入到队列SQ中 2 int EnterQueue(SeqQueue* SQ, DataType data) 3 { 4 if (!SQ) return 0; 5 if (IsFull(SQ)) 6 { 7 cout << "无法插入元素 " << data << ", 队列已满!" << endl; return 0; 8 } 9 SQ->queue[SQ->rear] = data; //在队尾插入元素data 10 SQ->rear++; //队尾指针后移一位 11 12 return 1; 13 }
打印队列中的元素
1 void PrintQueue(SeqQueue* SQ) 2 { 3 if (!SQ) return; 4 int i = SQ->front; while (i < SQ->rear) 5 { 6 cout << setw(4) << SQ->queue[i]; i++; 7 } 8 cout << endl; 9 }
出队方式一
删除 front 所指的元素, 后面所有元素前移 1 并返回被删除元素
1 int DeleteQueue(SeqQueue * SQ, DataType * data) 2 { 3 if (!SQ || IsEmpty(SQ)) 4 { 5 cout << "队列为空!" << endl; 6 return 0; 7 } if (!data) return 0; 8 *data = SQ->queue[SQ->front]; 9 for (int i = SQ->front + 1; i < SQ->rear; i++) 10 { 11 //移动后面的元素 12 SQ->queue[i-1]=SQ->queue[i]; 13 } 14 SQ->rear--; //队尾指针前移一位 15 return 1; 16 }
出队方式二
删除 front 所指的元素,然后加 1 并返回被删元素。
1 int DeleteQueue2(SeqQueue * SQ, DataType * data) 2 { 3 if (!SQ || IsEmpty(SQ)) 4 { 5 cout << "队列为空!" << endl; 6 return 0; 7 } 8 if (SQ->front >= MaxSize) { 9 cout << "队列已到尽头!" << endl; 10 return 0; 11 } 12 *data = SQ->queue[SQ->front]; //出队元素值 13 SQ->front = (SQ->front)+1; //队首指针后移一位 14 return 1; 15 }
取队首元素
返回 front 指向的元素值
1 int GetHead(SeqQueue* SQ, DataType* data) 2 { 3 if (!SQ || IsEmpty(SQ)) 4 { 5 cout << "队列为空!" << endl; 6 } 7 return *data = SQ->queue[SQ->front]; 8 }
清空队列
1 void ClearQueue(SeqQueue* SQ) 2 { 3 if (!SQ) return; 4 SQ->front = SQ->rear = 0; 5 }
获取队列中元素的个数
1 int getLength(SeqQueue* SQ) 2 { 3 if (!SQ) return 0; 4 return SQ->rear - SQ->front; 5 }
完整代码实现
1 #include <stdio.h> 2 #include <assert.h> 3 #include <Windows.h> 4 #include <iostream> 5 #include <iomanip> 6 7 using namespace std; 8 9 #define MaxSize 5 //队列的最大容量 10 typedef int DataType; //队列中元素类型 11 12 typedef struct Queue 13 { 14 DataType queue[MaxSize]; 15 int front; //队头指针 16 int rear; //队尾指针 17 }SeqQueue; 18 19 20 //队列初始化,将队列初始化为空队列 21 void InitQueue(SeqQueue* SQ) 22 { 23 if (!SQ) return; 24 SQ->front = SQ->rear = 0; //把对头和队尾指针同时置0 25 } 26 27 //判断队列为空 28 int IsEmpty(SeqQueue* SQ) 29 { 30 if (!SQ) return 0; 31 if (SQ->front == SQ->rear) 32 { 33 return 1; 34 } return 0; 35 } 36 37 //判断队列是否为满 38 int IsFull(SeqQueue* SQ) 39 { 40 if (!SQ) return 0; 41 if (SQ->rear == MaxSize) 42 { 43 return 1; 44 } return 0; 45 } 46 47 //入队,将元素data插入到队列SQ中 48 int EnterQueue(SeqQueue* SQ, DataType data) 49 { 50 if (!SQ) return 0; 51 if (IsFull(SQ)) 52 { 53 cout << "无法插入元素 " << data << ", 队列已满!" << endl; return 0; 54 } 55 SQ->queue[SQ->rear] = data; //在队尾插入元素data 56 SQ->rear++; //队尾指针后移一位 57 58 return 1; 59 } 60 61 //打印队列中的各元素 62 void PrintQueue(SeqQueue* SQ) 63 { 64 if (!SQ) return; 65 int i = SQ->front; while (i < SQ->rear) 66 { 67 cout << setw(4) << SQ->queue[i]; i++; 68 } 69 cout << endl; 70 } 71 72 //出队,将队列中队头的元素data出队,后面的元素向前移动 73 int DeleteQueue(SeqQueue * SQ, DataType * data) 74 { 75 if (!SQ || IsEmpty(SQ)) 76 { 77 cout << "队列为空!" << endl; 78 return 0; 79 } if (!data) return 0; 80 *data = SQ->queue[SQ->front]; 81 for (int i = SQ->front + 1; i < SQ->rear; i++) 82 { 83 //移动后面的元素 84 SQ->queue[i-1]=SQ->queue[i]; 85 } 86 SQ->rear--; //队尾指针前移一位 87 return 1; 88 } 89 90 //出队2,将队列中队头的元素data出队,出队后队头指针front后移一位 91 int DeleteQueue2(SeqQueue * SQ, DataType * data) 92 { 93 if (!SQ || IsEmpty(SQ)) 94 { 95 cout << "队列为空!" << endl; 96 return 0; 97 } 98 if (SQ->front >= MaxSize) { 99 cout << "队列已到尽头!" << endl; 100 return 0; 101 } 102 *data = SQ->queue[SQ->front]; //出队元素值 103 SQ->front = (SQ->front)+1; //队首指针后移一位 104 return 1; 105 } 106 107 //获取队首元素,返回 front 指向的元素值 108 int GetHead(SeqQueue* SQ, DataType* data) 109 { 110 if (!SQ || IsEmpty(SQ)) 111 { 112 cout << "队列为空!" << endl; 113 } 114 return *data = SQ->queue[SQ->front]; 115 } 116 117 //清空队列 118 void ClearQueue(SeqQueue* SQ) 119 { 120 if (!SQ) return; 121 SQ->front = SQ->rear = 0; 122 } 123 124 //获取队列中元素的个数 125 int getLength(SeqQueue* SQ) { 126 if (!SQ) return 0; 127 return SQ->rear - SQ->front; 128 } 129 130 int main() 131 { 132 SeqQueue* SQ = new SeqQueue; 133 DataType data = -1; 134 135 //初始化队列 136 InitQueue(SQ); 137 138 //入队 139 for (int i = 0; i < 7; i++) 140 { 141 EnterQueue(SQ, i); 142 } 143 144 //打印队列中的元素 11 145 printf("队列中的元素(总共%d 个):", getLength(SQ)); 146 147 PrintQueue(SQ); 148 cout << endl; 149 150 ////出队 151 //for(int i=0; i<10; i++) 152 //{ 153 // if(DeleteQueue2(SQ, &data)) 154 // { 155 // cout<<"出队的元素是:"<<data<<endl; 156 // } 157 // else 158 // { 159 // cout << "出队失败!" << endl; 160 // } 161 //} 162 163 //打印队列中的元素 164 printf("出队一个元素后,队列中剩下的元素:"); 165 PrintQueue(SQ); 166 cout << endl; 167 system("pause"); 168 return 0; 169 }
===================================================================================================================