用C/C++寫的佇列在VS2012上執行成功
阿新 • • 發佈:2019-01-30
這是我第一次自己寫佇列,以前都是看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; }