順序隊列基本操作

來源:互聯網
上載者:User

#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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.