栈就是一种可以实现“先进后出(或者叫后进先出)”的线性数据结构,是普通线性结构的一个子集。他的特殊性在于他的操作受限,他只能够在表尾进行插入和删除操作的线性表,是数据“后进先出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(); } }
相关推荐
Stack S; cin>>S; cout输出栈:"(); */////////////////////////////////////////////////// char m,n; //选择输入进制 cout请选择输入数的进制!"; cout进制 "进制 "进制 "进制 "; do{ cin>>m; if(m!='...
C语言数据结构中数制转换实例代码 数制转换是严蔚敏的数据结构那本书中的例子,但是那本书中的例子大都是用伪代码的形式写的,不是很容易理解和实现,对初学者造成了不小的困扰,在这里我们将其详尽的实现出来,...
用栈实现数制转换 c语言 void initstack(struct stack *s) { s->base=(int*)malloc(20*sizeof(int)); if(!s->base) exit(0); s->top=s->base; s->stacksize=20; return; }
运用栈的思想解决的。 可以把十进制的数转换成任意进制的数。 数据结构为: typedef struct Stack { int base; int top; int num[stacksize]; }stack;
Python数据结构预算法之栈(Stack)的实现与应用 数据结构预算法.pdf
利用Stack数据结构实现进制的转换,内含详细注释
数据结构 严蔚敏 栈 stack
而现在老师可以立刻通过示范数据结构工具来向学生们展示如何用堆栈执行一些计算,比如数制之间的转换。有了这些知识后,学生可以课后学习数据结构(或算法)的基本原理,甚至可以构造属于他们自己的实现。
数据结构中stack的详细编码及使用 改代码中包含了stack的初始,添加,删除,修改,销毁以及一些简单应用的代码
介绍数据结构栈(Stack)的概念、特点、优缺点、适用场景和Java示例代码
java数据结构 ArrayList、Stack、Map,为提高效率,未做边界判断(由开发人员保证逻辑上不会出现越界),实现了添加和查询的功能,无修改删除功能
这些都是数据结构与算法,一部分方法是团队其他成员实现的,一部分我自己做的,有什么其他实现方法或错误,欢迎各位大佬指点,感谢。 一、栈有什么特点,生活中有什么例子? 栈( stack )又称堆栈,是一
数据结构实例应用(包含源代码) 一、停车场管理 /*停车场管理队列实现,离开时间比进入时间早的情况没做处理*/ #include "consts.h" #define MAXNUM 2 /*车库容量*/ #define price 0.05 /*每车每分钟费用*/ typedef ...
用c语言编写的一个stack程序,实现了进栈,出栈,删除栈顶元素等功能。
数制之间的转换,有良好界面,输入10进制数,可以随意转换成2、8、16进制数
基于C语言的数据结构-栈stack
数据结构_stack_source
这个是用c语言实现的一个stack类数据结构的操作。对c语言堆栈更加深入的认识!
数据结构实验用堆栈实现计算器,大二数据结构实验
void delete_all(Stack<T> &s, const T &x)——删除栈s中所有等于x的数据项,保持其他数据项顺序不变。 输入:input.txt,其第一个字符为x,其后按栈底到栈顶的顺序依次给出栈中字符,字符间用空格、回车或制表符...