鏈佇列以及順序佇列驗證實驗

2021-08-19 08:59:40 字數 3657 閱讀 2735

一、實驗目的

1.掌握佇列的鏈結儲存結構—鏈佇列以及順序儲存結構—順序佇列

2.驗證鏈佇列的儲存結構和基本的實現

3.驗證佇列的操作特性。

二、實驗內容

1.建立乙個空佇列

2.對已建立的佇列進行插入、刪除、取隊頭元素等操作

三、設計與編碼

1.理論知識

定義鏈佇列和順序佇列的資料型別如入隊出隊去隊頭等操作,利用了佇列元素的線性關係和先進先出的特性。

2.演算法設計

鏈佇列驗證實驗

#ifndef linkqueue_h

#define linkqueue_h

template

struct node

datatypedata;

node*next;

template

class linkqueue

public:

linkqueue();

~linkqueue();

voidenqueue(datatype x);

datatypedequeue();

datatypegetqueue();

intempty();

private:

node*front,*rear;

#endif;

#include"linkqueue.h"

template

linkqueue::linkqueue()

node*s=null;

s=newnode;

s->next=null;

front=rear=s;

template

linkqueue::~linkqueue()

node*p=null;

while(front!=null)

p=front->next;

deletefront;

front=p;

template

void linkqueue::enqueue(datatypex)

node*s=null;

s=newnode;

s->data=x;

s->next=null;

rear->next=s;rear=s;

template

datatypelinkqueue::dequeue()

node*p=null;

intx;

if(rear==front)throw"下溢";

p=front->next;

x=p->data;

front->next=p->next;

if(p->next==null)rear=front;

deletep;

returnx;

template

datatypelinkqueue::getqueue()

if(front!=rear)

returnfront->next->data;

template

int linkqueue::empty()

if(front==rear)

return1;

else

return0;

#include

using namespace std;

#include"linkqueue.cpp"

void main()

linkqueue.q;

if(q.empty())

cout<

cout<

q.enqueue(100);

q.enqueue(32);

q.enqueue(69);

catch(char*wrong)

cout

q.dequeue();

q.enqueue(23);

catch(char*wrong)

cout

順序佇列驗證實驗

四、執行與除錯

1.遇到問題為如何建立鏈佇列的指標以及出隊入隊操作,通過查閱書本

2.測試結果如上。

實驗三 順序佇列與鏈佇列

1 熟練掌棧和佇列的結構特點,掌握棧和佇列的順序儲存和鏈式儲存結構和實現。2 學會使用棧和佇列解決實際問題。1 自己確定結點的具體資料型別和問題規模 分別建立乙個順序棧和鏈棧,實現棧的壓棧和出棧操作。分別建立乙個順序佇列和鏈佇列,實現佇列的入隊和出隊操作。2 設計演算法並寫出 實現乙個十將二進位制轉...

實驗四 順序佇列(迴圈佇列)和鏈佇列

1.迴圈佇列 ifndef cirqueue h define cirqueue h const int queuesize 100 定義儲存佇列元素的陣列的最大長度 template 定義模板類cirqueue class cirqueue endif include cirqueue.h tem...

順序表驗證實驗

標頭檔案 ifndef seqlist h define seqlist h const int maxsize 10 class seqlist seqlist int a,int n seqlist void insert int i,int x int delete int i int loc...