資料結構C語言實現之隊列

來源:互聯網
上載者:User
#include <stdio.h>
#include <stdlib.h>

typedef int elemType;
/************************************************************************/
/* 以下是關於隊列連結儲存操作的6種演算法 */
/************************************************************************/
struct sNode{
elemType data; /* 範圍 */
struct sNode *next; /* 連結指標 */
};
struct queueLK{
struct sNode *front; /* 隊首指標 */
struct sNode *rear; /* 隊尾指標 */
};

/* 1.初始化鏈隊 */
void initQueue(struct queueLK *hq)
{
hq->front = hq->rear = NULL; /* 把隊首和隊尾指標置空 */
return;
}

/* 2.向鏈隊中插入一個元素x */
void enQueue(struct queueLK *hq, elemType x)
{
/* 得到一個由newP指標所指向的新結點 */
struct sNode *newP;
newP = malloc(sizeof(struct sNode));
if(newP == NULL){
printf("記憶體空間分配失敗! ");
exit(1);
}
/* 把x的值賦給新結點的範圍,把新結點的指標域置空 */
newP->data = x;
newP->next = NULL;
/* 若鏈隊為空白,則新結點即是隊首結點又是隊尾結點 */
if(hq->rear == NULL){
hq->front = hq->rear = newP;
}else{ /* 若鏈隊非空,則依次修改隊尾結點的指標域和隊尾指標,使之指向新的隊尾結點 */
hq->rear = hq->rear->next = newP; /* 注意賦值順序哦 */
}
return;
}

/* 3.從隊列中刪除一個元素 */
elemType outQueue(struct queueLK *hq)
{
struct sNode *p;
elemType temp;
/* 若鏈隊為空白則停止運行 */
if(hq->front == NULL){
printf("隊列為空白,無法刪除! ");
exit(1);
}
temp = hq->front->data; /* 暫存隊尾元素以便返回 */
p = hq->front; /* 暫存隊尾指標以便回收隊尾結點 */
hq->front = p->next; /* 使隊首指標指向下一個結點 */
/* 若刪除後鏈隊為空白,則需同時使隊尾指標為空白 */
if(hq->front == NULL){
hq->rear = NULL;
}
free(p); /* 回收原隊首結點 */
return temp; /* 返回被刪除的隊首元素值 */
}

/* 4.讀取隊首元素 */
elemType peekQueue(struct queueLK *hq)
{
/* 若鏈隊為空白則停止運行 */
if(hq->front == NULL){
printf("隊列為空白,無法刪除! ");
exit(1);
}
return hq->front->data; /* 返回隊首元素 */
}

/* 5.檢查鏈隊是否為空白,若為空白則返回1, 否則返回0 */
int emptyQueue(struct queueLK *hq)
{
/* 判斷隊首或隊尾任一個指標是否為空白即可 */
if(hq->front == NULL){
return 1;
}else{
return 0;
}
}

/* 6.清除鏈隊中的所有元素 */
void clearQueue(struct queueLK *hq)
{
struct sNode *p = hq->front; /* 隊首指標賦給p */
/* 依次刪除隊列中的每一個結點,最後使隊首指標為空白 */
while(p != NULL){
hq->front = hq->front->next;
free(p);
p = hq->front;
} /* 迴圈結束後隊首指標已經為空白 */
hq->rear = NULL; /* 置隊尾指標為空白 */
return;
}

/************************************************************************/
int main(int argc, char* argv[])
{
struct queueLK q;
int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
int i;
initQueue(&q);
for(i = 0; i < 8; i++){
enQueue(&q, a[i]);
}
printf("%d ", outQueue(&q)); printf("%d ", outQueue(&q));
enQueue(&q, 68);
printf("%d ", peekQueue(&q)); printf("%d ", outQueue(&q));
while(!emptyQueue(&q)){
printf("%d ", outQueue(&q));
}
printf(" ");
clearQueue(&q);
system("pause");
}

聯繫我們

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