Java-栈的设计(顺序栈,链表栈)
问题描述】设计栈,并利用设计的栈实现进制转换。
【输入形式】输入1或2,和一个整数。
【输出形式】分别采用顺序栈和链式栈实现将输入的整数转换成二进制数。
【样例输入】1 10
【样例输出】1010
package demo1;
import java.util.Scanner;
interface Stack {
void push(Object obj);
Object pop();
Object getTop();
boolean stackEmpty();
}
//设计链式栈
class LinkStack implements Stack{ //链式栈
private Node top;
public LinkStack() {
top=new Node();
}
public void push(Object obj) { //入栈
Node p=new Node();
p.data=obj;
p.next=top.next;
top.next=p;
}
public Object pop() { //出栈
if(!stackEmpty()) { //链表不为空
Object temp=top.next.data;
top=top.next;
return temp;
}
return null;
}
public Object getTop() { //获得栈顶元素
if(top==null)
return null;
return top.data;
}
public boolean stackEmpty() {//判断是否为空
if(top.next==null)
return true;
return false;
}
class Node{ //node类
private Object data;
private Node next;
public Node() {} //无参构造
public Node(Object data){ //有参构造
this.data=data;
}
}
}
//设计顺序栈
class SqStack implements Stack{ //顺序栈
private Object stack[];
private int top=-1;
private final int INIT_SIZE=10;
private final int SIZE_INCRE=5;
public SqStack(){
stack=new Object[INIT_SIZE]; //无参构造默认值
}
public SqStack(int n) { //有参构造
stack=new Object[n];
}
public void push(Object obj) {
ensureCapacity();
top++;
stack[top]=obj;
}
public void ensureCapacity() {
if((top+1)==stack.length) {
Object obj[]=new Object[stack.length+SIZE_INCRE];
System.arraycopy(stack, 0,obj, 0, stack.length);
stack=obj;
}
}
public Object pop() {
if(top<0)
return null;
int temp=top;
top--;
return stack[temp];
}
public Object getTop() {
if(top<0)
return null;
return stack[top];
}
public boolean stackEmpty() {
if(top==-1)
return true;
else
return false;
}
}
public class demo5 {
public static void main(String[] args) {
int selected;
int m;
Stack stack;
Scanner scan=new Scanner(System.in);
selected=scan.nextInt();
if(selected==1){
stack=new SqStack();
}else{
stack=new LinkStack();
}
m=scan.nextInt();
while(m!=0){
stack.push(m%2);
m=m/2;
}
while(!stack.stackEmpty()){
System.out.print(stack.pop());
}
scan.close();
}
}
泛型接口
【问题描述】根据泛型接口,和main方法,实现泛型顺序栈类和链式栈类。
【输入形式】两个整数:第一个整数,选择使用顺序栈还是链式栈初始化stack对象,第二个整数:要转换成二进制的原始数据。例如:1 10
【输出形式】1010
package demo1;
import java.util.Scanner;
interface Stack<T>{
void push(T t);
T pop();
T getTop();
boolean stackEmpty();
}
class LinkStack<T> implements Stack<T>{
private Node top;
public LinkStack() {
top=new Node();
}
public void push(Object obj) { //入栈
Node p=new Node();
p.data=obj;
p.next=top.next;
top.next=p;
}
public T pop() { //出栈
if(!stackEmpty()) { //链表不为空
T temp=(T) top.next.data;
top=top.next;
return temp;
}
return null;
}
public T getTop() { //获得栈顶元素
if(top==null)
return null;
return (T)top.data;
}
public boolean stackEmpty() {//判断是否为空
if(top.next==null)
return true;
return false;
}
class Node{ //node类
private Object data;
private Node next;
public Node() {} //无参构造
public Node(Object data){ //有参构造
this.data=data;
}
}
}
class SqStack<T> implements Stack<T>{ //顺序栈
private Object stack[];
private int top=-1;
private final int INIT_SIZE=10;
private final int SIZE_INCRE=5;
public SqStack(){
stack=new Object[INIT_SIZE]; //无参构造默认值
}
public SqStack(int n) { //有参构造
stack=new Object[n];
}
public void push(Object obj) {
ensureCapacity();
top++;
stack[top]=obj;
}
public void ensureCapacity() {
if((top+1)==stack.length) {
Object obj[]=new Object[stack.length+SIZE_INCRE];
System.arraycopy(stack, 0,obj, 0, stack.length);
stack=obj;
}
}
public T pop() {
if(top<0)
return null;
int temp=top;
top--;
return (T) stack[temp];
}
public T getTop() {
if(top<0)
return null;
return (T) stack[top];
}
public boolean stackEmpty() {
if(top==-1)
return true;
else
return false;
}
}
public class demo9 {
public static void main(String[] args) {
Stack<Integer> stack=null;
Scanner in=new Scanner(System.in);
switch(in.nextInt()){
case 1:
stack=new LinkStack<Integer>();
break;
case 2:
stack=new SqStack<Integer>();
}
int m;
m=in.nextInt();
while(m!=0){
stack.push(m%2);
m=m/2;
}
while(!stack.stackEmpty()){
System.out.print(stack.pop());
}
in.close();
}
}