- 1、本文档共6页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验报告三 栈和队列
一、 实验目的:
(1) 掌握栈的基本操作的实现方法。
(2) 利用栈先进后出的特点,解决一些实际问题。
(3) 掌握链式队列及循环队列的基本操作算法。
(4) 应用队列先进先出的特点,解决一些实际问题。
二、 实验内容:
1、使用一个栈,将一个十进制转换成二进制。
粘贴源程序:
package Q1;
public class SeqStack {
public int element[];
public int top;
public static SeqStack p;
public SeqStack(int size){
this.element=new int[size];
this.top=-1;
}
public void push(int x){
this.top++;
this.element[this.top]=x;
}
public int pop(){
return this.top==-1 ? -1: (int)this.element[this.top--];
}
public int get(){
return this.top==-1 ? -1: (int)this.element[this.top];
}
public static void disp(SeqStack p){
int t = -2;
while(t!=-1){
t=p.pop();
if(t!=-1)
System.out.printf("%d",t);
}
}
public static void fun(int x){
int t;
while(x!=1){
t=x%2;
x=x/2;
p .push(t);
}
if(x==1)
p .push(x);
}
public static void main(String args[]){
p=new SeqStack(13);
fun (99);
disp(p);
}
}
粘贴测试数据及运行结果:
2、回文是指正读反读均相同的字符序列,如 “acdca”、“dceecd”均是回文,但 “book”
不是回文。利 1 中的基本算法,试写一个算法判定给定的字符串是否为回文。(提示:将
一半字符入栈,依次弹出与另一半逐个比较)
粘贴源程序:
package Q2;
public class SeqStack {
public int element[];
public int top;
public static SeqStack p;
public SeqStack(int size){
this.element=new int[size];
this.top=-1;
}
public void push(int x){
this.top++;
this.element[this.top]=x;
}
public int pop(){
return this.top==-1 ? -1: (int)this.element[this.top--];
}
public int get(){
return this.top==-1 ? -1: (int)this.element[this.top];
}
public static void input(String str){
int i=0;
文档评论(0)