第一篇:實驗三 棧和隊列
實驗報告三 棧和隊列
班級: 姓名: 學號: 專業:
一、實驗目的:
(1)掌握棧的基本操作的實現方法。
(2)利用棧先進后出的特點,解決一些實際問題。(3)掌握鏈式隊列及循環隊列的基本操作算法。(4)應用隊列先進先出的特點,解決一些實際問題。
二、實驗內容:
1、使用一個棧,將一個十進制轉換成二進制。粘貼源程序:
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;
粘貼測試數據及運行結果:
2、回文是指正讀反讀均相同的字符序列,如“acdca”、“dceecd”均是回文,但“book”不是回文。利用1中的基本算法,試寫一個算法判定給定的字符串是否為回文。(提示:將一半字符入棧,依次彈出與另一半逐個比較)粘貼源程序:---------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()); ------------- 粘貼測試數據及運行結果: 3、使用3個隊列分別保留手機上最近10個“未接來電”、“已接來電”、“已撥電話”。 粘貼源程序: 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(); 粘貼測試數據及運行結果: 三、心得體會:(含上機中所遇問題的解決辦法,所使用到的編程技巧、創新點及編程的心得) 一、實驗目的 掌握棧的數據類型描述及棧的特點; 掌握棧的順序存儲結構的特點及算法描述; 掌握隊列的數據類型描述及鏈式存儲結構的特點和算法描述。 二、實驗內容 停車場管理。設有一個可以停放n輛汽車的狹長停車場(先進后出),它只有一個大門可以供車輛進出。車輛按到達停車場時間的先后依次從停車場最里面向大95E8口處停放(最先到達的第一輛車停放在停車場的最里面)。如果停車場已放滿n輛車,則后來的車輛只能在停車場大門外的便道上等待,一旦停車場內有車離開,則排在便道上的第一輛車就可以進入停車場。停車場內如有某輛車要離開,在它之后進入停車場的車都必須先退出停車場為它讓路,待其開出停車場后,這些車再按原來的次序進停車場。每輛車在離開停車場時,都應根據它在停車場內停留的時間長短交費。如果停留在便道上的車沒進停車場就要離開,允許其離開,不收停車費,并且仍然保持在便道上的車輛次序。試編程模擬停車場管理。 三、算法描述 提示:可以將停車場定義成一個順序棧s1,便道定義成一個鏈隊列q,而停車場中的某輛車要離開,則在它后面進停車場的車必須讓道,讓其離開,故還必須有一個臨時的順序棧s2,存放讓道的車輛。 當有車輛進停車場時,直接進入s1棧,若s1棧滿,則進入便道(鏈隊列q)。若有s1中車輛x離開時,先讓在x后面進棧的車從s1退棧并進棧到s2中,讓x離開并收取停車費,然后,再把s2中的所有車輛退棧并重新進入s1棧,最后,將鏈隊列q的隊頭車輛進棧到s1中并刪除隊頭車輛。若有鏈隊列q(便道)中的車輛y離開時,從鏈隊列中刪除該車輛即可,不收停車費。 車輛的數據可以表示為(車輛編號,到達/離開時間)。 四.程序清單: #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<<“該車牌已經存在!請重新輸入: ”; i=-1; cin>>x; } } top++;data[top]=x;} //返回數組地址 int *SeqStack::Return(){ return data;} //臨時棧 void SeqStack::Push2(int x){ top++;data[top]=x; } //輸出函數 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;} //數數 int SeqStack::Count(){ return top;} //隊列 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;};//構造函數 LinkQueue::LinkQueue(){ Node *s=new Node;s->next=NULL;front=rear=s;} //入隊 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;} //出隊 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;} //計算結點數 int LinkQueue::Count(){ Node *p=new Node;p=front;int i=0;while(p&&p->next!=NULL){ p=p->next; i++;} return i;} //選擇性出隊 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是作為臨時存放車輛的棧 LinkQueue c;//c是作為便道的隊列 cout<<“tttt1.車輛進入”< cout<<“tttt4.便道車輛離開”< int xh1=1;//xh1為菜單最外層的循環控制變量 int time[100];//記錄各車輛進入停車場的時間 int t1=0;//作為車輛對應的時間編號 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<<“停車場已滿,請在便道等候!”< break; } a.Push(n); cout<<“請輸入進入時間: ”; cin>>time[t1]; while(time[t1]<0||time[t1]>=24) { cout<<“請輸入正確的時間(0~23時):”; 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<<“請輸入離開時間: ”; cin>>time1; while(time1<0||time1>23) { cout<<“請輸入正確的時間(0~23時): ”; cin>>time1; } int day=0; if(time1第二篇:實驗三 棧和隊列的應用