第一篇:實(shí)驗(yàn)三 隊(duì)列實(shí)現(xiàn)楊輝三角
實(shí)驗(yàn)3隊(duì)列實(shí)現(xiàn)楊輝三角
一、實(shí)驗(yàn)?zāi)康?/p>
1.熟悉隊(duì)列的邏輯結(jié)構(gòu)。2.回顧常用的存儲(chǔ)結(jié)構(gòu)。
3.掌握System.Collection.Queue類的使用方法。
4.熟悉隊(duì)列的幾種典型應(yīng)用,用隊(duì)列來(lái)解決一些編程問(wèn)題。5.用循環(huán)隊(duì)列實(shí)現(xiàn)楊輝三角并測(cè)試。
二、實(shí)驗(yàn)內(nèi)容
楊輝三角是除了每一行的第一個(gè)元素和最后一個(gè)元素是1,其他元素的值是上一行與之相鄰的兩個(gè)元素之和。
1.實(shí)現(xiàn)循環(huán)隊(duì)列類 a)兩個(gè)構(gòu)造函數(shù) b)入隊(duì) c)出隊(duì)
2.用順序循環(huán)隊(duì)列實(shí)現(xiàn)楊輝三角(一)程序分析 2.1存儲(chǔ)結(jié)構(gòu)
用一片連續(xù)的存儲(chǔ)空間來(lái)存儲(chǔ)循環(huán)隊(duì)列中的數(shù)據(jù)元素,即采用順序存儲(chǔ)的方式。2.2 關(guān)鍵算法分析 1.出隊(duì)
1)自然語(yǔ)言描述: a.判斷隊(duì)列是否空 b.如果隊(duì)列空,拋出異常 c.如果隊(duì)列不空,則
i.ii.iii.元素出隊(duì) 移動(dòng)對(duì)頭指針 隊(duì)列長(zhǎng)度減1 代碼描述:
publicvirtualobjectDequeue()//出隊(duì) {
} if(this._size == 0){ //隊(duì)下溢
} object obj2 = this._array[this._head];//出隊(duì) this._array[this._head] = null;//刪除出隊(duì)元素 //移動(dòng)隊(duì)頭指針
this._head =(this._head + 1)% this._array.Length;this._size--;return obj2;thrownewInvalidOperationException(“隊(duì)列為空”);時(shí)間復(fù)雜度:O(1)2.入隊(duì)
自然語(yǔ)言描述:
d.判斷隊(duì)列是否滿 e.如果隊(duì)列滿,則擴(kuò)容 f.如果隊(duì)列不滿,則
a)元素入隊(duì) b)移動(dòng)隊(duì)尾指針 c)隊(duì)列長(zhǎng)度加1 代碼描述:
publicvirtualvoidEnqueue(objectobj)//入隊(duì) {
} if(this._size == this._array.Length)//當(dāng)數(shù)組滿員時(shí) { //計(jì)算新容量
} this._array[this._tail] = obj;//入隊(duì)
this._tail =(this._tail + 1)% this._array.Length;//移動(dòng)隊(duì)尾指針 this._size++;int capacity =(int)((this._array.Length * this._growFactor)/ 100L);if(capacity <(this._array.Length + _MinimumGrow)){ //最少要增長(zhǎng)4個(gè)元素
} this.SetCapacity(capacity);//調(diào)整容量 capacity = this._array.Length + _MinimumGrow;時(shí)間復(fù)雜度:O(1)3.打印楊輝三角
自然語(yǔ)言描述: 要定義的變量:
1)行:n
2)列:
i.ii.空格 j 數(shù)值
k 3)左肩:left 4)右肩:right 代碼描述:
三、程序運(yùn)行結(jié)果
四、實(shí)驗(yàn)心得
1.調(diào)試時(shí)出現(xiàn)的問(wèn)題及解決的方法
2.心得體會(huì)
3.下一步的改進(jìn)
第二篇:實(shí)驗(yàn)三 棧和隊(duì)列
實(shí)驗(yàn)報(bào)告三 棧和隊(duì)列
班級(jí): 姓名: 學(xué)號(hào): 專業(yè):
一、實(shí)驗(yàn)?zāi)康模?/p>
(1)掌握棧的基本操作的實(shí)現(xiàn)方法。
(2)利用棧先進(jìn)后出的特點(diǎn),解決一些實(shí)際問(wèn)題。(3)掌握鏈?zhǔn)疥?duì)列及循環(huán)隊(duì)列的基本操作算法。(4)應(yīng)用隊(duì)列先進(jìn)先出的特點(diǎn),解決一些實(shí)際問(wèn)題。
二、實(shí)驗(yàn)內(nèi)容:
1、使用一個(gè)棧,將一個(gè)十進(jìn)制轉(zhuǎn)換成二進(jìn)制。粘貼源程序:
package Word1;
public class Node
} T data;Node
} this.data=a;this.next=n;this(a,null);
-----package Word1;
public class Stack
} public Node
}
T a=this.Top.data;this.Top=this.Top.next;return a;this.Top=new Node
--package Word1;
import java.util.*;
public class Test {
} static Scanner scan=new Scanner(System.in);static int temp=0;static int a=0;static Stack
} temp=scan.nextInt();while(true){
} while(s.Top!=null){
} System.out.printf(“%d”,s.Out());a=temp%2;s.push(a);temp=temp/2;if(temp==0)break;
粘貼測(cè)試數(shù)據(jù)及運(yùn)行結(jié)果:
2、回文是指正讀反讀均相同的字符序列,如“acdca”、“dceecd”均是回文,但“book”不是回文。利用1中的基本算法,試寫一個(gè)算法判定給定的字符串是否為回文。(提示:將一半字符入棧,依次彈出與另一半逐個(gè)比較)粘貼源程序:---------package Word1;
import java.util.*;public class Test1 {
} static Scanner sc=new Scanner(System.in);static char[] c={'a','b','c','b','a'};static Stack
} public static String One(){
} public static String Two(){
} for(int i=0;i<(c.length/2);i++){ } for(int i=c.length/2;i } return “該字符串是回文”;if(s.Out()!=c[i])return “該字符不是回文”;s.push(c[i]);for(int i=0;i<(c.length/2);i++){ } for(int i=c.length/2+1;i } return “該字符串是回文”;if(s.Out()!=c[i])return “該字符串不是回文”;s.push(c[i]);if(c.length%2!=0){ } else{ } System.out.println(Two());System.out.println(One()); ------------- 粘貼測(cè)試數(shù)據(jù)及運(yùn)行結(jié)果: 3、使用3個(gè)隊(duì)列分別保留手機(jī)上最近10個(gè)“未接來(lái)電”、“已接來(lái)電”、“已撥電話”。 粘貼源程序: package Word3; import java.util.*; public class Queue LinkedList } } System.out.printf(“%d n”,this.deQ()); package Word3; import java.util.*; public class Test { static Queue } static private void T2(){ int c;int[] a={22324,321321,222333};for(int i=0;i 1、查詢 2、增加”);c=sc.nextInt();if(c==1){ } else{ c=sc.nextInt();while(!(list2.isEmpty()))System.out.printf(“%d n”,list2.deQ());list2.enQ(a[i]);int c=0;System.out.println(“請(qǐng)選擇記錄類型:”);System.out.println(“ 1、未接來(lái)電 2、已接來(lái)電 3、已撥電話”);switch(c=sc.nextInt()){ } case 1:T1();break;case 2:T2();break;case 3:T3();break;Frame(); } } list2.enQ(c);while(!(list2.isEmpty()))System.out.printf(“%d n”,list2.deQ());sc.close();static private void T3(){ } static private void T1(){ int c;int[] a={12324,321321,222333};for(int i=0;i 1、查詢 2、增加”);c=sc.nextInt();if(c==1){ } else{ c=sc.nextInt();while(!(list1.isEmpty()))System.out.printf(“%d n”,list1.deQ());list1.enQ(a[i]);int c;int[] a={32324,321321,222333};for(int i=0;i 1、查詢 2、增加”);c=sc.nextInt();if(c==1){ } else{ } sc.close();c=sc.nextInt();list3.enQ(c);while(!(list3.isEmpty()))System.out.printf(“%d n”,list3.deQ());while(!(list3.isEmpty()))System.out.printf(“%d n”,list3.deQ());list3.enQ(a[i]); } } } list1.enQ(c);while(!(list1.isEmpty()))System.out.printf(“%d n”,list1.deQ());sc.close(); 粘貼測(cè)試數(shù)據(jù)及運(yùn)行結(jié)果: 三、心得體會(huì):(含上機(jī)中所遇問(wèn)題的解決辦法,所使用到的編程技巧、創(chuàng)新點(diǎn)及編程的心得) 一、實(shí)驗(yàn)?zāi)康?掌握棧的數(shù)據(jù)類型描述及棧的特點(diǎn); 掌握棧的順序存儲(chǔ)結(jié)構(gòu)的特點(diǎn)及算法描述; 掌握隊(duì)列的數(shù)據(jù)類型描述及鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的特點(diǎn)和算法描述。 二、實(shí)驗(yàn)內(nèi)容 停車場(chǎng)管理。設(shè)有一個(gè)可以停放n輛汽車的狹長(zhǎng)停車場(chǎng)(先進(jìn)后出),它只有一個(gè)大門可以供車輛進(jìn)出。車輛按到達(dá)停車場(chǎng)時(shí)間的先后依次從停車場(chǎng)最里面向大95E8口處停放(最先到達(dá)的第一輛車停放在停車場(chǎng)的最里面)。如果停車場(chǎng)已放滿n輛車,則后來(lái)的車輛只能在停車場(chǎng)大門外的便道上等待,一旦停車場(chǎng)內(nèi)有車離開(kāi),則排在便道上的第一輛車就可以進(jìn)入停車場(chǎng)。停車場(chǎng)內(nèi)如有某輛車要離開(kāi),在它之后進(jìn)入停車場(chǎng)的車都必須先退出停車場(chǎng)為它讓路,待其開(kāi)出停車場(chǎng)后,這些車再按原來(lái)的次序進(jìn)停車場(chǎng)。每輛車在離開(kāi)停車場(chǎng)時(shí),都應(yīng)根據(jù)它在停車場(chǎng)內(nèi)停留的時(shí)間長(zhǎng)短交費(fèi)。如果停留在便道上的車沒(méi)進(jìn)停車場(chǎng)就要離開(kāi),允許其離開(kāi),不收停車費(fèi),并且仍然保持在便道上的車輛次序。試編程模擬停車場(chǎng)管理。 三、算法描述 提示:可以將停車場(chǎng)定義成一個(gè)順序棧s1,便道定義成一個(gè)鏈隊(duì)列q,而停車場(chǎng)中的某輛車要離開(kāi),則在它后面進(jìn)停車場(chǎng)的車必須讓道,讓其離開(kāi),故還必須有一個(gè)臨時(shí)的順序棧s2,存放讓道的車輛。 當(dāng)有車輛進(jìn)停車場(chǎng)時(shí),直接進(jìn)入s1棧,若s1棧滿,則進(jìn)入便道(鏈隊(duì)列q)。若有s1中車輛x離開(kāi)時(shí),先讓在x后面進(jìn)棧的車從s1退棧并進(jìn)棧到s2中,讓x離開(kāi)并收取停車費(fèi),然后,再把s2中的所有車輛退棧并重新進(jìn)入s1棧,最后,將鏈隊(duì)列q的隊(duì)頭車輛進(jìn)棧到s1中并刪除隊(duì)頭車輛。若有鏈隊(duì)列q(便道)中的車輛y離開(kāi)時(shí),從鏈隊(duì)列中刪除該車輛即可,不收停車費(fèi)。 車輛的數(shù)據(jù)可以表示為(車輛編號(hào),到達(dá)/離開(kāi)時(shí)間)。 四.程序清單: #include SeqStack(){top=-1;} ~SeqStack(){};void Push(int x);void Push2(int x);int *Return();int Pop(int y);int Count();void PrintStack();private: int data[StackSize];int top;};//入棧 void SeqStack::Push(int x){ if(top>=StackSize-1)throw“上溢”;for(int i=0;i<=top+1;i++){ if(data[i]==x) { cout<<“該車牌已經(jīng)存在!請(qǐng)重新輸入: ”; i=-1; cin>>x; } } top++;data[top]=x;} //返回?cái)?shù)組地址 int *SeqStack::Return(){ return data;} //臨時(shí)棧 void SeqStack::Push2(int x){ top++;data[top]=x; } //輸出函數(shù) void SeqStack::PrintStack(){ for(int i=0;i<=top;i++) cout<<“位置為”< int SeqStack::Pop(int y){ if(top==-1)throw“下溢”;int x;x=data[top--];if(y==top+2)data[top+1]=123456789;if(top==-1)data[top+1]=123456789;return x;} //數(shù)數(shù) int SeqStack::Count(){ return top;} //隊(duì)列 struct Node { int data;Node *next;};class LinkQueue { public: LinkQueue();void EnQueue(int x,int *q);void xzDeQueue(int x);int Count();int DeQueue(); private: Node *front,*rear;};//構(gòu)造函數(shù) LinkQueue::LinkQueue(){ Node *s=new Node;s->next=NULL;front=rear=s;} //入隊(duì) void LinkQueue::EnQueue(int x,int *q){ Node *s=new Node;Node *p=new Node;p=front;while(p){ if(p->data ==x) { cout<<“便道已有該車牌號(hào),請(qǐng)重新輸入: ”; cin>>x; for(int i=0;i<5;i++) { if(x==q[i]) { cout<<“停車場(chǎng)已有該車牌號(hào),請(qǐng)重新輸入: ”; cin>>x; i=-1; } } p=front;} p=p->next;} s->data =x;s->next =NULL; rear->next =s;rear=s;} //出隊(duì) int LinkQueue::DeQueue(){ if(front==rear)throw“便道無(wú)車輛”;Node *p=new Node;int x;p=front->next;x=p->data;front->next =p->next;if(p->next ==NULL)rear=front;delete p;return x;} //計(jì)算結(jié)點(diǎn)數(shù) int LinkQueue::Count(){ Node *p=new Node;p=front;int i=0;while(p&&p->next!=NULL){ p=p->next; i++;} return i;} //選擇性出隊(duì) void LinkQueue::xzDeQueue(int x){ if(rear==front)throw“便道無(wú)車輛”;Node *p=new Node;p=front;int y;int i=0;for(;p->next!=NULL;p=p->next) { if(p->next->data ==x) if(p->next->next!=NULL) { Node *q=new Node; q=p->next; y=q->data; p->next =q->next; i=1; delete q; cout<<“車牌號(hào)為:”< break; } else { Node *q=new Node; q=p->next; y=q->data; p->next =NULL; i=1; delete q; if(front->next==NULL)rear=front; cout<<“車牌號(hào)為:”< break; } } if(i==0)cout<<“無(wú)車牌號(hào)為:”< SeqStack b;//b是作為臨時(shí)存放車輛的棧 LinkQueue c;//c是作為便道的隊(duì)列 cout<<“tttt1.車輛進(jìn)入”< cout<<“tttt4.便道車輛離開(kāi)”< int xh1=1;//xh1為菜單最外層的循環(huán)控制變量 int time[100];//記錄各車輛進(jìn)入停車場(chǎng)的時(shí)間 int t1=0;//作為車輛對(duì)應(yīng)的時(shí)間編號(hào) int money=1;while(xh1==1){ cout<<“請(qǐng)選擇指令: ”; cin>>zl; switch(zl) { case 1: try{ int n1=a.Count(); int n; cout<<“請(qǐng)輸入車牌號(hào): ”; cin>>n; if(n1==4) { int *Num=a.Return(); for(int i=0;i<=4;i++) if(Num[i]==n) { cout<<“停車場(chǎng)已有該車牌號(hào),請(qǐng)重新輸入!”; cin>>n; i=-1; } int *CarNum=a.Return(); c.EnQueue(n,CarNum); cout<<“停車場(chǎng)已滿,請(qǐng)?jiān)诒愕赖群?”< break; } a.Push(n); cout<<“請(qǐng)輸入進(jìn)入時(shí)間: ”; cin>>time[t1]; while(time[t1]<0||time[t1]>=24) { cout<<“請(qǐng)輸入正確的時(shí)間(0~23時(shí)):”; cin>>time[t1]; } t1++; } catch(char*s){cout< break; case 2: try{int n2;//離開(kāi)車輛的編號(hào) cout<<“請(qǐng)輸入要離開(kāi)的車的位置: ”; cin>>n2; if(a.Count()+1==0) { cout<<“該停車場(chǎng)沒(méi)有車輛,請(qǐng)選擇其他操作!”; break; } else while(n2<1||n2>a.Count()+1) { cout<<“請(qǐng)輸入1~”< cin>>n2; } int j=a.Count(); for(int i=0;i<(j+1-n2);i++) b.Push2(a.Pop(n2)); a.Pop(n2); int j2=b.Count(); for(int i1=0;i1<=j2;i1++) a.Push(b.Pop(n2)); int j3=c.Count(); int time1; cout<<“請(qǐng)輸入離開(kāi)時(shí)間: ”; cin>>time1; while(time1<0||time1>23) { cout<<“請(qǐng)輸入正確的時(shí)間(0~23時(shí)): ”; cin>>time1; } int day=0; if(time1第三篇:實(shí)驗(yàn)三 棧和隊(duì)列的應(yīng)用