这是我第一次自己写队列,以前都是看Linux内核源码中的队列,看得多写得少,现在的驱动基本不需要自己写太多代码,只需要你懂原理会修改就OK。
好了废话不多说我们进入正题吧!
下面是我写的两个代码,一个是数字的另一个是字符串的:
一.数字或字符型
//SqQueue.h
#pragma once
#include "stdafx.h"
#include "iostream"
//队列的最大数据长度
#define MAXSIZE 4096
typedef char ElemType;
//队列的数据单元
typedef struct {
ElemType data[MAXSIZE]; //队列数据
int front,rear; //队列头和尾
}SqQueue;
//SqQueue.cpp
//初始化队列 void Init_JstrQueue(SqQueue &JstrQueue){ JstrQueue.front = 0;JstrQueue.rear = 0; }//销毁队列void Destroy_JstrQueue(SqQueue &Q){ if(Q.data){ free(Q.data); Q.front = Q.rear = 0; }}//清空队列void Clear_JstrQueue(SqQueue &Q){ Q.front = Q.rear = 0;}//判断队列int IsEmpty_JstrQueue(SqQueue &Q){ return (Q.rear == Q.front);}//判断队列int iSFull_JstrQueue(SqQueue &Q){ return ((Q.rear + 1) % MAXSIZE == Q.front);}//获取队列长度int GetLength_JstrQueue(SqQueue &Q){ return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;}//取得队列的的队头void GetHead_JstrQueue(SqQueue &Q,ElemType *x){ if(IsEmpty_JstrQueue(Q)){ printf("顺序队列空!\n"); exit(0); } else{ *x = Q.data[Q.front]; }}//取得队列的的队尾void GetRear_JstrQueue(SqQueue &Q,ElemType *x){ if(IsEmpty_JstrQueue(Q)){ printf("顺序队列空!\n"); exit(0); } else{ *x = Q.data[(Q.rear - 1 + MAXSIZE) % MAXSIZE]; }}//插入队列void En_JstrQueue(SqQueue &Q,ElemType x){ if(iSFull_JstrQueue(Q)){ printf("顺序队列已满!\n"); exit(0); } else{ Q.data[Q.rear] = x; Q.rear = (Q.rear + 1) % MAXSIZE; } }//出队列void De_JstrQueue(SqQueue &Q,ElemType *x){ if(IsEmpty_JstrQueue(Q)){ printf("顺序队列空!\n"); exit(0); } else{ *x = Q.data[Q.front]; Q.front = (Q.front + 1) % MAXSIZE; } }//打印队列void Print_JstrQueue(SqQueue &Q){ int i = 0; int j = Q.front; if(IsEmpty_JstrQueue(Q)){std::cout<<"顺序队列空!\n"<<std::endl; exit(0); } else{ while(i < GetLength_JstrQueue(Q)){ std::cout<<Q.data[j]<<std::endl; j = (j + 1) % MAXSIZE; i++; } std::cout<<std::endl; }}
//主函数
int main(int argc, _TCHAR* argv[]) { SqQueue Jstr; int str4[2] = {0}; int str5[2] = {0}; int str6[2] = {0}; Init_JstrQueue(Jstr); En_JstrQueue(Jstr, 12); En_JstrQueue(Jstr, 23); En_JstrQueue(Jstr, 34); Print_JstrQueue(Jstr); std::cout<<"开始取队列中的1!\n"<<std::endl; De_JstrQueue(Jstr, str4); Print_JstrQueue(Jstr); std::cout<<"开始取队列中的2!\n"<<std::endl; De_JstrQueue(Jstr, str5); Print_JstrQueue(Jstr); std::cout<<"开始取队列中的3!\n"<<std::endl; De_JstrQueue(Jstr, str6); Print_JstrQueue(Jstr); std::cin.get(); std::cin.get(); return 0; }
二:字符串
//SqQueue.h //顺序队列的类型描述 #pragma once #include "stdafx.h" #include "stdlib.h" #include "iostream" #define MAXSIZE 5 typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; int front,rear; }SqQueue; //SqQueue.cpp //初始化顺序队列(循环队列) Init_SqQueue(); void Init_SqQueue(SqQueue &JstrQueue) { JstrQueue.front = 0; JstrQueue.rear = 0; } //销毁顺序队列(循环队列)Destroy_SqQueue(SqQueue* Q); void Destroy_SqQueue(SqQueue &Q) { if(Q.data) { free(Q.data); Q.front = Q.rear = 0; } } //清空顺序队列(循环队列)Clear_SqQueue(SqQueue* Q); void Clear_SqQueue(SqQueue &Q) { Q.front = Q.rear = 0; } //判断顺序队列(循环队列)是否为空IsEmpty_SqQueue(SqQueue* Q) int IsEmpty_SqQueue(SqQueue &Q) { return (Q.rear == Q.front); } //判断顺序队列(循环队列)是否已满 iSFull_SqQueue(SqQueue* Q); int iSFull_SqQueue(SqQueue &Q) { return ((Q.rear + 1) % MAXSIZE == Q.front); } //求得顺序队列(循环队列)的长度GetLength_SqQueue(SqQueue* Q); int GetLength_SqQueue(SqQueue &Q) { return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; } //取得顺序队列(循环队列)的的队头GetHead_SqQueue(SqQueue* Q,ElemType *x); void GetHead_SqQueue(SqQueue &Q,ElemType *x) { if(IsEmpty_SqQueue(Q)) { printf("顺序队列空!\n"); exit(0); } else { *x = Q.data[Q.front]; } } //取得顺序队列(循环队列)的的队尾GetRear_SqQueue(SqQueue* Q,ElemType *x); void GetRear_SqQueue(SqQueue &Q,ElemType *x) { if(IsEmpty_SqQueue(Q)) { printf("顺序队列空!\n"); exit(0); } else { *x = Q.data[(Q.rear - 1 + MAXSIZE) % MAXSIZE]; } } //入顺序队列(循环队列)En_SqQueue(SqQueue* Q,ElemType x); void En_SqQueue(SqQueue &Q,ElemType *x) { int len = strlen(x); if(iSFull_SqQueue(Q)) { printf("顺序队列已满!\n"); exit(0); } else { for(int i=0; i<len; i++) { Q.data[Q.rear] = x[i]; Q.rear = (Q.rear + 1) % MAXSIZE; } Q.data[Q.rear] = '\0'; Q.rear = (Q.rear + 1) % MAXSIZE; } } //出顺序队列(循环队列)De_SqQueue(SqQueue* Q,ElemType *x); void De_SqQueue(SqQueue &Q,ElemType *x) { int i = 0; int j = 0; if(IsEmpty_SqQueue(Q)) { printf("顺序队列空!\n"); exit(0); } else { if(Q.data[Q.front] == '\0') { j = 1; } while(Q.data[Q.front] != '\0') { if(Q.data[Q.front] != '\0') { x[i] = Q.data[Q.front]; Q.front = (Q.front + 1) % MAXSIZE; i++; } } if(j == 1) { while(Q.data[Q.front + 1] != '\0') { if(Q.data[Q.front + 1] != '\0') { x[i] = Q.data[Q.front + 1]; Q.front = (Q.front + 2) % MAXSIZE; i++; } } x[i] = Q.data[Q.front]; Q.front = (Q.front + 1) % MAXSIZE; j++; } } } //打印顺序队列(循环队列)Print_SqQueue(SqQueue* Q); void Print_SqQueue(SqQueue &Q) { int i = 0; int j = Q.front; if(IsEmpty_SqQueue(Q)) { std::cout<<"顺序队列空!\n"<<std::endl; exit(0); } else { while(i < GetLength_SqQueue(Q)) { std::cout<<Q.data[j]; j = (j + 1) % MAXSIZE; i++; } std::cout<<std::endl; } } //主函数 int main(int argc, _TCHAR* argv[]) { SqQueue Jstr; char *str1 = "abfdsdkfhwiosggq"; char *str2 = "kldhfowtrgwaghsw"; char *str3 = "fdgbpsgtrmifdvbsg"; char str4[56] = {0}; char str5[56] = {0}; char str6[56] = {0}; Init_SqQueue(Jstr); En_SqQueue(Jstr, str1); En_SqQueue(Jstr, str2); En_SqQueue(Jstr, str3); Print_SqQueue(Jstr); std::cout<<"开始取队列中的str1字符串!\n"<<std::endl; De_SqQueue(Jstr, str4); Print_SqQueue(Jstr); std::cout<<"开始取队列中的str2字符串!\n"<<std::endl; De_SqQueue(Jstr, str5); Print_SqQueue(Jstr); std::cout<<"开始取队列中的str3字符串!\n"<<std::endl; De_SqQueue(Jstr, str6); Print_SqQueue(Jstr); std::cin.get(); std::cin.get(); return 0; }