基於數組的隊列實現(C語言) – ahljjun的專欄 – 部落格頻道 – CSDN.NET

來源:互聯網
上載者:User

基於數組的隊列實現(C語言) - ahljjun的專欄 - 部落格頻道 - CSDN.NET

基於數組的隊列實現(C語言) 分類: 演算法與資料結構 2008-11-12 16:55 371人閱讀 評論(0) 收藏 舉報語言cstructnull測試

 

 

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

                  介面檔案QUEUE.h

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

 

#ifndef _QUEUE_H
#define _QUEUE_H

#ifndef BOOL
#define BOOL int
#endif

typedef int Item;
struct _Queue;
typedef struct _Queue  Queue;

typedef Queue* hQueue;//handle to Queue

hQueue Queue_Init(int nMax);

void Queue_Destroy(hQueue q);

void Queue_Put(hQueue q, Item elem);

Item Queue_Get(hQueue q);

int Queue_Size(hQueue q);

BOOL Queue_IsEmpty(hQueue q);
BOOL Queue_IsFull(hQueue q);

 

#endif

 

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

         實現檔案QUEUE.c

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

 

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

void Error(char* msg)
{
 printf("Error:%s",msg);
}

//機遇數組的隊列實現
struct _Queue
{
 Item* elem;
 int head,tail;
 int N;
};

hQueue Queue_Init(int nMax)
{
 hQueue que=malloc(sizeof(*que));
 que->elem=malloc((nMax+1)*sizeof(*que->elem));
 que->head=que->tail=0;
 que->N=nMax+1;
 return que;
}

void Queue_Destroy(hQueue que)
{
 if(que->elem)
  free(que->elem);
 que->elem=NULL;
 free(que);
 que=NULL;

}

void Queue_Put(hQueue que, Item elem)
{
 if(Queue_IsFull(que))
 {
  Error("The Queue Is Full!/n");
  exit(-1);
 }
 que->elem[que->tail]=elem;
 que->tail=(que->tail+1)%(que->N);
}

Item Queue_Get(hQueue que)
{
 Item elem;
 if(Queue_IsEmpty(que))
 {
  Error("The Queue Is Empty!/n");
  exit(-1);
 }
 elem=que->elem[que->head];
 que->head=(que->head+1)%(que->N);
 return elem;
}

int Queue_Size(hQueue que)
{
 return (que->tail-que->head+que->N)%(que->N);
}

 

BOOL Queue_IsEmpty(hQueue que)
{
 return (que->head==que->tail);
}

BOOL Queue_IsFull(hQueue que)
{
 return (que->head==(que->tail+1)%(que->N));
}

 

 

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

                   測試檔案main.c

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

 

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

int main()
{
 hQueue  q=Queue_Init(7);
 int t=7;
 while(t)
 Queue_Put(q,t--);

 printf("Queue:%d/n",Queue_Get(q));
 printf("Queue:%d/n",Queue_Get(q));
 printf("Queue:%d/n",Queue_Get(q));

 printf("Queu Size= %d/n",Queue_Size(q));
 
printf("******************************************/n");
 t=30;
 Queue_Put(q,t--);
 Queue_Put(q,t--);

 printf("Queu Size= %d/n",Queue_Size(q));

 

 while(!Queue_IsEmpty(q))
 printf("Queue:%d/n",Queue_Get(q));
 Queue_Destroy(q);

 return 0;
}

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.