當前位置:首頁 » 編程語言 » c語言隊列的程序

c語言隊列的程序

發布時間: 2023-12-11 07:26:13

c語言實現隊列的基本操作



structpQueue
{
ElemType*head;//指向開辟的空間的首地址
Elemtype*tail;
intlength;//(總容量)
intL_now;//(當前容量)
};
if(pQueue.L_now==pQueue.length)
{
每次申請空間都是+N
}
pQueue->tail=p;

⑵ C語言,用數組實現隊列的入隊,出隊函數編程

這樣的話應該符合你的要求:

#include<stdio.h>
voidadd(intqueue[],intx);
intTop(intqueue[]);
voiddel(intqueue[]);
intend=0;
intmain()
{
intn;
scanf("%d",&n);//將要入隊列n個元素
intqueue[1000];
for(inti=1;i<=n;i++)//輸入n個元素
{
add(queue,i);//將i加入隊列
}
//驗證加入隊列的元素,將隊列中的元素按照輸入的順序輸出:
for(i=1;i<=n;i++)
{
printf("%d",Top(queue));//Top函數返回隊頭元素
del(queue);//刪除隊頭元素
}
//驗證輸出已經出隊列後的隊列(數組)元素:
printf(" ");
for(i=1;i<=n;i++)
printf("%d",queue[i]);
printf(" ");
return0;
}
voidadd(intqueue[],intx)
{
queue[++end]=x;
}
intTop(intqueue[])
{
returnqueue[1];//注意,這里的函數始終returnqueue[1];這里是和將普通數組中的元素輸出最大的不同之處。!!!!!!
}
voiddel(intqueue[])
{
for(inti=2;i<=end;i++)
{
queue[i-1]=queue[i];
}
queue[end]=0;//將刪除後的地方置0
end--;
}

⑶ 數據結構c語言版,出隊入隊及依次輸出一個隊列的操作。

#include<stdio.h>
#include<stdlib.h>

#defineElemTypeint
#defineStatusint
#defineOK1
#defineERROR0

typedefstructQNode{
ElemTypedata;
structQNode*next;
}QNode;

typedefstructLinkQueue{
QNode*front;
QNode*rear;
}LinkQueue;

StatusInitQueue(LinkQueue*q){//建立隊列
q->front=q->rear=(QNode*)malloc(sizeof(QNode));
if(!q->front)
returnERROR;
q->front->next=NULL;
returnOK;
}

StatusEnQueue(LinkQueue*q,ElemTypee){//入隊
QNode*p=(QNode*)malloc(sizeof(QNode));
if(!p)
returnERROR;
p->data=e;
p->next=NULL;
q->rear->next=p;//入隊操作,從隊尾(rear)進入
q->rear=p;
returnOK;
}

StatusDeQueue(LinkQueue*q,ElemType*e){//出隊
QNode*p=(QNode*)malloc(sizeof(QNode));
if(!p)
returnERROR;

p=q->front->next;//q指向的是front指針的下一個位置,亦即隊首元素
*e=p->data;
q->front->next=p->next;//出隊操作後,front++
if(q->rear==p)//判斷是否全部出隊
q->rear=q->front;//如果全部出隊,則將隊列置為空
returnOK;
}

StatusPrintfQueue(LinkQueue*Q){
QNode*p;

for(p=Q->front->next;p!=NULL;p=p->next)
printf("%d ",p->data);
}

intmain(void)
{
inti,n;
ElemTypee,de;
LinkQueue*q=(LinkQueue*)malloc(sizeof(QNode));
if(!q)
returnERROR;
InitQueue(q);

printf("以下開始構造初始隊列: ");

printf("請輸入元素個數:");
scanf("%d",&n);
printf(" ");

for(i=0;i<n;++i){
printf("請輸入第%d個元素:",i+1);
scanf("%d",&e);
EnQueue(q,e);
}
printf(" ");
printf("初始隊列構造完畢! ");

printf("初始隊列: ");
PrintfQueue(q);
printf(" ");
printf("====================================================== ");

printf("以下開始執行入隊操作: ");

printf("請輸入需入隊的元素個數:");
scanf("%d",&n);
printf(" ");

for(i=0;i<n;++i){
printf("請輸入第%d個元素:",i+1);
scanf("%d",&e);
EnQueue(q,e);
}
printf(" ");
printf("入隊%d個元素操作完畢! ",n);

printf("此時隊列: ");
PrintfQueue(q);
printf(" ");
printf("====================================================== ");

printf("以下開始執行出隊操作: ");

printf("請輸入需出隊的元素個數:");
scanf("%d",&n);
printf(" ");

for(i=0;i<n;++i)
DeQueue(q,&de);
printf(" ");
printf("出隊%d個元素操作完畢! ",n);

printf("此時隊列: ");
PrintfQueue(q);
printf(" ");
printf("====================================================== ");

free(q);
return0;
}

執行結果

⑷ 求一個C語言 循環隊列的插入 完整程序

(1)編寫一個程序,實現順序環形隊列的各種基本運算,並在此基礎上設計一個主程序完成如下功能:
(1)初始化隊列q;
(2)判斷隊列q是否非空;
(3)依次進隊元素100、909、44、8;
(4)出隊一個元素,輸出該元素;
(5)輸出隊列q的元素個數;
(6)依次進隊元素-67、55、99、70;
(7)輸出隊列q的元素個數;
#include<stdio.h>
#include<malloc.h>
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define OK 1
#define TURE 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;

Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType *)malloc
(QUEUE_INIT_SIZE*sizeof(QElemType));
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueNumElem(SqQueue Q)
{
return (Q.rear-Q.front)%QUEUE_INIT_SIZE;
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE;

return OK;
}
SqQueue DeQueue(SqQueue Q,int e)
{
int i;
if(Q.front==Q.rear)
printf("隊為空!\n");
for(i=e;i<Q.rear;i++)
Q.base[i]=Q.base[i+1];
Q.rear--;
return Q;
}
int main()
{
SqQueue Q,Q1;
static int qele=0;
int i,j=0,k=0,m=0;
static int frontd=Q.front;
i=InitQueue(Q);
if(i==1)
printf("隊初始化成功!!\n");
else
printf("隊初始化失敗!!\n");
if(Q.front!=Q.rear)
printf("隊不為空!!\n");
else
printf("隊為空L!!\n");
printf("輸入數據(END of '9999'):");
scanf("%d",&qele);
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele);
scanf("%d",&qele);
}
frontd=Q.front;
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front]);
Q.front++;
j++;
}
printf("\n");
Q.front=frontd;
printf("輸入要出隊的元素:");
scanf("%d",&j);
while(Q.front!=Q.rear)
{
if(Q.base[Q.front]==j)
{
printf("%d\n",Q.base[Q.front]);
Q=DeQueue(Q,Q.front);
m++;
break;
}
Q.front++;
}
Q.front=frontd;
while(Q.front!=Q.rear)
{
printf(" %d ",Q.base[Q.front]);
Q.front++;
}
printf("\n");
Q.front=frontd;
printf("隊的長度:%d\n",Q.rear-Q.front);
printf("輸入數據(END of '9999'):");
scanf("%d",&qele);
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele);
scanf("%d",&qele);
}
Q.front=frontd;
printf("隊的長度:%d\n",Q.rear-Q.front);
Q.front=frontd;
printf("出隊順序:");
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front]);
Q=DeQueue(Q,Q.front);
m++;
}
printf("end\n");
Q.front=0;
Q.rear=m;
while(Q.rear!=Q.front)
{
free(Q.base);
//Q.base++;
Q.front++;
if(Q.rear-1==Q.front)
printf("隊已經釋放!\n");
}
return 0;
}

⑸ 數據結構(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語言實現優先隊列(priority queue)

        堆排序是一個比較優秀的演算法,堆這種數據結構在現實生活中有很多的應用,比如堆可以作為一個優先隊列來使用,作為一個高效的優先隊列,它與堆的結構一樣,都有最大優先隊列,最小優先隊列.優先隊列priority queue 是一種用來維護一組元素構成的集合S的數據結構,每一個元素都有一個相關的值,稱為關鍵字(key)。

        最大優先隊列包含以下操作:

         將元素x插入到S的集合中,等價於 ;

         返回S中最大元素;

         返回並且刪除S中最大元含李素;

         將元素x的關鍵字增加到key,要求 。

        同樣的,最小優先隊列操作也包括: , , , 。只不過是對最小值進行操作。

        在這里主要討論最大優先隊列,其應用很多,在共享計算機作業系統就是,類似於早期的unix主機,管理員root可以設置n個不同的用戶,以及各個用戶不同的操作許可權,從主機那裡接出多個終端,每個操作人員(程序員)在自己的工作終端 ,感覺像是自己擁有自己獨立的作業主機一樣,其實不是,通過一些任務調度來實現,其中就有任務等待執行相關隊列,並且有各個任務有著自己優先順序,以便確定調度執行具體任務,如果你學過操作系統相關知識,那麼應該對系統調度有所了解。

        當一個作業被完成或者被中斷後,調度器會調用 來調用所有在隊列中等待任務中優先順序最高的任務執行,在新任務加入等待任務時會配稿調用 加入任務等待隊列,當某個任務等待時間過長時可通過 提高其優先順序培老孝,從而減少等待時間。

        下面是具體實現C程序源碼

#include <stdio.h>

#define NAGE_INFINIT -99999

#define parent(i) i/2

#define left(i) 2*i+1

#define right(i) 2*i+2

//get array of A first element

int heap_maximum(int A[]){ return A[0];}

/***********************************************

*

* function max_heapify();

*

* args

*  A[] inttype save elements of heap

*  i index of A

*  heap_size real length of A

*

* ********************************************/

void max_heapify(int A[],int i,int heap_size){

    int l,r,largest,temp;

    l=left(i);

    r=right(i);

    if((l<=heap_size)&&(A[l]>A[i]))

        largest=l;

    else

        largest=i;

    if((r<=heap_size)&&(A[r]>A[largest]))

        largest=r;

    if(largest!=i){

        temp=A[i];

        A[i]=A[largest];

        A[largest]=temp;

        max_heapify(A,largest,heap_size);

    }

}

/*********************************************

*

* function heap_extract_max()

*

* args

*  A[] inttype save elements of heap

*  heap_size inttype the real length of A

*

* return max the parent node value

*

* ******************************************/

int heap_extract_max(int A[],int heap_size){

    int max;

    if(heap_size<0)

        return -1;  //heap underflow

    max=A[0];  //parent node the max value of element

    A[0]=A[heap_size];

    heap_size--;

    /**************************************

    * dajust binary heap(or tree) to make

    * sure heap fo A true every times

    *

    * ************************************/

    max_heapify(A,0,heap_size);

    return max;

}

/***********************************************

*

* function heap_increase_key();

*

* args

*  A[] inttype save elemnts of heap

*  i index of A

*  key inserted element

*

* *********************************************/

void heap_increase_key(int A[],int i,int key){

    int temp;

    if(key<A[i]){

        printf("new key is smaller than current key\n");

        return;    //over programming

    }

    A[i]=key;

    //p=parent(i);

    while ((i>0)&&(A[parent(i)]<A[i])) {

        temp=A[i];

        A[i]=A[parent(i)];

        A[parent(i)]=temp;

        i=parent(i); //update index of A

        //p=parent(i);

    }

}

/***************************************************

*

* function max_heap_insert();

*

* args

*  A[] inttype save elements of A

*  key inserted element to A

*  heap_size real length of A

*

* **************************************************/

void max_heap_insert(int A[],int key,int heap_size){

    heap_size+=1;

    A[heap_size]=NAGE_INFINIT;

    heap_increase_key(A,heap_size,key);

}

int main()

{

    int heap_max,max,i,key;

    int A[11],Temp[11];

    int heap_size=0;

    char c;

    while (1) {

        scanf("%d",&A[heap_size]);

        c=getchar();

        if(c=='\n')

            break;

        heap_size++;

    }

    // A to Temp

    for(i=0;i<=heap_size;i++)

        Temp[i]=A[i];

    //get heap maximum element

    heap_max=heap_maximum(A);

    printf("heap of A maximum element: %d\n",heap_max);

    // Temp to A

    for(i=0;i<=heap_size;i++)

        A[i]=Temp[i];

    /*--extract maximum element--*/

    max=heap_extract_max(A,heap_size);

    printf("extract element: %d \n",max);

    printf("new heap of A after extract maximum node\n");

    for(i=0;i<heap_size;i++)

        printf("%d ",A[i]);

    printf("\n");  //next line

    // Temp to A

    for(i=0;i<=heap_size;i++)

        A[i]=Temp[i];

    /*--increase from A[i] to key--*/

    printf("input i key ");

    scanf("%d %d",&i,&key);

    heap_increase_key(A,i,key);

    for(i=0;i<=heap_size;i++)

        printf("%d ",A[i]);

    printf("\n");

    // Temp to A

    for(i=0;i<=heap_size;i++)

        A[i]=Temp[i];

    /*--insert queueu--*/

    key=0;  //init key;

    printf("input key: ");

    scanf("%d",&key);

    max_heap_insert(A,key,heap_size);

    for(i=0;i<=heap_size+1;i++)

        printf("%d ",A[i]);

    printf("\n");

    return 0;

}

/****************************************

*

* input 16 14 10 8 7 9 3 2 4 1

*      i: 8

*      key: 15

*

* output in function main()

* **************************************/

其運行結果如下圖:

歡迎留言交流,也感謝指正,一起進步。

熱點內容
編程培訓福州 發布:2024-07-27 12:28:06 瀏覽:876
哈弗h6女生適合哪個配置 發布:2024-07-27 12:10:52 瀏覽:954
memcached啟動腳本 發布:2024-07-27 11:55:41 瀏覽:558
電動車怎麼看配置 發布:2024-07-27 11:55:05 瀏覽:238
mfc打開默認文件夾 發布:2024-07-27 11:41:23 瀏覽:648
電腦找不到伺服器的原因 發布:2024-07-27 11:33:58 瀏覽:864
sql2005操作 發布:2024-07-27 11:33:19 瀏覽:437
安卓什麼app軟體可以代替藍牙 發布:2024-07-27 11:24:50 瀏覽:745
vb編譯運行 發布:2024-07-27 11:14:42 瀏覽:754
恆線速的演算法 發布:2024-07-27 10:45:46 瀏覽:759