佇列的鏈式儲存結構 C實現

2021-09-27 03:01:02 字數 1532 閱讀 7187

佇列的鏈式儲存結構。佇列常用鍊錶來實現,簡稱為鏈佇列

本次設計的鏈佇列有頭結點,並且隊首指向頭結點,隊尾指向鏈尾結點。

佇列是先進先出的線性表,在隊尾那一端插入元素,在隊首那一端刪除元素。

c語言**:

#define _crt_secure_no_warnings

#include#includetypedef char elemtype;

typedef struct qnode

qnode, *qptr; //定義節點

typedef struct linkqueue

linkqueue; //定義隊首、隊尾指標

void initqueue(linkqueue *q) //初始化

q->front->next = null;

}void pushqueue(linkqueue *q, elemtype e) //插入

p->data = e;

p->next = null;

q->rear->next = p;

q->rear = p;

}void detqueue(linkqueue *q, elemtype &e) //出佇列

qptr p = q->front->next;

e = p->data;

q->front->next = p->next;

if (q->rear == p)

free(p);

}void clearqueue(linkqueue *q) //清空佇列

}void displayqueue(linkqueue *q) //顯示佇列

else

printf("\n"); }}

int main()

scanf("%c", &c);

} c = getchar();//清除緩衝區

printf("佇列元素為:\n");

displayqueue(q);

printf("插入佇列元素為: \n");

scanf("%c", &c);

pushqueue(q, c);

printf("佇列元素為:\n");

displayqueue(q);

printf("出佇列次數: \n");

scanf("%d", &i);

for (int j = 0; j < i; j++)

printf("佇列元素為:\n");

displayqueue(q);

printf("銷毀佇列 \n");

clearqueue(q);

displayqueue(q);

system("pause");

return 0;

}

執行結果:

佇列的鏈式儲存結構及實現

佇列的鏈式儲存結構其實就是線性表的單鏈表,只不過它只能尾進頭出,也稱為鏈佇列。為了操作方便講隊頭指標指向鏈佇列的頭結點,而隊尾指標指向終端節點。空佇列是這樣 下面使用 演示 include include include typedef struct node node,pnode typedef ...

佇列的鏈式儲存結構以及實現

佇列的鏈式儲存結構,其實就是線性表的單鏈表,只不過它只能尾進頭出而已,通常簡稱為鏈佇列。為了操作上的方便,將隊頭指標指向鏈佇列的頭結點,隊尾指標指向終端結點。當隊列為空時,front和rear都指向頭結點。鏈佇列的結構定義如下所示 typedef int qelemtype qnode代表佇列中元素...

佇列的鏈式儲存結構及實現

佇列的鏈式儲存結構,其實就是線性表的單鏈表,只不過它只能尾進頭出而已,稱之為鏈佇列。為了操作上的方便,我們將隊頭指標指向鏈佇列的頭結點,將隊尾指標指向終端結點,如下圖所示 空佇列時,頭尾指標都指向頭結點,如下圖所示 鏈佇列的結構為 typedef int qelemtype qelemtype 型別...