生活随笔
收集整理的這篇文章主要介紹了
C语言队列创建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構之隊列,隊列是另一種特殊的線性表,是先進先出的,并且出去的元素會被刪除。就如同我們真實生活中排隊一樣,先到的人排在前面,也最先離開隊列。那么我們就來談一下它的具體實現,這里主要使用鏈式存儲方式。
下面是代碼
#include<stdio.h>
#include<stdlib.h>
創建一個鏈表結構
typedef struct QueueNode
{int data
;struct QueueNode
*next
;
}QueueNode
,*Queuelink
;
再創建一個隊列結構
typedef struct Queue
{Queuelink front
, rear
;
}Queue
;
void init(Queue
*q
)
{
q
->front
= q
->rear
= (Queuelink
)malloc(sizeof(QueueNode
));if (!q
->front
){exit(0);}q
->front
->next
= NULL;
}
void insert(Queue
*q
, int e
)
{Queuelink p
;p
= (Queuelink
)malloc(sizeof(QueueNode
));if (p
== NULL)exit(0);p
->data
= e
;p
->next
= NULL;q
->rear
->next
= p
;q
->rear
= p
;
}
void out(Queue
*q
, int *e
)
{Queuelink p
;if (q
->front
== q
->rear
){return;}p
= q
->front
->next
;*e
= p
->data
;q
->front
->next
= p
->next
;if (q
->rear
== p
){q
->rear
= q
->front
;}free(p
);
}
int isempty(Queue
*q
)
{if (q
->front
== q
->rear
)return 1;else return 0;
}
總結
以上是生活随笔為你收集整理的C语言队列创建的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。