LinkList_Queue(鏈式隊列)

#include<stdio.h>
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;
typedef struct Queue {
node *front, *rear;
}queue;


queue *init()
{
queue *q;
q = (queue *)malloc(sizeof(queue));
node *n = (node *)malloc(sizeof(node));
n->next = NULL;
q->front = n;
q->rear = n;
return q;
}


int empty(queue *q)
{
if (q->front->info == NULL)
return 1;
else
return 0;
}


void display(queue *q)
{
if (!q->front)
printf("棧爲空,無法進行輸出!\n");
else
{
node *p = q->front;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}


queue *insert(queue *q, Datatype x)
{
node *p, *l=q->rear;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (!empty(q))
q->front=q->rear = p;
else
{
q->rear->next = p;
q->rear = p;
}
return q;
}




queue *dele(queue *q)
{
queue *p = q;
if (!p)
printf("棧爲空,無法進行刪除!\n");
else
{
node *n = q->front;
q->front = q->front->next;
free(n);
}
return q;
}


void main()
{
queue *Q;
Q = init();
int i;
for (i = 0; i<10; ++i)
Q = insert(Q, i*2);
display(Q);
putchar('\n');
printf("隊頭結點的值爲:%d\n", Q->front->info);
Q = insert(Q, 666);
printf("666進隊:");
display(Q);
putchar('\n');
Q = dele(Q);
printf("刪除隊頭的結點後爲:");
display(Q);
putchar('\n');
Q = dele(Q);
printf("刪除隊頭的結點後爲:");
display(Q);
putchar('\n');
}
發佈了46 篇原創文章 · 獲贊 16 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章