Java数据结构04 栈与队列

来源:互联网 发布:淘宝上牛仔裤 编辑:程序博客网 时间:2024/06/06 01:34

4.1 栈
  栈(Stack)是限定仅在表尾进行茶如何删除操作的线性表。
  
4.1.1 顺序栈

  • 和顺序表一样,顺序栈也采用数组来存放数据元素。
  • 为了保证栈底位置不变,采用数组下标为0的位置作为顺序栈的栈底。
  • 栈顶位置top必须小于StackSize(存储栈长度),最大值为StackSize-1
  • 当栈为空栈时,栈顶指针top=-1。

若现在有一个栈,StackSize是5,则栈普通情况、空栈和栈满的情况示意图可以从下图清楚看出。

 这里写图片描述

进栈:
 ① 栈顶指针top先自增1,给需要进栈的元素腾出内存空间
 ② 然后给top对应的数组元素赋值,data[top] = e

public class SequenceStack {    final int defaultSize = 100;    int top;    int maxSize;    int[] contents;    public SequenceStack(){         contents = new int[defaultSize];        top = -1;    }    public void expand(){        int[] larger = new int[size()*2];        for (int index=0;index<top;index++){            larger[index] = contents[index];        }        contents = larger;    }    public int size() {        return top;    }    public boolean isEmpty() {        return (size()==-1);    }    public void push(int element){        if (top == contents.length){            expand();        }        top++;        contents[top]=element;    }    public int pop(){        if (isEmpty()){            System.out.println("Stack is empty!");            System.exit(1);        }        int result = contents[top];        top--;        return result;          }    public static void main(String[] args) {        SequenceStack stack = new SequenceStack();        System.out.println("0到24依次压栈,然后连续10次出栈");        for(int i=0;i<25;i++){            stack.push(i);        }//      for(int i=0;i<10;i++){//          stack.pop();//      }        System.out.println("栈的大小:"+stack.size());        System.out.println("栈为空么? "+stack.isEmpty());        //System.out.println("栈顶元素为:"+stack.peak());    }}

4.1.2 链式栈

  • 同单链表类似,不过链栈只能从栈顶插入数据;
  • 单链表的头指针作为栈顶指针,而去掉单链表的头结点,这样就能得到链栈了。
  • 链栈不存在栈满的情况,空链栈就是栈顶指针top = null。
public class LinkedStack {    ListNode head;    int size;    public LinkedStack(){        head = null;        size = 0;    }    public int getTop(){        return head.getValue();    }    public boolean isEmpty(){               return head == null;    }    public int pop(){        if (isEmpty()){            System.out.println("空栈");            System.exit(1);        }        int temp = head.getValue();        head = head.getNext();        size--;        return temp;    }    public void push(int temp){        head = new ListNode(temp,head);        size++;    }    public static void main(String[] args){        LinkedStack stack = new LinkedStack();        for (int i=0;i<10;i++){            stack.push(i);        }        for (int i=0;i<5;i++){            stack.pop();        }        System.out.println(stack.getTop());    }    class ListNode{        private int value;        private ListNode next;        public ListNode(int value){            this.value = value;        }        public ListNode(int value, ListNode next){            this.value = value;            this.next = next;        }        public void setValue(int value){            this.value = value;        }        public void setNext(ListNode next){            this.next = next;        }        public int getValue(){            return this.value;        }        public ListNode getNext(){            return this.next;        }        public void display(){            System.out.print(value + " ");        }    }   }

4.2 队列
  队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。允许插入的一端称为队尾,允许删除的一端称为队头。
  这里写图片描述
  
  

0 0