实验三 栈和队列的基本操作实现及其应用 (1)
2017-11-08 14:16
573 查看
实验三 栈和队列的基本操作实现及其应用 (1)
实验内容:
1、自己确定结点的具体数据类型和问题规模:
分别建立一个顺序栈和链栈,实现栈的压栈和出栈操作。分别建立一个顺序队列和链队列,实现队列的入队和出队操作。
2、设计算法并写出代码,实现一个十将二进制转换成2进制数。
实验步骤:
建立一个顺序栈:
1.建立一个封装数据的book类对象,其中实现seter,geter,toString方法。package cn.gdou.stack1; public class Book { public Book(String name, double price) { super(); this.name = name; this.price = price; } private String name ; private double price ; public Book() { // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "书名:+"+this.name+",价格"+this.price+"."; } }
2.建立顺序栈,编写构造函数及其属性。
public class Stack { private int top ; // 栈的顶部 private int size ; //栈的大小 private Object[] listArray ; //存储数据使用的对象数组 public static final int DEFUALSIZE = 10 ; // 默认值 //无参构造 默认大小为10 public Stack() { this.size = DEFUALSIZE ; this.top = 0 ; this.listArray = new Object [DEFUALSIZE] ; } //有参构造 输入要创建的大小 public Stack(int size) { this.size = size ; this.top = 0 ; this.listArray = new Object [size] ; }
编写相关的操作方法:
public boolean isEmpty() { //判断是否为空 return this.top== 0; } public void push(Object obj) { //压栈 if(this.top < this.size){ this.listArray[++ top - 1] = obj; }else{ System.out.println("Stack overflow!"); } } public Object pop() { // 出栈 if(isEmpty()){ System.out.println("Stack is Empty!"); return null ; }else{ this.top -- ; return this.listArray[top - 1]; } } public void display() { //遍历 for (int i = top - 1; i >= 0; i--) { System.out.println(listArray[i].toString()); } } }
3.进行测试:
p 4000 ackage cn.gdou.stack; import cn.gdou.stack.*; public class Stacktesk { public static void main(String[] args) { Book book1 = new Book("java开发",29.9); Book book2 = new Book("HTML开发",25.9); Book book3 = new Book("javaWEB开发",24.9); Book book4 = new Book("IOS开发",23.9); Stack stack1 = new Stack(4) ; stack1.push(book1); stack1.push(book2); stack1.push(book3); stack1.push(book4); stack1.display(); stack1.pop(); System.out.println("~~~~~~~~~~~~~"); stack1.display(); } }
结果如图:
建立一个链栈:
1.建立一个封装数据的Book类,可以使用上一个实验使用的book类。2.建立链栈
附源码
/**********Stack.java**********/ package cn.gdou.stack; //顺序栈的实现 public class Stack { private int top ; private int size ; private Object[] listArray ; public static final int DEFUALSIZE = 10 ; //无参构造 默认大小为10 public Stack() { this.size = DEFUALSIZE ; this.top = 0 ; this.listArray = new Object [DEFUALSIZE] ; } //有参构造 输入要创建的大小 public Stack(int size) { this.size = size ; this.top = 0 ; this.listArray = new Object [size] ; } public boolean isEmpty() { return this.top== 0; } public void push(Object obj) { if(this.top < this.size){ this.listArray[++ top - 1] = obj; }else{ System.out.println("Stack overflow!"); } } public Object pop() { if(isEmpty()){ System.out.println("Stack is Empty!"); return null ; }else{ this.top -- ; return this.listArray[top - 1]; } } public void display() { for (int i = top - 1; i >= 0; i--) { System.out.println(listArray[i].toString()); } } } /**************Book.java****************/ package cn.gdou.stack; public class Book { public Book(String name, double price) { super(); this.name = name; this.price = price; } private String name ; private double price ; public Book() { // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "书名:+"+this.name+",价格"+this.price+"."; } } /***********StackTest.java*************/ package cn.gdou.stack; import cn.gdou.stack.*; public class Stacktesk { public static void main(String[] args) { Book book1 = new Book("java开发",29.9); Book book2 = new Book("HTML开发",25.9); Book book3 = new Book("javaWEB开发",24.9); Book book4 = new Book("IOS开发",23.9); Stack stack1 = new Stack(4) ; stack1.push(book1); stack1.push(book2); stack1.push(book3); stack1.push(book4); stack1.display(); stack1.pop(); System.out.println("~~~~~~~~~~~~~"); stack1.display(); } }
相关文章推荐
- 实验4:栈和队列的基本操作实现及其应用之《顺序队列》
- 实验4:栈和队列的基本操作实现及其应用——链栈
- 实验4:栈和队列的基本操作实现及其应用之《链栈》
- 实验4:栈和队列的基本操作实现及其应用之《进制转换》
- 实验4:栈和队列的基本操作实现及其应用之《进制转换》
- 实验4:栈和队列的基本操作实现及其应用——链队列
- 实验3:栈和队列的基本操作实现及其应用——十进制转换为二进制
- 实验4:栈和队列的基本操作实现及其应用之《链队列》
- 实验三:栈和队列的基本操作实现及其应用——顺序栈
- 实验4:栈和队列的基本操作实现及其应用之《链栈》
- 实验4:栈和队列的基本操作实现及其应用——循环队列
- 实验4:栈和队列的基本操作实现及其应用之《链栈》
- 实验4:栈和队列的基本操作实现及其应用——进制转换以及实验总结
- 数据结构:实验四栈和队列的基本操作实现及其应用
- 实验4:栈和队列的基本操作实现及其应用之《链队列》
- 实验三 栈和队列的基本操作实现及其应用
- 实验4:栈和队列的基本操作实现及其应用之《顺序栈》
- 实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列
- 实验4:栈和队列的基本操作实现及其应用之《排号叫号管理》
- 实验4:栈和队列的基本操作实现及其应用之《链队列》