数据结构应用回顾之Stack(一) 数制转换
栈就是一种可以实现“先进后出(或者叫后进先出)”的线性数据结构,是普通线性结构的一个子集。他的特殊性在于他的操作受限,他只能够在表尾进行插入和删除操作的线性表,是数据“后进先出LIFO”,last in first out。
本文简单的实现了stack的基本操作,并写了一个数制转换的代码。
stack 代码
package com.xx.dataStructure.stack;
public class Stack<T> {
static final int defaultSize = 10;
private Object [] data ;
private int maxSize;
private int top;
public Stack(){
this(defaultSize);
}
public Stack(int maxSize){
this.maxSize = maxSize;
data = new Object[maxSize];
}
public void init(){
for(Object s : data){
s = null;
}
top = 0;
}
public void clear(){
while(!isEmpty()){
try {
pop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean isFull(){
return (top == (maxSize - 1));
}
public boolean isEmpty(){
return (top == 0 );
}
public Stack<T> push(T t) throws Exception{
if (isFull()) {
throw new Exception("StackOverFlow");
}
data[top] = t;
top++;
return this;
}
@SuppressWarnings("unchecked")
public T pop() throws Exception {
if (isEmpty())
throw new Exception();
top--;
T t = (T)data[top];
data[top] = null;
return t;
}
public int getLength(){
return top;
}
public void traverse(){
for(int i = top -1 ; i >= 0; i --){
System.out.print(data[i] + ",");
}
System.out.println("");
}
public static void main(String [] args){
Stack<Integer> stack = new Stack<Integer>(5);
stack.init();
try {
stack.push(1).push(2).push(3).push(4);
stack.traverse();
System.out.println(stack.getLength());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.xx.dataStructure.stack;
public class NumberTranslate {
static Stack<Character> stack = new Stack<Character>(100);
{
stack.init();
}
static final Character [] CHAR = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
static final String NUMBRRSTRING = "0123456789ABCDEF";
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(trans10to8(10));
System.out.println(trans10to16(160));
System.out.println(trans16to10("FF"));
} catch (Exception e) {
e.printStackTrace();
}
}
//分治法计算平方
static long square(int x,int n){
long result = 0L;
if (n == 0)
return 1L;
if (n == 1)
return x;
if (n % 2 == 0 ){
long s = square(x,n/2);
result = s*s;
}
if (n % 2 == 1){
long s = square(x,(n-1)/2);
result = s*s*x;
}
return result;
}
static long trans16to10(String number) throws Exception {
char[] ss = number.toCharArray();
long result = 0;
for(int i = 0; i < ss.length; i++){
result += NUMBRRSTRING.indexOf(ss[i])*square(16,ss.length -i -1);
}
return result;
}
static String trans10to8(long number) throws Exception{
long s = number;
while(s / 8 > 0){
stack.push(CHAR[(int)s%8]);
s = s / 8;
}
s = s % 8 ;
stack.push(CHAR[(int)s]);
StringBuffer buf = new StringBuffer();
while(!stack.isEmpty()){
buf.append(stack.pop());
}
return buf.toString();
}
static String trans10to16(long number) throws Exception{
long s = number;
while(s / 16 > 0){
stack.push(CHAR[ (int)s % 16]);
s = s / 16;
}
stack.push(CHAR[ (int)s % 16]);
StringBuffer buf = new StringBuffer();
while(!stack.isEmpty()){
buf.append(stack.pop());
}
return buf.toString();
}
}