java数据结构和算法2 表 栈和队列1

1栈(stack)

又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

栈的实现

public class stack {
	int top =-1;
	int arry[];
	public stack(int x) {
		arry=new int[x];
	}
	public boolean stackempty(){
        if(top == -1){
            return true;
        }
        else 
            return false;
    }
	
	public void push(int x){
		 if(top<=arry.length-1){
			 arry[++top] = x;
	        }
	        else{
	            System.out.println("overflow");
	        }
    }
	
	public int pop(){
		 
		if(top>-1){
		 int n=arry[top--];
		 return n;
		 }
		
		else {
            System.out.println("underflow");
            return -1;
        }
  	 }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		stack s=new stack(5);
		s.push(4);s.push(3);s.push(2);s.push(1);
		while( !s.stackempty() )     // 遍历队列并移除所有元素
	     {                            
	     int n = s.pop();   // (40, 50, 60, 70, 80)
	     System.out.print(n);
	     System.out.print(" ");
	     }
	}

	}



1队列( Quan

队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表。

(1)允许删除的一端称为队头(Front)。

(2)允许插入的一端称为队尾(Rear)。

(3)当队列中没有元素时称为空队列。

(4)队列亦称作先进先出(First In First Out)的线性表,简称为FIFO表。

一 循环队列


如上图所示的为循环队列

循环队列中需要注意的几个重要问题:

①队空的判定条件,队空的条件是front=rear;

②队满的判定条件,(rear+1)%QueueSize=front。QueueSize为队列初始空间大小。

	public class Quan{
	private int maxsize;
	private int front; //队头
	private int rear; //队尾
	private int nItems;  //元素的个数
	private long [] arr;
	Quan(int s){
		maxsize=s;
		rear=-1;
		front=0;
		arr=new long[s];
		nItems=0;
	}
	
	public void enquen(long x){
		if(rear==arr.length-1)
			rear=-1;
		arr[++rear] = x;          // 队尾指针加1,把值j加入队尾
	       nItems++;   
	}
	
	public long outquen(){
		 long temp = arr[front++]; // 取值和修改队头指针
	       if(front == maxsize)            // 处理循环
	          front = 0;
	       nItems--;                      
	       return temp;
	}
    public long peekFront()       // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。
    {
    return arr[front];
    }
//--------------------------------------------------------------
	
	 public boolean isEmpty()     // 判队列是否为空。若为空返回一个真值,否则返回一个假值。
    {
    return (nItems==0);
    }
//--------------------------------------------------------------
 	
	public boolean isFull()      // 判队列是否已满。若已满返回一个真值,否则返回一个假值。
    {
    return (nItems==maxsize);
    }
//--------------------------------------------------------------
 	
	public int size()            // 返回队列的长度
    {
    return nItems;
    }
 	public static void main(String [] args){
	 Quan q=new Quan(5);
	 q.enquen(3);
	 q.enquen(4);
	 q.enquen(5);
	 
	 q.enquen(6);
	 
	 while( !q.isEmpty() )     // 遍历队列并移除所有元素
     {                            
     long n = q.outquen();   // (40, 50, 60, 70, 80)
     System.out.print(n);
     System.out.print(" ");
     }
  	System.out.println("");
 	 }  
 	}

队列

队列的链式存储结构简称为链队列。它是限制仅在表头删除和表尾插入的单链表。



/**
	 * 链队列:只能在表头删除,表位添加
	 */
public class LinkQuan {
	
	 private class Node{  
	        public  int data;  
	        public  Node next;  
	        //无参构造函数  
	        public Node(){}  
	          
	        public Node(int data,Node next){  
	            this.data=data;  
	            this.next=next;  
	        }  
	    }
	 	private Node front;  
	    //队列尾指针  
	    private Node rear;  
	    //队列长度  
	    private int size=0;  
	      
	    public LinkQuan(){  
	        Node n=new Node();  
	        n.next=null;  
	        front=rear=n;  
	    }  
	 /** 
	     * 队列入队算法 
	     * @param data 
	     * @author WWX 
	     */  
	    public void enqueue(int data){  
	        //创建一个节点  
	        Node s=new Node(data,null);  
	        //将队尾指针指向新加入的节点,将s节点插入队尾  
	        rear.next=s;  
	        rear=s;  
	        size++;  
	    }    public  int dequeue(){  
	        if(rear==front){  
	            try {  
	                throw new Exception("堆栈为空");  
	            } catch (Exception e) {  
	                e.printStackTrace();  
	            }  
	            return -1;  
	        }else{  
	            //暂存队头元素  
	            Node p=front.next;  
	            int x=p.data;  
	            //将队头元素所在节点摘链  
	            front.next=p.next;  
	            //判断出队列长度是否为1  
	            if(p.next==null)  
	                rear=front;  
	            //删除节点  
	            p=null;  
	            size--;  
	            return  x;  
	        }  
	    }  
	      
	    /** 
	     * 队列长队 
	     * @return 
	     * @author WWX 
	     */  
	    public int size(){  
	        return size;  
	    }  
	      
	    /** 
	     * 判断队列是否为空 
	     * @return 
	     * @author WWX 
	     */  
	    public  boolean isEmpty(){  
	        return  size==0;  
	          
	    }  
	      
	      
	    public String toString() {  
	        if(isEmpty()){  
	            return "[]";  
	        }else{  
	            StringBuilder sb = new StringBuilder("[");  
	            for(Node current=front.next;current!=null;current=current.next){  
	                sb.append(current.data+"" + ", ");  
	            }  
	            int len = sb.length();  
	            return sb.delete(len - 2, len).append("]").toString();  
	        }  
	    }  
	      
	    //测试  
	    public static void main(String[] args) {  
	        LinkQuan queue=new LinkQuan();  
	        queue.enqueue(1);  
	        queue.enqueue(2);  
	        queue.enqueue(3);  
	        queue.enqueue(4);  
	        queue.enqueue(5);  
	        queue.enqueue(6);  
	        System.out.println(queue);  
	        System.out.println("出队:"+queue.dequeue());  
	        System.out.println("队列长度="+queue.size());  
	        System.out.println(queue);  
	        System.out.println("出队:"+queue.dequeue());  
	        System.out.println("队列长度="+queue.size());  
	        System.out.println(queue);  
	        System.out.println("出队:"+queue.dequeue());  
	        System.out.println("队列长度="+queue.size());  
	        System.out.println(queue);  
	    }  
	}  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值