数据结构(二):栈

来源:互联网 发布:单片机swap是什么意思 编辑:程序博客网 时间:2024/06/18 09:58
一. 栈

 栈是只能在栈顶插入删除操作的数据结构


1.  顺序栈 : 就是一个只能在结尾操作的数组

 public class SeqStack<E> implements Stack<E> {private Object[] arr ; //用数组实现栈,top指向栈顶的下一个元素private int top;public SeqStack(){arr = new Object[10];}@Overridepublic void clear() {//栈的元素置空,top指向0(栈顶-1)arr = null;top = 0;}@Overridepublic boolean isEmpty() {return top == 0;}@Overridepublic int length() {return top;}@Overridepublic void show() {for(Object o:arr)System.out.print(o+",");}@Overridepublic E peek() {//获取栈顶元素if(top == 0)return null;return (E)arr[top-1];}@Overridepublic E pop() throws Exception {//删除栈顶元素if(top == 0)return null;top--; //出栈:先让top减1,再取值正好是叔组的下标return (E) arr[top];}@Overridepublic void push(E e) {int length = arr.length;if(top == length) //栈满length += length>>1;arr = Arrays.copyOf(arr, length);arr[top++] = e;}public static void main(String[] args) {int[] arr = {1,2};arr = Arrays.copyOf(arr, 5);for(int i:arr){System.out.println(i);}}}
2. 链栈 

 单链表 , 栈顶是top指针后的node, 其插入删除操作都在链表的首节点

public class SingleLinkedList<E> implements List<E>{private Node head; private int length = 0;public SingleLinkedList(){ this.head = new Node(); //初始化头指针(data=null,prior=null)}@Overridepublic void clear() {for(Node node = head.next;node != null;){Node x = node.next;node.data = null;node.next = null;node = x;}head.next = null;length = 0;}@Overridepublic int indexOf(E e) throws Exception {if(length ==0)throw new Exception("链表为空");int index = 0;Node n = head.next;while(n!=null && n.data != e){index++;n = n.next;}if (n!=null)return index;return -1;}@Overridepublic void insert(int index, E e) throws Exception {if (index>length)throw new Exception("超出链表范围");int j = 0;for(Node n = head;n!=null;){if(j!=index){n = n.next;j++;}else{Node newNode = new Node(e); //创建新节点newNode.next = n.next;n.next = newNode;break;}}length ++;}@SuppressWarnings("unchecked")@Overridepublic E get(int index) {if (index > length-1)throw new RuntimeException("超出链表范围");Node n = head.next;for(int j=0;j<=length-1;j++){if(j == index){return (E)n.data;}else{n = n.next;}}return null;}@Overridepublic boolean isEmpty() {return head.next == null;}@Overridepublic int length() {Node n = head.next;int length = 0;while(n != null){length ++;n = n.next;}return length;}@Overridepublic void remove(int index) throws Exception {if (index>length-1)throw new Exception("超出链表范围");int j = 0;for(Node n = head;n!=null;){if(j!=index){n = n.next;j++;}else{Node node = n.next;n.next = node.next;node = null;break;}}length --;}@Overridepublic void show() {Node n = head.next;while(n != null){System.out.print(n.data+",");n = n.next;}}class Node{private Object data;private Node next;public Node(){// 用与初始化head指针this.data = null;this.next = null;}public Node(Object o){ //用于构造新节点this.data = o;this.next = null;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}public static void main(String[] args) throws Exception {SingleLinkedList<Integer> ss = new SingleLinkedList<Integer>();ss.insert(0, 1);ss.insert(1, 2);System.out.println(ss.length);System.out.println(ss.get(1)+"sssssss");;ss.remove(1);ss.show();}}



0 0
原创粉丝点击