/*---------------------------------------------------------
Title: Link Queue(鍊隊列) 鍊隊列-鍊式存儲結構的隊列
請先閱讀教材74-77頁, 2.4.1-2.4.4節, 隊列的定義及基本運算
(注意:以下程式為簡化後的,僅供入門學習之用)
----------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
//定義隊列的結構
struct queueNode
{
int data;//存放資料元素
struct queueNode * next;//指針,指向下一個結點
};
struct queue
struct queueNode * front;
struct queueNode * rear;
//初始化隊列
struct queue * InitialQueue()
struct queue * head;
struct queueNode * node;
node=(struct queueNode *)malloc(sizeof(struct queueNode ));
head=(struct queue *)malloc(sizeof(struct queue ));
node->next=NULL;
head->front=node;
head->rear=node;
return head;
}
//入隊列
void EnterIntoQueue(struct queue * head, int value)
struct queueNode * node;
node=(struct queueNode *)malloc(sizeof(struct queueNode ));
node->data=value;
node->next=NULL;
head->rear->next=node;
head->rear=node;
}
//出隊列
void DeleteFromQueue(struct queue * head)
if(head->front==head->rear)
{
printf("Queue is empty, Delete failed\n");
else
{
node=head->front->next;
head->front->next=node->next;
free(node);
// when there is only one element, the following is necessary.
if(head->front->next==NULL)
head->rear=head->front;
}
//顯示隊列中所有元素
void ShowAllElements(struct queue * head)
printf("\n Show all elements: \n");
node=head->front->next;
while(node!=NULL)
printf(" %d ",node->data);
node=node->next;
void main()
struct queue * head1;
head1=InitialQueue();
ShowAllElements(head1);
EnterIntoQueue(head1,11);
EnterIntoQueue(head1,22);
DeleteFromQueue(head1);