`
bolange
  • 浏览: 27086 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

基础数据结构——栈和队列

    博客分类:
  • Java
阅读更多

      所谓的栈,是一个含有至少两个基本操作的抽象数据类型:插入新的元素;删除最近时间插入的元素。遵循FILO(First in,last out,先进后出)的原则。 
      所谓的队列,也是一个含有至少两个基本操作的抽象数据类型:插入新的元素;删除最久时间插入的元素。遵循FIFO(First in,first out,先进先出)的原则。 
      关于栈和队列的具体实现,我们即可以借助于数组,也可以采用链表来实现。
1) 栈的数组实现方式

Java代码 复制代码
  1. public class MyStack<E> {   
  2.     public int count;   
  3.     public Object [] items;   
  4.        
  5.     public boolean isEmpty(){   
  6.         return count==0;   
  7.     }   
  8.        
  9.     public MyStack (){   
  10.   
  11.         items=new Object[20];   
  12.         count=0;   
  13.     }   
  14.        
  15.     public MyStack (int len){   
  16.   
  17.         items=new Object[len];   
  18.         count=0;   
  19.     }   
  20.        
  21.     /**  
  22.     重新调整数组的大小  
  23.     **/  
  24.     private void resize(int size){   
  25.         Object [] newItems=new Object[size];   
  26.         for(int i=0;i<count;i++){   
  27.             newItems[i]=items[i];   
  28.         }   
  29.         items=newItems;   
  30.     }   
  31.        
  32.     public void push(E e){   
  33.         if(count==items.length) resize(2*items.length);   
  34.            
  35.         items[count++]=e;   
  36.     }   
  37.        
  38.     public E pop(){   
  39.         if(count==0return null;   
  40.         E item=(E)items[count-1];   
  41.         items[count-1]=null;   
  42.         count--;   
  43.         if(count>0&&count<=items.length/4) resize(items.length/2);   
  44.         return item;   
  45.     }   
  46.        
  47.     public E peek(){   
  48.         if(count==0return null;   
  49.            
  50.         E item=(E)items[count-1];   
  51.            
  52.         return item;   
  53.     }   
  54. }  

2) 栈的链式实现方式

Java代码 复制代码
  1. public class MyStack<E> {   
  2.     private class Node{   
  3.         E item;   
  4.         Node next;   
  5.     }   
  6.        
  7.     Node head;   
  8.        
  9.     public boolean isEmpty(){   
  10.         return head==null;   
  11.     }   
  12.        
  13.     public void push(E t){   
  14.         Node node=new Node();   
  15.         node.item=t;   
  16.         node.next=head;   
  17.         head=node;   
  18.     }   
  19.        
  20.     public E pop(){   
  21.            
  22.         if(head==null)   
  23.             return null;   
  24.         E t=head.item;   
  25.         head=head.next;   
  26.            
  27.         return t;   
  28.     }   
  29.        
  30.     public E peek(){   
  31.         if(head==null)   
  32.             return null;   
  33.         else  
  34.             return head.item;   
  35.     }   
  36. }  


3) 队列的数组实现

Java代码 复制代码
  1. public class ArrayQueue<E> {   
  2.     private int front;   
  3.     private int rear;   
  4.     private int count;   
  5.     private int capacity;   
  6.     private int capacityIncrement;   
  7.        
  8.        
  9.     private Object[] itemArray;   
  10.        
  11.     public ArrayQueue(){   
  12.         front=0;   
  13.         rear=0;   
  14.         count=0;   
  15.         capacity=10;   
  16.         capacityIncrement=5;   
  17.         itemArray=new Object[capacity];   
  18.     }   
  19.        
  20.     public boolean empty(){   
  21.         return count==0;   
  22.     }   
  23.        
  24.     public void insert(E e){   
  25.         if(count==capacity){   
  26.             capacity+=capacityIncrement;   
  27.             Object [] newArray=new Object[capacity];   
  28. //          for(int i=0;i<count;i++){   
  29. //              newArray[i]=itemArray[i];   
  30. //          }   
  31.             if(front<rear){   
  32.                 //如果元素位于itemArray[front:rear-1]中   
  33.                 for(int i=front;i<rear;i++){   
  34.                     newArray[i]=itemArray[i];   
  35.                 }   
  36.             }else{   
  37.                 //否则,将元素分成两个区间   
  38.                 //区间1:itemArray[0:rear-1]   
  39.                 for(int i=0;i<rear;i++){   
  40.                     newArray[i]=itemArray[i];   
  41.                 }   
  42.                 //区间2:itemArray[front:count-1]   
  43.                 for(int i=front;i<count;i++){   
  44.                     newArray[i]=itemArray[i];   
  45.                 }   
  46.                    
  47.                 front+=capacityIncrement;//然后,将front改为指向其新位置   
  48.             }   
  49.             itemArray=newArray;   
  50.         }   
  51.         itemArray[rear]=e;   
  52.         rear=(rear+1)%capacity;   
  53.         count++;   
  54.     }   
  55.        
  56.     public E remove(){   
  57.         if(count==0){   
  58.             return null;   
  59.         }   
  60.         else{   
  61.             E temp=(E) itemArray[front];   
  62.             itemArray[front]=null;   
  63.             front=(front+1)%capacity;   
  64.             count--;   
  65.                
  66.             return temp;   
  67.         }   
  68.     }   
  69. }  

数组的另一种更简单的实现方式:

Java代码 复制代码
  1. import java.util.NoSuchElementException;   
  2.   
  3. public class ArrayQueue {   
  4.     protected Object [] data;   
  5.     protected int size,   
  6.                     head,   
  7.                     tail;   
  8.     public ArrayQueue(){   
  9.         final int INITIAL_LENGTH=100;   
  10.         data=new Object[INITIAL_LENGTH];   
  11.         size=0;   
  12.         head=0;   
  13.         tail=-1;   
  14.     }   
  15.        
  16.     public int size(){   
  17.         return size;   
  18.     }   
  19.        
  20.     public boolean isEmpty(){   
  21.         return size==0;   
  22.     }   
  23.        
  24.     public Object front(){   
  25.         if(size==0)   
  26.             throw new NoSuchElementException();   
  27.         return data[head];   
  28.     }   
  29.        
  30.     public void enqueue(Object element){   
  31.         if(size==data.length){   
  32.             //double the length of data   
  33.             Object [] oldData=data;   
  34.             data=new Object[data.length*2];   
  35.                
  36.             //copy oldData[head...OldData.length-1]   
  37.             //to  data[0... OldData.length-1-head]    
  38.             System.arraycopy(oldData, head,data , 0, oldData.length-head);   
  39.                
  40.             if(head>0)   
  41.                 //copy oldData[0...tail] to data [head+1 .. oldlenght-1]   
  42.                 System.arraycopy(oldData, 0, data, head+1, tail+1);   
  43.             head=0;   
  44.             tail=oldData.length-1;   
  45.         }   
  46.            
  47.         tail=(tail+1)%data.length;   
  48.         size++;   
  49.         data[tail]=element;   
  50.     }   
  51.        
  52.     public Object dequeue(){   
  53.         if(size--==0){   
  54.             throw new NoSuchElementException();   
  55.         }   
  56.         Object element=data[head];   
  57.         head=(head+1)%data.length;   
  58.            
  59.         return element;   
  60.     }   
  61.                        
  62. }  

4) 队列的链式实现方式

Java代码 复制代码
  1. public class ListQueue<E> {   
  2.     private class Node<E>{   
  3.         E item;   
  4.         Node<E> link;   
  5.     }   
  6.        
  7.     private Node<E> front,rear;   
  8.     private int count;   
  9.        
  10.     public boolean empty(){   
  11.         return count==0;   
  12.     }   
  13.        
  14.     public void insert(E e){   
  15.         //如果队列为空   
  16.         Node<E> newNode=new Node<E>();   
  17.         newNode.item=e;   
  18.            
  19.         if(rear==null){   
  20.             front=rear=newNode;   
  21.         }else{   
  22.             rear.link=newNode;   
  23.             rear=newNode;   
  24.         }   
  25.         count++;   
  26.     }   
  27.        
  28.     public E remove(){   
  29.         if(count==0){   
  30.             return null;   
  31.         }else{   
  32.             E e=front.item;   
  33.             front=front.link;   
  34.             if(front==null){   
  35.                 rear=null;   
  36.             }   
  37.                
  38.             count--;   
  39.                
  40.             return e;   
  41.         }   
  42.     }   
  43.        
  44.     public ListQueue<E> clone(){   
  45.         ListQueue<E> Q=new ListQueue<E>();   
  46.            
  47.         for(Node<E> t=front;t!=null;t=t.link)   
  48.             Q.insert(t.item);   
  49.         return Q;   
  50.     }   
  51. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics