第一篇:實(shí)驗(yàn)三 棧和隊(duì)列
實(shí)驗(yàn)報(bào)告三 棧和隊(duì)列
班級: 姓名: 學(xué)號: 專業(yè):
一、實(shí)驗(yàn)?zāi)康模?/p>
(1)掌握棧的基本操作的實(shí)現(xiàn)方法。
(2)利用棧先進(jìn)后出的特點(diǎn),解決一些實(shí)際問題。(3)掌握鏈?zhǔn)疥?duì)列及循環(huán)隊(duì)列的基本操作算法。(4)應(yīng)用隊(duì)列先進(jìn)先出的特點(diǎn),解決一些實(shí)際問題。
二、實(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;
粘貼測試數(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()); ------------- 粘貼測試數(shù)據(jù)及運(yùn)行結(jié)果: 3、使用3個(gè)隊(duì)列分別保留手機(jī)上最近10個(gè)“未接來電”、“已接來電”、“已撥電話”。 粘貼源程序: 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(“請選擇記錄類型:”);System.out.println(“ 1、未接來電 2、已接來電 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(); 粘貼測試數(shù)據(jù)及運(yùn)行結(jié)果: 三、心得體會(huì):(含上機(jī)中所遇問題的解決辦法,所使用到的編程技巧、創(chuàng)新點(diǎn)及編程的心得) 一、實(shí)驗(yàn)?zāi)康?掌握棧的數(shù)據(jù)類型描述及棧的特點(diǎn); 掌握棧的順序存儲結(jié)構(gòu)的特點(diǎn)及算法描述; 掌握隊(duì)列的數(shù)據(jù)類型描述及鏈?zhǔn)酱鎯Y(jié)構(gòu)的特點(diǎn)和算法描述。 二、實(shí)驗(yàn)內(nèi)容 停車場管理。設(shè)有一個(gè)可以停放n輛汽車的狹長停車場(先進(jìn)后出),它只有一個(gè)大門可以供車輛進(jìn)出。車輛按到達(dá)停車場時(shí)間的先后依次從停車場最里面向大95E8口處停放(最先到達(dá)的第一輛車停放在停車場的最里面)。如果停車場已放滿n輛車,則后來的車輛只能在停車場大門外的便道上等待,一旦停車場內(nèi)有車離開,則排在便道上的第一輛車就可以進(jìn)入停車場。停車場內(nèi)如有某輛車要離開,在它之后進(jìn)入停車場的車都必須先退出停車場為它讓路,待其開出停車場后,這些車再按原來的次序進(jìn)停車場。每輛車在離開停車場時(shí),都應(yīng)根據(jù)它在停車場內(nèi)停留的時(shí)間長短交費(fèi)。如果停留在便道上的車沒進(jìn)停車場就要離開,允許其離開,不收停車費(fèi),并且仍然保持在便道上的車輛次序。試編程模擬停車場管理。 三、算法描述 提示:可以將停車場定義成一個(gè)順序棧s1,便道定義成一個(gè)鏈隊(duì)列q,而停車場中的某輛車要離開,則在它后面進(jìn)停車場的車必須讓道,讓其離開,故還必須有一個(gè)臨時(shí)的順序棧s2,存放讓道的車輛。 當(dāng)有車輛進(jìn)停車場時(shí),直接進(jìn)入s1棧,若s1棧滿,則進(jìn)入便道(鏈隊(duì)列q)。若有s1中車輛x離開時(shí),先讓在x后面進(jìn)棧的車從s1退棧并進(jìn)棧到s2中,讓x離開并收取停車費(fèi),然后,再把s2中的所有車輛退棧并重新進(jìn)入s1棧,最后,將鏈隊(duì)列q的隊(duì)頭車輛進(jìn)棧到s1中并刪除隊(duì)頭車輛。若有鏈隊(duì)列q(便道)中的車輛y離開時(shí),從鏈隊(duì)列中刪除該車輛即可,不收停車費(fèi)。 車輛的數(shù)據(jù)可以表示為(車輛編號,到達(dá)/離開時(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)存在!請重新輸入: ”; 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<<“便道已有該車牌號,請重新輸入: ”; cin>>x; for(int i=0;i<5;i++) { if(x==q[i]) { cout<<“停車場已有該車牌號,請重新輸入: ”; 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“便道無車輛”;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“便道無車輛”;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<<“車牌號為:”< 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<<“車牌號為:”< break; } } if(i==0)cout<<“無車牌號為:”< SeqStack b;//b是作為臨時(shí)存放車輛的棧 LinkQueue c;//c是作為便道的隊(duì)列 cout<<“tttt1.車輛進(jìn)入”< cout<<“tttt4.便道車輛離開”< int xh1=1;//xh1為菜單最外層的循環(huán)控制變量 int time[100];//記錄各車輛進(jìn)入停車場的時(shí)間 int t1=0;//作為車輛對應(yīng)的時(shí)間編號 int money=1;while(xh1==1){ cout<<“請選擇指令: ”; cin>>zl; switch(zl) { case 1: try{ int n1=a.Count(); int n; cout<<“請輸入車牌號: ”; cin>>n; if(n1==4) { int *Num=a.Return(); for(int i=0;i<=4;i++) if(Num[i]==n) { cout<<“停車場已有該車牌號,請重新輸入!”; cin>>n; i=-1; } int *CarNum=a.Return(); c.EnQueue(n,CarNum); cout<<“停車場已滿,請?jiān)诒愕赖群?”< break; } a.Push(n); cout<<“請輸入進(jìn)入時(shí)間: ”; cin>>time[t1]; while(time[t1]<0||time[t1]>=24) { cout<<“請輸入正確的時(shí)間(0~23時(shí)):”; cin>>time[t1]; } t1++; } catch(char*s){cout< break; case 2: try{int n2;//離開車輛的編號 cout<<“請輸入要離開的車的位置: ”; cin>>n2; if(a.Count()+1==0) { cout<<“該停車場沒有車輛,請選擇其他操作!”; break; } else while(n2<1||n2>a.Count()+1) { cout<<“請輸入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<<“請輸入離開時(shí)間: ”; cin>>time1; while(time1<0||time1>23) { cout<<“請輸入正確的時(shí)間(0~23時(shí)): ”; cin>>time1; } int day=0; if(time1第二篇:實(shí)驗(yàn)三 棧和隊(duì)列的應(yīng)用