posts - 1,  comments - 0,  trackbacks - 0

          棧和隊列
          所謂的棧,是一個含有至少兩個基本操作的抽象數據類型:插入新的元素;刪除最近時間插入的元素。?遵循FILO(First in,last out,先進后出)的原則。
          所謂的隊列,也是一個含有至少兩個基本操作的抽象數據類型:插入新的元素;刪除最久時間插入的元素。?遵循FIFO(First in,first out,先進先出)的原則。
          關于棧和隊列的具體實現,我們即可以借助于數組,也可以采用鏈表來實現。
          1) 棧的數組實現方式
          public class MyStack<E> {
          public int count;
          public Object [] items;

          public boolean isEmpty(){
          return count==0;
          }

          public MyStack (){

          items=new Object[20];
          count=0;
          }

          public MyStack (int len){

          items=new Object[len];
          count=0;
          }

          /**
          重新調整數組的大小
          **/
          private void resize(int size){
          Object [] newItems=new Object[size];
          for(int i=0;i<count;i++){
          newItems[i]=items[i];
          }
          items=newItems;
          }

          public void push(E e){
          if(count==items.length) resize(2*items.length);

          items[count++]=e;
          }

          public E pop(){
          if(count==0) return null;
          E item=(E)items[count-1];
          items[count-1]=null;
          count--;
          if(count>0&&count<=items.length/4) resize(items.length/2);
          return item;
          }

          public E peek(){
          if(count==0) return null;

          E item=(E)items[count-1];

          return item;
          }
          }
          2) 棧的鏈式實現方式
          public class MyStack<E> {
          private class Node{
          E item;
          Node next;
          }

          Node head;

          public boolean isEmpty(){
          return head==null;
          }

          public void push(E t){
          Node node=new Node();
          node.item=t;
          node.next=head;
          head=node;
          }

          public E pop(){

          if(head==null)
          return null;
          E t=head.item;
          head=head.next;

          return t;
          }

          public E peek(){
          if(head==null)
          return null;
          else
          return head.item;
          }
          }
          3) 隊列的數組實現
          public class ArrayQueue<E> {
          private int front;
          private int rear;
          private int count;
          private int capacity;
          private int capacityIncrement;


          private Object[] itemArray;

          public ArrayQueue(){
          front=0;
          rear=0;
          count=0;
          capacity=10;
          capacityIncrement=5;
          itemArray=new Object[capacity];
          }

          public boolean empty(){
          return count==0;
          }

          public void insert(E e){
          if(count==capacity){
          capacity+=capacityIncrement;
          Object [] newArray=new Object[capacity];
          // for(int i=0;i<count;i++){
          // newArray[i]=itemArray[i];
          // }
          if(front<rear){
          //如果元素位于itemArray[front:rear-1]中
          for(int i=front;i<rear;i++){
          newArray[i]=itemArray[i];
          }
          }else{
          //否則,將元素分成兩個區間
          //區間1:itemArray[0:rear-1]
          for(int i=0;i<rear;i++){
          newArray[i]=itemArray[i];
          }
          //區間2:itemArray[front:count-1]
          for(int i=front;i<count;i++){
          newArray[i]=itemArray[i];
          }

          front+=capacityIncrement;//然后,將front改為指向其新位置
          }
          itemArray=newArray;
          }
          itemArray[rear]=e;
          rear=(rear+1)%capacity;
          count++;
          }

          public E remove(){
          if(count==0){
          return null;
          }
          else{
          E temp=(E) itemArray[front];
          itemArray[front]=null;
          front=(front+1)%capacity;
          count--;

          return temp;
          }
          }
          }
          4) 隊列的鏈式實現方式
          public class ListQueue<E> {
          private class Node<E>{
          E item;
          Node<E> link;
          }

          private Node<E> front,rear;
          private int count;

          public boolean empty(){
          return count==0;
          }

          public void insert(E e){
          //如果隊列為空
          Node<E> newNode=new Node<E>();
          newNode.item=e;

          if(rear==null){
          front=rear=newNode;
          }else{
          rear.link=newNode;
          rear=newNode;
          }
          count++;
          }

          public E remove(){
          if(count==0){
          return null;
          }else{
          E e=front.item;
          front=front.link;
          if(front==null){
          rear=null;
          }

          count--;

          return e;
          }
          }

          public ListQueue<E> clone(){
          ListQueue<E> Q=new ListQueue<E>();

          for(Node<E> t=front;t!=null;t=t.link)
          Q.insert(t.item);
          return Q;
          }
          }

          posted on 2009-11-05 08:52 clovergame 閱讀(74) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 米脂县| 亳州市| 察隅县| 濉溪县| 房产| 肇庆市| 凌海市| 兰考县| 家居| 郧西县| 鸡西市| 山东省| 夏邑县| 随州市| 色达县| 古丈县| 大方县| 綦江县| 莱西市| 吉林省| 新乡市| 齐齐哈尔市| 马公市| 礼泉县| 渝北区| 丹阳市| 锦屏县| 南阳市| 海晏县| 九江市| 桐梓县| 建平县| 防城港市| 开封市| 德保县| 泸西县| 贵德县| 临洮县| 新河县| 长岛县| 华安县|