當前位置:首頁 » 編程語言 » c語言非同步隊列
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言非同步隊列

發布時間: 2022-07-17 21:27:49

① 數據結構(c語言版)隊列基本操作的實現

/***************/
/* 鏈式隊列 */
/***************/
#include "stdlib.h"
#include "stdio.h"

/* 定義鏈式隊列類型 */
typedef int ElemType;
typedef struct QNode
{ ElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct
{ QueuePtr front;
QueuePtr rear;
} LinkQueue;

/* 1、初始化鏈式隊列 */
void InitQueue(LinkQueue *Q)
{ Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if (!(Q->front)) exit(0);
Q->front->next=NULL; }

/* 2、銷毀鏈式隊列 */
void DestroyQueue(LinkQueue *Q)
{ while (Q->front)
{ Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear; }
}

/* 3、清空鏈式隊列 */
void ClearQueue(LinkQueue *Q)
{ QueuePtr p;
p=Q->front->next;
while (p)
{ Q->front->next=p->next;
free(p); }
Q->rear=Q->front;
}

/* 4、判斷空隊列 */
int QueueEmpty(LinkQueue Q)
{ if (Q.front==Q.rear)
return 1;
else
return 0; }

/* 5、求鏈式隊列長度 */
int QueueLength(LinkQueue Q)
{ QueuePtr p; int n=0;
p=Q.front;
while (p!=Q.rear)
{ n++; p=p->next; }
return n;
}

/* 6、取隊頭元素 */
ElemType GetHead(LinkQueue Q)
{ if (Q.front!=Q.rear)
return Q.front->next->data;
}

/* 7、入隊列 */
void EnQueue(LinkQueue *Q, ElemType e)
{ QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if (!p) exit(0);
p->data=e; p->next=NULL;
Q->rear->next=p;
Q->rear=p; }

/* 8、出隊列 */
void DeQueue(LinkQueue *Q, ElemType *e)
{ QueuePtr p;
if (Q->front!=Q->rear)
{ p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if (Q->rear==p) Q->rear=Q->front;
free(p); }
}

/* 9、遍歷鏈式隊列並輸出元素 */
void QueueTraverse(LinkQueue Q)
{ QueuePtr p;
printf("\nQueue: ");
p=Q.front->next;
while (p)
{ printf("%d\t",p->data);
p=p->next;}
}

/* 約瑟夫問題 */
void Joseffer(int n)
{ LinkQueue Q; int i; ElemType x;
InitQueue(&Q);
for(i=1; i<=n; i++)
EnQueue(&Q,i);
while (!QueueEmpty(Q))
{ for(i=1; i<=3; i++)
{ DeQueue(&Q,&x);
if (i!=3)
EnQueue(&Q,x);
else
printf("%5d",x);
}
}
}

/* 主函數 */
main()
{ LinkQueue Q; int i; ElemType x;
InitQueue(&Q);
for(i=2; i<=5; i++)
EnQueue(&Q,i);
printf("len:%d\n",QueueLength(Q));
while (!QueueEmpty(Q))
{ DeQueue(&Q,&x);
printf("%d\t",x); }
//QueueTraverse(Q);
//Joseffer(6);
}

自己去調試吧,這個是C語言版的鏈式隊列,如果看不懂或者調不出來就去看書吧。否則你這門是白學了。
註:這里是鏈式隊列

/***************/
/* 循環隊列 */
/***************/
#include "stdlib.h"
#include "stdio.h"
#define N 100

/* 定義循環隊列類型 */
typedef int ElemType;
typedef struct
{ ElemType *base;
int front;
int rear;
} SqQueue;

/* 1、初始化循環隊列 */
void InitQueue(SqQueue *Q)
{ Q->base=(ElemType*)malloc(N*sizeof(ElemType));
Q->front=Q->rear=0; }

/* 2、銷毀循環隊列 */
void DestroyQueue(SqQueue *Q)
{ free(Q->base); }

/* 3、清空循環隊列 */
void ClearQueue(SqQueue *Q)
{ Q->front=Q->rear=0; }

/* 4、判斷空隊列 */
int QueueEmpty(SqQueue Q)
{ if (Q.front==Q.rear)
return 1;
else
return 0; }

/* 5、求循環隊列長度 */
int QueueLength(SqQueue Q)
{ return (Q.rear+N-Q.front)%N; }

/* 6、取隊頭元素 */
void GetHead(SqQueue Q, ElemType *e)
{ if (Q.front!=Q.rear)
*e=Q.base[Q.front];
}

/* 7、入隊列 */
int EnQueue(SqQueue *Q, ElemType e)
{ if ((Q->rear+1)%N==Q->front)
return 0;
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%N;
return 1; }

/* 8、出隊列 */
int DeQueue(SqQueue *Q, ElemType *e)
{ if (Q->front==Q->rear)
return 0;
*e=Q->base[Q->front];
Q->front=(Q->front+1)%N;
return 1; }

/* 9、遍歷循環隊列並輸出元素 */
void QueueTraverse(SqQueue Q)
{ int i;
printf("\nQueue: ");
if (Q.rear<Q.front) Q.rear=Q.rear+N;
for(i=Q.front; i<Q.rear; i++)
printf("%d\t",Q.base[i%N]); }

/* 主函數 */
main()
{ SqQueue Q; int i; ElemType x;
InitQueue(&Q);
for(i=2; i<=5; i++)
EnQueue(&Q,i);
printf("len:%d\n",QueueLength(Q));
while (!QueueEmpty(Q))
{ DeQueue(&Q,&x);
printf("%d\t",x); }
QueueTraverse(Q);
}

在給你個循環隊列吧

② C語言中使用隊列

如果你用vc,#include<deque>就好了,但是注意要加上using naemspace std;
我是當你用的c++的STL,STL中沒有真正的隊列和棧,他們都是通過對雙端隊列的改造得到的,所以包含的文件可能和你想的不一樣。而且這些頭文件都沒有.h結尾!很特別
如果你不是vc,當我沒說

③ c語言實現隊列和棧的方法

#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 20
typedef char SElemType;
typedef int Status;typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;#include<stdio.h>
#include<stdlib.h>Status CreatStack(SqStack &S){
//創建棧
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//CreatStackStatus Push(SqStack &S,SElemType e){
//插入e為新的棧頂元素
if(S.top-S.base>=S.stacksize){//棧滿,追加存儲空間
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);//存儲空間分配失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//PushStatus Pop(SqStack &S ,SElemType &e){
//若棧不空,刪除棧頂元素,並用e返回其值
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}//Pop typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QNodePtr;typedef struct{
QNodePtr front;
QNodePtr rear;
}LinkQueue;Status CreatQueue(LinkQueue &Q){
//建立一個空的鏈式棧
Q.front=Q.rear=(QNodePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}Status EnQueue(LinkQueue &Q,QElemType e){ QNodePtr p;
p=(QNodePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}Status DeQueue(LinkQueue &Q,QElemType &e){QNodePtr p;<br>if(Q.front==Q.rear) return ERROR;<br>p=Q.front->next; e=p->data;<br>Q.front->next=p->next;<br>if(Q.rear==p) Q.rear=Q.front;<br>free(p);<br>return OK;<br>}int stackPalindrom_Test()//判別輸入的字元串是否迴文序列,是則返回1,否則返回0
{
printf("棧練習,請輸入要判斷的字元串以#作為結束符,不要超過二十個字元\n");
SqStack S;
CreatStack(S);
char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
Push(S,b[count]); //進棧
count++;
}
int i = 0;
while(i < count)
{
Pop(S,a);
if(a!=b[i])
return ERROR;
i++;
}
return OK;}//stackPalindrom_Test int queuePalindrom_Test()//判別輸入的字元串是否迴文序列,是則返回1,否則返回0
{
printf("隊列練習,請輸入要判斷的字元串以#作為結束符,不要超過二十個字元\n");
LinkQueue Q;
CreatQueue(Q); char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
EnQueue(Q,b[count]);; //入列
count++;
} while(count-- > 0)
{
DeQueue(Q,a);
if(a!=b[count])
return ERROR;
}
return OK;
}//queuePalindrom_Test Status main(){ if(stackPalindrom_Test()==1)
printf("是迴文\n");
else printf("不是迴文\n"); if(queuePalindrom_Test()==1)
printf("是迴文\n");
else printf("不是迴文\n");
return OK;
}

④ 關於C語言的隊列

可以使用c++中的容器如queue,頭文件在#include <queue>

⑤ c語言 隊列的操作

//定義隊列結構體
typedef struct Qnode
{
int data;
struct Qnode *next;
} Queue , *QueuePtr;

typedef struct
{
QueuePtr front;
QueuePtr rear;
} linkQnode;
//創建一個隊列
initQueue (linkQnode *q)
{
q -> front = q -> rear = (QueuePtr) malloc (sizeof (Queue));
if (!q -> front) exit (0);
q -> front -> next = NULL;
}
//入隊列
EnterQueue (linkQnode *q , int item)
{
QueuePtr p;
p = (QueuePtr) malloc (sizeof (Queue));
if (!p) exit (0);
p -> data = item;
p -> next = NULL;
q -> rear -> next = p;
q -> rear = p;
}
//出隊列
DelQueue (linkQnode *q , int *item)
{
QueuePtr p;
if (q -> front = q -> rear) return;
p = q -> front -> next;
*item = p -> data;
q -> front -> next = p -> next;
if (q -> rear == p)
q -> rear = q -> front;
free (p);
}

⑥ c語言隊列操作

pq->rear->next
=
pnew這個代碼從隊列的尾部增加新節點,
然後pq->rear
=
pnew更新隊列尾部指針。隊列的數據結構形式就是由一個頭front指針,一個尾rear指針來表徵,items的設計是用空間換時間,涉及隊列大小的操作會非常方便。
隊列的特徵是先進先出,你給出的鏈式實現,其實就跟一個鏈表一樣,鏈表的添加刪除如果能理解了,隊列只是鏈表的元素增加/刪除
按先進先出特點的一種實現。
但對於隊列來說,實現方式不是重點,先進先出的性質才是重點,這在實際應用中很多,比如排隊叫號。

⑦ C語言中,隊列是什麼意思,有什麼用途

隊列是一種特殊的線性表。

隊列一種可以實現「先進先出」的存儲結構,即「一端入,一端出」,隊首(front)出隊,隊尾(rear)入隊,若front指向隊首,則rear指向隊尾最後一個有效元素的下一個元素;若rear指向隊尾,則front指向隊首第一個有效元素的下一個元素。

隊列特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。

(7)c語言非同步隊列擴展閱讀

循環隊列各個參數的含義

1、隊列初始化front和rear的值都是零,初始化時隊列就是空的。

2、隊列非空front代表隊列的第一個元素rear代表了最後一個有效元素的下一個元素。

3、隊列空front和rear的值相等,但是不一定是零。

⑧ C語言同步和非同步的區別

同步:代碼立即執行
非同步:代碼程序先在隊列排隊,等到可以執行再去執行。

⑨ 用C語言編寫隊列程序

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define NULL 0
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef int QElemType;
typedef int Status;
typedef struct QNode
{
QElemType data;
QNode *next;
}*QueuePtr;
struct LinkQueue
{
QueuePtr front,rear;//隊頭,隊尾指針
};
//函數列表
void InitQueue(LinkQueue &Q)
{//初始化一個隊列
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)//生成頭結點失敗
exit(OVERFLOW);
Q.front->next=NULL;
}
void DestoryQueue(LinkQueue &Q)
{ //銷毀隊列
while(Q.front)
{
Q.rear=Q.front->next;//Q.rear指向Q.front的下一個結點
free(Q.front);//釋放Q.front所指結點
Q.front=Q.rear;//Q.front指向Q.front的下一個結點
}
}
void ClearQueue(LinkQueue &Q)
{ //將隊列清為空
DestoryQueue(Q);//銷毀隊列
InitQueue(Q);//重新構造隊列
}
Status QueueEmpty(LinkQueue Q)
{ //判斷隊列是否為空
if(Q.front->next==NULL)
return TRUE;
else return FALSE;
}
int QueueLength(LinkQueue Q)
{ //求隊列的長度
int i=0;//計數器清0
QueuePtr p=Q.front;//p指向結點
while(Q.rear!=p)//p所指向的不是尾結點
{
i++;//計數器加1
p=p->next;
}
return i;
}
Status GetHead(LinkQueue Q,QElemType &e)
{ //若隊列不空,則用e返回隊頭元素
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;//p指向隊頭結點
e=p->data;//將隊頭元素的值賦給e
return OK;
}
void EnQueue(LinkQueue &Q,QElemType e)
{ //插入元素e為隊列Q的新的隊尾元素
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
//動態生成新結點
if(!p)
exit(OVERFLOW);
p->data=e;//將e的值賦給新結點
p->next=NULL;//新結點的指針為空
Q.rear->next=p;//原隊尾結點的指針域為指向新結點
Q.rear=p;//尾指針指向新結點
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{ //若隊列不為空,刪除Q的隊頭元素,用e返回其值
QueuePtr p;
if(Q.front==Q.rear)//隊列為空
return ERROR;
p=Q.front->next;//p指向隊頭結點
e=p->data;//隊頭元素賦給e
Q.front->next=p->next;//頭結點指向下一個結點
if(Q.rear==p)//如果刪除的隊尾結點
Q.rear=Q.front;//修改隊尾指針指向頭結點
free(p);
return OK;
}
void QueueTraverse(LinkQueue Q,void(*visit)(QElemType))
{ //對隊頭到隊尾依次對隊列中每個元素調用函數visit()
QueuePtr p;
p=Q.front->next;
while(p)
{
visit(p->data);//對p所指元素調用visit()
p=p->next;
}
printf("\n");
}
void print(QElemType e)
{
printf("%2d",e);
}
void main()
{
int i,k;
QElemType d;
LinkQueue q;
InitQueue(q);//構造一個空棧
for(i=1;i<=5;i++)
{
EnQueue(q,i);
}
printf("棧的元素為:");
QueueTraverse(q,print);
k=QueueEmpty(q);
printf("判斷棧是否為空,k=%d(1:為空9;0:不為空)\n",k);
printf("將隊頭元素賦給d\n");
k=GetHead(q,d);
printf("隊頭元素為d=%d\n",d);
printf("刪除隊頭元素:\n");
DeQueue(q,d);
k=GetHead(q,d);
printf("刪除後新的隊頭元素為d=%d\n",d);
printf("此時隊列的長度為%d\n",QueueLength(q));
ClearQueue(q);//清空隊列
printf("清空隊列後q.front=%u,q.rear=%u,q.front->next=%u\n",q.front,q.rear,q.front->next);
DestoryQueue(q);
printf("銷毀隊列後,q.front=%u,q.rear=%u\n",q.front,q.rear);

⑩ C語言隊列

隊列可以這樣啊,用一個環形數組來避免假溢出...寫成這樣真是看不下去...
或者你用C++直接帶隊列的#include<queue>