《資料結構》實驗三:棧和佇列實驗報告
阿新 • • 發佈:2019-01-01
一..實驗目的
鞏固棧和佇列資料結構,學會運用棧和佇列。
1.回顧棧和佇列的邏輯結構和受限操作特點,棧和佇列的物理儲存結構和常見操作。
2.學習運用棧和佇列的知識來解決實際問題。
3.進一步鞏固程式除錯方法。
4.進一步鞏固模板程式設計。
二.實驗時間
準備時間為第5周到第6周,具體集中實驗時間為6周第2次課。2個學時。
三..實驗內容
1.自己選擇順序或鏈式儲存結構,定義一個空棧類,並定義入棧、出棧、取棧元素基本操作。然後在主程式中對給定的N個數據進行驗證,輸出各個操作結果。
2.自己選擇順序或鏈式儲存結構,定義一個空棧佇列,並定義入棧、出棧、取棧元素基本操作。然後在主程式中對給定的 N個數據進行驗證,輸出各個操作結果。
3.程式設計實現一個十進位制數轉換成二進位制數。要求,要主程式中輸出一個10進度數,輸出其對應的2進位制數序列。
前兩題是必做題,第3題是選做題。
四.實驗總結
棧和佇列是兩種常用的資料結構,棧和佇列是操作受限的線性表,棧和佇列的資料元素具有單一的前驅和後繼的線性關係;棧和佇列又是兩種重要的抽象資料型別。
棧是限定在表尾進行插入和刪除操作的線性表允許插入和刪除的一端為棧頂,另一端為棧底,出棧元素只能是棧頂元素,後進先出,相鄰元素具有前驅與後繼關係。
佇列是隻允許在一端進行插入操作,在另一端進行刪除操作的線性表。允許插入的一端為隊尾,允許刪除的一端為隊頭,先進先出,相鄰元素具有前驅與後繼關係。
1.
#ifndef seqstack_h #define seqstack_h const int stacksize=10; template<class datatype> class seqstack { public: seqstack(); ~seqstack(){} void push(datatype x); datatype pop(); datatype gettop(); int empty(); private: datatype data[stacksize]; int top; }; #endif #include"seqstack.h" template<class datatype> seqstack<datatype>::seqstack() { top=-1; } template<class datatype> void seqstack<datatype>::push(datatype x) { if(top==stacksize-1)throw"上溢"; top++; data[top]=x; } template<class datatype> datatype seqstack<datatype>::pop() { datatype x; if(top==-1)throw"下溢"; x=data[top--]; return x; } template<class datatype> datatype seqstack<datatype>::gettop() { if(top!=-1); return data[top]; } template<class datatype> int seqstack<datatype>::empty() { if(top==-1) return 1; else return 0; } #include<iostream> using namespace std; #include"seqstack.cpp" void main() { seqstack<int> s; if(s.empty()) cout<<"棧為空"<<endl; else cout<<"棧非空"<<endl; cout<<"對1和5執行入棧操作"<<endl; s.push(1); s.push(5); cout<<"棧頂元素為"<<endl; cout<<s.gettop<<endl; cout<<"執行一次出棧操作"<<endl; s.pop(); cout<<"棧頂元素為"<<endl; cout<<s.gettop<<endl; }
2.
#ifndef linkqueue_h
#define linkqueue_h
template<class datatype>
struct node
{
datatype data;
node<datatype> *next;
};
template<class datatype>
class linkqueue
{
public:
linkqueue();
~linkqueue();
void enqueue(datatype x);
datatype dequeue();
datatype getqueue();
int empty();
private:
node<datatype> *front,*rear;
};
#endif
#include"linkqueue.h"
template<class datatype>
linkqueue<datatype>::linkqueue()
{
node<datatype> *s=NULL;
s=new node<datatype>;
s->next=NULL;
front=rear=s;
}
template<class datatype>
linkqueue<datatype>::~linkqueue()
{
node<datatype> *p=NULL;
while(front!=NULL)
{
p=front->next;
delete front;
front=p;
}
}
template<class datatype>
void linkqueue<datatype>::enqueue(datatype x)
{
node<datatype> *s=NULL;
s=new node<datatype>;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
template<class datatype>
datatype linkqueue<datatype>::dequeue()
{
node<datatype> *p=NULL;
int x;
if(rear==front)throw"下溢";
p=front->next;
x=p->data;
front->next=p->next;
if(p->next==NULL)rear=front;
delete p;
return x;
}
template<class datatype>
datatype linkqueue<datatype>::getqueue()
{
if(front!=rear)
return front->next->data;
}
template<class datatype>
int linkqueue<datatype>::empty()
{
if(front==rear) return 1;
else return 0;
}
#include<iostream>
using namespace std;
#include"linkqueue.cpp"
void main()
{
linkqueue<int> q;
if(q.empty())
cout<<"佇列為空"<<endl;
else
cout<<"佇列非空"<<endl;
cout<<"對元素1和5執行入隊操作"<<endl;
try
{
q.enqueue(1);
q.enqueue(5);
}
catch(char *wrong)
{
cout<<wrong<<endl;
}
cout<<"檢視隊頭元素:"<<endl;
cout<<q.getqueue<<endl;
cout<<"執行出隊操作:"<<endl;
try
{
q.dequeue();
}
catch(char *wrong)
{
cout<<wrong<<endl;
}
cout<<"檢視隊頭元素:"<<endl;
cout<<q.getqueue<<endl;
}