扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define SMALL 1
#if(SMALL)
#define MAX 7
int QueueData[MAX] = {'A','B','C','D','E','F','G'};
#else
#define MAX 14
int QueueData[MAX] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N'};
#endif
/*顺序队列类型定义*/
typedef struct
{
int data[MAX];
int head;
int tail;
}sqQueue;
/*链队列类型定义*/
struct LinkQueue
{
int data;
struct LinkQueue *link;
};
typedef struct LinkQueue lkQueue;
//-----------------------------------------------------------------------------------------
/*顺序队列基本操作函数定义部分*/
//顺序队列的初始化函数sqInitialize()
sqQueue *sq_Initialize()
{
sqQueue *p;
p = (sqQueue *)malloc(sizeof(sqQueue));
if(p==NULL)
{
return(NULL);
}
else
{
p->data[0] = 0;
p->head = 0;
p->tail = 0;
return(p);
}
}
//顺序队列的判空函数qIsEmpty()
int qIsEmpty(sqQueue *queue)
{
if(queue->head==queue->tail)
return(1);
else
return(0);
}
//顺序队列的判满函数qIsFull()
int qIsFull(sqQueue *queue)
{
if(queue->head==MAX)
return(1);
else
return(0);
}
//顺序队列的入队函数qInQueue()
int qInQueue(sqQueue *queue,int data)
{
if(queue->tail==MAX)
{
printf("队列已满!\n");
return(0);
}
else
{
queue->data[queue->tail++] = data;
return(1);
}
}
//顺序队列的出队函数qOutQueue()
int qOutQueue(sqQueue *queue,int *p)
{
if(queue->head==queue->tail)
{
printf("队列已空!\n");
return(0);
}
else
{
*p = queue->data[queue->head++];
return(1);
}
}
//顺序队列的数据入队操作函数qInputValue()
void qInputValue(sqQueue *queue,int array[])
{
int i=0;
while(qInQueue(queue,array[i]))
printf("Queue[%d] = %c\t",i,array[i++]);
}
//顺序队列的数据出队操作函数qOutputValue()
void qOutputValue(sqQueue *queue)
{
int i,QueueData;
i=0;
while(qOutQueue(queue,&QueueData))
printf("Queue[%d] = %c\t",i++,QueueData);
printf("\n");
}
//-----------------------------------------------------------------------------------------
/*链式队列基本操作函数定义部分*/
//链式队列的初始化函数lk_Initialize()
lkQueue *lk_Initialize()
{
lkQueue *p;
p = (lkQueue *)malloc(sizeof(lkQueue));
if(p==NULL)
{
return(NULL);
}
else
{
p->data = 0;
p->link = NULL;
return(p);
}
}
//链式队列的入队函数lkInQueue()
//注意:在入队时,参数是队尾指针,返回值也是队尾指针
lkQueue *lkInQueue(lkQueue *tail,int data)
{
lkQueue *p;
p = (lkQueue *)malloc(sizeof(lkQueue));
if(p==NULL)
{
printf("新增链式队列元素时内存溢出!\n");
return(tail);
}
else
{
p->data = data;
p->link = tail;
return(p);
}
}
//链式队列的出队函数lkInQueue()
//注意:在出队时,参数是队首指针,返回值也是队首指针
lkQueue *lkOutQueue(lkQueue *head,int *data)
{
*p = head->data;
lkQueue *p;
p = (lkQueue *)malloc(sizeof(lkQueue));
if(p==NULL)
{
printf("创建链式队列时内存溢出!\n");
return(tail);
}
else
{
p->data = data;
p->link = tail;
return(p);
}
}
//-----------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
sqQueue *queue1;
lkQueue **lkHead,**lkTail;
queue1 = sq_Initialize();
if(queue1==NULL)
{
printf("创建顺序队列时内存溢出!\n");
return 0;
}
else
{
printf("\n顺序队列[入队]数据.........\n");
qInputValue(queue1,QueueData);
printf("\n顺序队列[出队]数据.........\n");
qOutputValue(queue1);
}
printf("\n运行完毕!\n");
return 0;
}
濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ巻鍋撶紓宥咃躬楠炲啫螣鐠囪尙绐為梺褰掑亰閸撴盯鎮惧ú顏呪拺闂傚牊鍗曢崼銉ョ柧婵犲﹤瀚崣蹇旂節婵犲倻澧涢柛瀣ㄥ妽閵囧嫰寮介妸褋鈧帡鏌熼挊澶婃殻闁哄瞼鍠栭幃婊堝煛閸屾稓褰嬮柣搴ゎ潐濞叉ê鐣濈粙璺ㄦ殾闁割偅娲栭悡娑㈡煕鐏炲墽鐭嬫繛鍫熸倐濮婄粯鎷呯粵瀣異闂佹悶鍔嬮崡鍐茬暦閵忋倕鍐€妞ゆ劑鍎卞皬闂備焦瀵х粙鎴犫偓姘煎弮瀹曚即宕卞Ο闀愮盎闂侀潧鐗嗛幊搴㈡叏椤掆偓閳规垿鍩ラ崱妞剧凹濠电姰鍨洪敋閾荤偞淇婇妶鍛櫤闁稿鍊圭换娑㈠幢濡纰嶉柣搴㈣壘椤︾敻寮诲鍫闂佸憡鎸鹃崰搴敋閿濆鏁嗗〒姘功閻绻涢幘鏉戠劰闁稿鎹囬弻锝呪槈濞嗘劕纾抽梺鍝勬湰缁嬫垿鍩為幋锕€宸濇い鏇炴噺閳诲﹦绱撻崒娆戝妽妞ゃ劌鎳橀幆宀勫磼閻愰潧绁﹂柟鍏肩暘閸斿矂鎮為崹顐犱簻闁圭儤鍨甸鈺呮倵濮橆剦妲归柕鍥у瀵粙濡歌閸c儳绱撴担绛嬪殭婵☆偅绻堝濠氭偄绾拌鲸鏅i悷婊冪Ч閹﹢鎳犻鍌滐紲闁哄鐗勯崝搴g不閻愮儤鐓涢悘鐐跺Г閸犳﹢鏌℃担鐟板鐎规洜鍠栭、姗€鎮╅搹顐ら拻闂傚倷娴囧畷鍨叏閹惰姤鈷旂€广儱顦崹鍌炴煢濡尨绱氶柨婵嗩槸缁€瀣亜閺嶃劎鈽夋繛鍫熺矒濮婅櫣娑甸崨顔俱€愬銈庡亝濞茬喖宕洪埀顒併亜閹哄棗浜鹃梺鎸庢穿婵″洤危閹版澘绫嶉柛顐g箘椤撴椽姊虹紒妯哄鐎殿噮鍓欒灃闁告侗鍠氶崢鎼佹⒑閸撴彃浜介柛瀣閹﹢鏁冮崒娑氬幈闁诲函缍嗛崑鍡樻櫠椤掑倻纾奸柛灞剧☉缁椦囨煙閻熸澘顏柟鐓庢贡閹叉挳宕熼棃娑欐珡闂傚倸鍊风粈渚€骞栭銈傚亾濮樺崬鍘寸€规洖缍婇弻鍡楊吋閸涱垽绱遍柣搴$畭閸庨亶藝娴兼潙纾跨€广儱顦伴悡鏇㈡煛閸ャ儱濡煎褜鍨伴湁闁绘ǹ绉鍫熺畳闂備焦瀵х换鍌毼涘Δ鍛厺闁哄洢鍨洪悡鍐喐濠婂牆绀堟慨妯挎硾閽冪喖鏌曟繛褍瀚烽崑銊╂⒑缂佹ê濮囨い鏇ㄥ弮閸┿垽寮撮姀鈥斥偓鐢告煥濠靛棗鈧懓鈻嶉崶銊d簻闊洦绋愰幉楣冩煛鐏炵偓绀嬬€规洟浜堕、姗€鎮㈡總澶夌处