經典的資料結構 佇列

2021-07-22 05:17:39 字數 1564 閱讀 6490

超級經典的資料結構,**如下:

// testconsole.cpp : 定義控制台應用程式的入口點。// 

#include "stdafx.h"

#include #include using namespace std;

struct node

;struct queue

;/*初始化鏈隊*/

void initqueue(struct queue *hq)

/*向鏈隊中插入乙個元素x*/

void inqueue(struct queue *hq, int x)

newnode->data=x; /*把x的值賦給新結點的值域*/

newnode->next=null; /*把新結點的指標域置空*/

/*若鏈隊為空,則新結點即是隊首結點又是隊尾結點*/

if(hq->rear==null)

else

//return;

}/*從佇列中刪除乙個元素*/

int delqueue(struct queue*hq)

temp=hq->front->data;

/*暫存隊首元素以便返回*/

p=hq->front;

/*暫存隊首指標以便**隊尾結點*/

hq->front=p->next; /*使隊首指標指向下乙個結點*/

/*若刪除後鏈隊為空,則需同時使隊尾指標為空*/

if(hq->front==null)

free(p); /***原隊首結點*/

return temp; /*返回被刪除的隊首元素值*/

}/*讀取隊首元素*/

int peekqueue(struct queue *hq)

return hq->front->data; /*返回隊首元素*/

}/*檢查鏈隊是否為空,若為空則返回1,否則返回0*/

int emptyqueue(struct queue *hq)

else }

/*清除鏈隊中的所有元素*/

void clearqueue(struct queue *hq)

/*迴圈結束後隊首指標已經為空*/

hq->rear=null; /*置隊尾指標為空*/

return;

}int _tmain(int argc, _tchar* argv)

; int i;

initqueue(&q);

for(i=0;i<8;i++)

printf("delnode is %d\n",delqueue(&q));

printf("delnode is %d\n",delqueue(&q));

inqueue(&q,68);

printf("peeknode is %d\n",peekqueue(&q));

while(!emptyqueue(&q))

clearqueue(&q);

system("pause");

return 0;

}

全都在vs2012上執行,可用

經典資料結構系列之 佇列的應用

1 前言 資料結構,是計算機程式設計中對資料儲存最基本的操作,不同的資料結構適用不同的業務場景。如今大部分情況都是呼叫開發api封裝好的類庫,直接呼叫,幾乎不需要程式設計師再去深究其中背後實現的邏輯,大大簡化和減低了對程式設計師的要求。正是這種,知其然而不知其所以然,導致很多程式設計師缺乏對於底層結...

資料結構 佇列

一 佇列的迴圈陣列實現。1 初始化 空佇列。令rear front 0。2 入佇列 約定rear指向佇列尾元素的下乙個位置。入佇列時,先判斷佇列是否已滿,而後將array rear x 然後rear 3 出佇列 約定front指向佇列的首元素位置。出佇列時,先判斷佇列是否為空,而後返回隊首元素re ...

資料結構 佇列

資料參考自 資料結構c 語言描述 佇列是一種先進先出的資料結構,這與棧正好相反。下例是簡單的queue實現 queue.h檔案 ifndef queue h define queue h include include 資料元素結構 自定義 struct datatype 佇列元素最大數 const...