队列的链表实现

/*** 
   time : 2016 11 29
   author: yb
   title: queue(lian biao)
                    ***/
#ifndef QUEUE_H
#define QUEUE_H

template
   
   
    
     
class Node
{
public:
	Node();
	Node(T item);

	T val;
	Node
    
    
     
     * next;
};

template 
     
     
      
      
class Cqueue
{
public:
	Cqueue();//构造函数
	void enqueue(T item);//入队(先进先出)
    T dequeue();//出对
	bool isEmpty();//是否为空
	int size();//包含的元素个数

//private:
	int qsize;
	Node
      
      
       
       * head;
	Node
       
       
         * tail; }; #endif 
       
      
      
     
     
    
    
   
   
/*** 
   time : 2016 11 29
   author: yb
   title: queue(lian biao)
                    ***/

#include "queue.h"
#include 
   
   
    
    

template 
    
    
     
     
Node
     
     
      
      ::Node(T item):val(item),next(NULL)
{

}

template 
      
      
       
       
Cqueue
       
       
         ::Cqueue():qsize(0),head(NULL),tail(NULL) { } template 
        
          bool Cqueue 
         
           ::isEmpty() { return (qsize == 0); } /* 函数名:void enqueue(T) 参数:T item 输入的元素 功能:入队 返回:void */ template 
          
            void Cqueue 
           
             ::enqueue(T item) { if (isEmpty()) { Node 
            
              *t_node = new Node 
             
               (item); head = t_node; tail = t_node; qsize++; std::cout<<"first the item is:"< 
              
                < 
               
                 * t_node = new Node 
                
                  (item); tail->next = t_node; tail = tail->next; qsize++; std::cout<<"the item is:"< 
                 
                   < 
                  
                    T Cqueue 
                   
                     ::dequeue() { if (isEmpty()) { std::cout<<"the queue is empty!"< 
                    
                      val; Node 
                     
                       * t_node = head; head = head->next; delete t_node; t_node = NULL; qsize--; return item; } else if( head == tail ) { T item = head->val; delete head; head = NULL; qsize--; return item; } return NULL; } template 
                      
                        int Cqueue 
                       
                         ::size() { return qsize; } void main() { Cqueue 
                        
                          myQueue; for (int i = 0 ;i<10;i++) { myQueue.enqueue(i); } while (!myQueue.isEmpty()) { std::cout< 
                         
                           <<" "; } } 
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
我在编写过程中出现的问题,这是我之前没想过的,队列看了很多遍以为没问题了,结果动手编写还是发现了问题:

head 和tail重复删除,导致内存错误,我没有注意head和tail指向同一片内存,所以删除了两遍;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值