Thread: Trying to make a queue!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Trying to make a queue!

    Self explanitory... I am getting access violations. It is close to working but alas...

    QUEUE.H

    Code:
    
    #ifndef QUEUE_H
    #define QUEUE_H
    
    const int MAX = 5;
    
    
    template <class T> struct node{
    	T value;
    	node<T>* ptr;
    };
    
    
    template <class T> class queue{
    
    	private:
    		int counter;
    		node<T>* head;
    		node<T>* tail;
    	
    	public:
    		queue();
    		int count();
    		void enqueue(T newtop);
    		T dequeue();
    		T onTop();	
    		bool isEmpty();  
    		bool isFull();	   
    };
    
    
    template <class T> queue<T>::queue(){
    	counter = 0; 
    	head = NULL; 
    	tail = NULL;
    }
    
    
    template <class T> int queue<T>::count(){ 
    	return counter; 	
    }
    
    
    template <class T> void queue<T>::enqueue(T newtop){
    	node<T>* temp = new node<T>;
    	//srand(time(NULL));
    	
    	if (!isFull()){	   
    		temp->value = newtop;  //you may have to fill in other values.
    		cout << "\nInserting " << temp->value << endl;
    		//temp->ptr = head;
    				
    		//head = temp;
    		
    		temp = NULL;
    
    		tail -> ptr = temp;
    		
    		tail = temp;
    		
    		counter++;
    	}
    	
    	else
    		cout << "Queue full: Cannot exceed " << MAX << " items\n";
    	
    }
    
    
    template <class T> T queue<T>::dequeue(){
    	node<T>* dummyPtr;
    
    	if (!isEmpty()){
    		counter--;
    		dummyPtr = head;
    		T popValue = head->value;
    		head = head->ptr;
    
    		delete dummyPtr;
    		return popValue;	
    	}
    	
    	else{
    		cout << "\n<<Cannot dequeue empty queue, returning T()>>\n";
    		return T();
    	}	   	    
    			
    }		 
    		 	 
    
    template <class T> T queue<T>::onTop(){
    
    	if (!isEmpty()){
    		T peekValue = head->value;
    
    		return peekValue;	 
    	}
    			 
    	else{
    		cout << "\n<<Cannot view top node of empty queue, returning T()>>\n";
    		return T();
    	
    	}
    			
    }
    
    
    template <class T> bool queue<T>::isEmpty(){
    
    	if (counter == 0){
    		return true;	
    	}
    			 
    	else{
    		return false;
    	}
    			
    }	
    
    
    template <class T> bool queue<T>::isFull(){
    
    	if (counter == MAX){
    		return true;	
    	}
    			 
    	else{
    		return false;
    	}
    			
    }	
    
    
    
    #endif

    Next, we have the driver file

    Code:
    #include <iostream>
    #include <conio>
    #include "queue.h"
    using namespace std;
    
    int main(){
    	queue<int> q;
    	q.enqueue(1);
    	q.enqueue(2);
    	q.dequeue();
    	q.enqueue(3);
    	q.enqueue(4);
    	q.dequeue();
        q.dequeue();
    	return 0;
    }





    Anyone willing to help me out; I'd really appreciate it

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I think it may be the node-adding function

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    template <class T> void queue<T>::enqueue(T newtop){
    	node<T>* temp = new node<T>;
    	//srand(time(NULL));
    	
    	if (!isFull()){	   
    		temp->value = newtop;  //you may have to fill in other values.
    		cout << "\nInserting " << temp->value << endl;
    		
    		if (tail != NULL)
    			tail -> ptr = temp;
    		tail = temp;
    		tail -> ptr = NULL;
    		if (head == NULL)
    			head = temp;
    		
    		counter++;
    	}
    	
    	else
    		cout << "Queue full: Cannot exceed " << MAX << " items\n";
    	
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or this.
    Code:
    template <class T> void queue<T>::enqueue(T newtop){
    	//srand(time(NULL));
    	
    	if (!isFull()){	   
    		node<T>* temp = new node<T>;
    		temp->value = newtop;  //you may have to fill in other values.
    		temp->ptr = NULL;
    		cout << "\nInserting " << temp->value << endl;
    		
    		if (tail != NULL)
    			tail -> ptr = temp;
    		tail = temp;
    		if (head == NULL)
    			head = temp;
    		
    		counter++;
    	}
    	
    	else
    		cout << "Queue full: Cannot exceed " << MAX << " items\n";
    	
    }

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Thanks! Did it have anything to do with dereferencing the null pointer?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Thanks! Did it have anything to do with dereferencing the null pointer?

    That's probably the reason. The first time you call the enqueue function, tail is NULL, so tail would be NULL here:

    tail -> ptr = temp;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help quick, compiler/file problem.
    By alex0486 in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2005, 01:03 AM
  2. ADT queue
    By ^xor in forum C Programming
    Replies: 4
    Last Post: 06-15-2005, 05:43 AM
  3. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM
  4. queue linked implementation
    By technoXavage in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 04:29 PM
  5. queue question
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2001, 05:06 PM