Exemple #1
0
//Check to make sure that numItems returns correct result on a fresh
// instance of the ArrayQueue class
void testCtor(ArrayQueue<int>& testQueue){
  if(testQueue.getNumItems() == 0){
    std::cout << "SUCCESS: Fresh queue has 0 items" << std::endl;
  } else {
    std::cout << "ERROR: Fresh queue should have 0 items, but has " << testQueue.getNumItems() << std::endl;
  }
}
Exemple #2
0
int main()
{
    string test = "s12fr3";
    ArrayQueue<string> aQueue;
    
    cout<<"The number is: "<<aQueue.getInteger(test);
    
//    string Array;
//    ArrayQueue<string> aQueue;
//    int print = stoi(Array.c_str());
//    
//    cout << "========================================" << endl;
//    cout << "     ENTER THE INTEGER IN THE QUEUE    " << endl;
//    cout << "========================================" << endl;
//    cout << "\t\tENTER THE NUMBER: ";
//    cin >> Array;
//    cout << "========================================" << endl;
//    cout << endl;
//    print = aQueue.getInteger(Array.c_str());
//    cout << "========================================" << endl;
//    cout << "     GETINTEGER() FUNCTION DISPLAY      " << endl;
//    cout << "========================================" << endl;
//    cout << "\t\t\tINTEGER: " << print << endl;
//    cout << "========================================" << endl;
//    
//    
    system("pause");
    return 0;
}
Exemple #3
0
 /**
  * Obtain the current number of active events. This method is
  * intended for testing and debugging.
  *
  * @return number of active events
  **/
 uint32_t countEvents() const {
     uint32_t cnt = _count;
     for (uint32_t i = 0; i < _queue.size(); ++i) {
         cnt += _queue.peek(i).first;
     }
     return cnt;
 }
Exemple #4
0
 /**
  * Initiate the detection of the minimal event barrier starting
  * now. If this method returns false it means that no events were
  * currently active and the minimal event barrier was infinitely
  * small. If this method returns false the handler will not be
  * notified of the completion of the barrier. If this method
  * returns true it means that the started barrier is pending and
  * that the handler passed to this method will be notified of its
  * completion at a later time.
  *
  * @return true if a barrier was started, false if no events were active
  * @param handler handler notified of the completion of the barrier
  **/
 bool startBarrier(T &handler) {
     if (_count == 0 && _queue.empty()) {
         return false;
     }
     _queue.push(std::make_pair(_count, &handler));
     ++_token;
     _count = 0;
     return true;
 }
Exemple #5
0
int main()
{
  ArrayQueue b;
  b.enq("hi");
  ArrayQueue a(b);
  string hold = a.front();
  //cout << hold << endl;
  a.deq();
  return 0;
}
ArrayQueue<T> create_queue(int n)
{
  cout << "Creating rval queue ";
  ArrayQueue<T> q;
  for (int i = 0; i < n; ++i)
    cout << q.put(T(i)) << " ";
  cout << endl;

  return q;
}
Exemple #7
0
//Test to see if your queue still works if we do add, remove, add, remove
// many times
void testAroundTheHorn(ArrayQueue<int>& testQueue){
  for(int i=0;i<1000;i++){
    testQueue.add(i);
    int t = testQueue.remove();
    if(t != i){
      std::cout << "ERROR: Added " << i << " but got back " << t << std::endl;
      return;
    }
  }
  std::cout << "SUCCESS: Added and removed 1000 items successfully" << std::endl;
}
int main()
{
    ArrayQueue q;
    try
    {
        if(q.isEmpty())
        {
            cout << "Queue is empty" << "\n";
        }
        //Enqueue Elements
        q.Enqueue(100);
        q.Enqueue(200);
        q.Enqueue(300);
        //Size of queue
        cout << "size of queue" << q.Size()<<"\n";
        //Front elements
        cout << q.Front() << "\n";
        //Dequeue elements
        cout << q.Dequeue() << "\n";
        cout << q.Dequeue() << "\n";
        cout << q.Dequeue() << "\n";
    }
    catch(...)
    {
        cout << "some exception occured" << "\n";
    }
}
Exemple #9
0
//Test to see if your queue still works if we do add, remove, add, remove
// many times
void testAroundTheHorn(ArrayQueue<int>& testQueue){
	//std::cout << "IM reall HERE" << std::endl;
	for (int i = 0; i<1000; i++){
		testQueue.add(i);
		int t = testQueue.remove();
		//std::cout << "IM HERE"<<t<<"!!" << std::endl;
		//breaks when trying to access i or t
		if (t != i){
			std::cout << "ERROR: Added " << i << " but got back " << t << std::endl;
			return;
		}
	}
	std::cout << "SUCCESS: Added and removed 1000 items successfully" << std::endl;
}
Exemple #10
0
//Test to see if your queue can grow to handle lots of items
void testGrow(ArrayQueue<int>& testQueue){
  for(int i=0;i<1000;i++){
    testQueue.add(i);
  }

  if(testQueue.getNumItems() != 1000){
    std::cout << "ERROR: Should have 1000 items in queue, but only found " << testQueue.getNumItems() << std::endl;
    return;
  }

  for(int i=0;i<1000;i++){
    int t = testQueue.remove();
    if(t != i){
      std::cout << "ERROR: Added " << i << " but got back " << t << std::endl;
      return;
    }
  }
  std::cout << "SUCCESS: Added 1000 items, then removed 1000" << std::endl;
}
Exemple #11
0
//Test to make sure you are throwing an exception if remove is
// called improperly
void testRemoveException(ArrayQueue<int>& testQueue){
  try {
    int t = testQueue.remove();
  } catch (std::string s) {
    std::cout << "SUCCESS: Caught exception: " << s << std::endl;
    return;
  } catch (...) {
    std::cout << "ERROR: Caught an exception, but it wasn't a string type" << std::endl;
    return;
  }

  std::cout << "ERROR: Tried to remove from an empty queue, but did not get an exception" << std::endl;
}
Exemple #12
0
 /**
  * Signal the completion of an event. The value passed to this
  * method must be the same as the return value previously obtained
  * from the startEvent method. This method will signal the
  * completion of all pending barriers that were completed by the
  * completion of this event.
  *
  * @param token opaque token identifying the completed event
  **/
 void completeEvent(uint32_t token) {
     if (token == _token) {
         --_count;
         return;
     }
     --_queue.access(_queue.size() - (_token - token)).first;
     while (!_queue.empty() && _queue.front().first == 0) {
         _queue.front().second->completeBarrier();
         _queue.pop();
     }
 }
void print(const ArrayQueue<T> & q)
{
  cout << "capacity = " << q.capacity() << endl
       << "size     = " << q.size() << endl;

  for (int i = 0; i < q.size(); ++i)
    cout << (T) q.front(i) << " ";
  cout << endl ;

  for (int i = 0; i < q.size(); ++i)
    cout << (T) q.rear(i) << " ";
  cout << endl
       << endl;
}
Exemple #14
0
//Add and remove some items, making sure they come back in the
// correct order
void testAddRemove(ArrayQueue<int>& testQueue){
  testQueue.add(5);
  testQueue.add(10);
  testQueue.add(4);
  if(testQueue.getNumItems() == 3){
    std::cout << "SUCCESS: 3 items added" << std::endl;
  } else {
    std::cout << "ERROR: Added 3 items, but getNumItems says " << testQueue.getNumItems() << std::endl;
    return;
  }
  int x = testQueue.remove();
  int y = testQueue.remove();
  int z = testQueue.remove();
  if(x != 5 || y != 10 || z != 4){
    std::cout << "ERROR: Expected 5, 10, 4, but got " << x <<", " << y << ", " << "z" << std::endl;
  } else {
    std::cout << "SUCCESS: 3 added items came back out in the correct order" << std::endl;
  }
}
Exemple #15
0
int main(int argc, char** argv) {
    ArrayQueue MevaCua; 
    cout<< "Mida actual de la cua: " << MevaCua.size() << endl; 
    cout<< "Encuem 3 elements a la cua... " << endl; 
    
    MevaCua.enqueue('1');
    MevaCua.enqueue('2'); 
    try{
    MevaCua.enqueue('3'); 
    }
    catch(CuaPlena& e){
            cout << e.what() << endl;
    }
    
    MevaCua.print(); 
    cout << "Cua plena (0:no, 1:si): " << MevaCua.full()<<endl; 
    cout<< "Treiem 1er element de la cua: "<<endl;
    try{
    MevaCua.dequeue();
    }
    catch(CuaBuida& e){
            cout << e.what() << endl;
        }
    MevaCua.print(); 
    cout<< "Treiem 2on element de la cua: " <<endl; 
    MevaCua.dequeue();  
    cout<< "Encuem 2 elements a la cua... " << endl; 
    MevaCua.enqueue('4'); 
    MevaCua.enqueue('5');  
    cout<< "Treiem 3er element de la cua: " <<endl; 
    MevaCua.dequeue(); 
    MevaCua.print(); 
    cout<<"Mida actual de la cua: " << MevaCua.size() << endl; 
    cout<<"Cua buida (0:no, 1:si): " << MevaCua.empty()<<endl;
}
Exemple #16
0
 /**
  * Move all items on this queue into the given queue while
  * retaining item order. The items will be inserted at the back of
  * the target queue.
  *
  * @param q the target queue
  **/
 void moveInto(ArrayQueue<T> &q) {
     while (_used > 0) {
         q.emplace(std::move(access(0)));
         pop();
     }
 }
Exemple #17
0
 /**
  * Obtain the current number of pending barriers. This method is
  * intended for testing and debugging.
  *
  * @return number of pending barriers
  **/
 uint32_t countBarriers() const {
     return _queue.size();
 }
Exemple #18
0
/*
 * Sequence of instructions that the queue object can run.
 */
int main(int argc, char** argv) {
    ArrayQueue mevaCua (MAX_QUEUE);
    cout << "Mida actual de la cua: " << mevaCua.size() << endl;
    cout << "Encuem 3 elements a la cua..." << endl;
    mevaCua.enqueue(1); mevaCua.enqueue(2); mevaCua.enqueue(3);
    mevaCua.print();
    cout << "Cua plena (0: no, 1: si): " << mevaCua.full() << endl;
    cout << "Traiem 1er element de la cua: " << mevaCua.dequeue() << endl;
    mevaCua.print();
    cout << "Traiem 2on element de la cua: " << mevaCua.dequeue() << endl;
    cout << "Encuem 2 elements a la cua... " << endl;
    mevaCua.enqueue(4); mevaCua.enqueue(5);
    cout << "Traiem 3er element de la cua: " << mevaCua.dequeue() << endl;
    cout << "Mida actual de la cua: " << mevaCua.size() << endl;
    mevaCua.print();
    cout << "Cua buida (0:no, 1:si): " << mevaCua.empty() << endl;
    return 0;
}
void Display::ShowDataStructItems()
{
	ArrayStack<int> dsArrayStack;
	ListStack<int>	lsListStack;
	ArrayQueue<int> aqArrayQueue;
	CycleDoublyLinkList<int> dcllCycleDoublyLinkList;
	BinarySearchTree<int> bstBinarySearchTree;
	RedBlackTree<int> rbtRedBlackTree;
	MergeFindSet<int> mfsMergeFindSet;
	HashTable<int> htHashTable;
	cout<<WELCOME_STRING<<endl;
	cout<<"This is the Implement of Data Structs"<<endl;
	cout<<"Please Select the Item you are Interest in:"<<endl;
	cout<<"1.	Stack(Implement of Array);"<<endl;
	cout<<"2.	Stack(Implement of List);"<<endl;
	cout<<"3.	Queue(Implement of Array);"<<endl;
	cout<<"4.	Cycle Doubly Linked List"<<endl;
	cout<<"5.	Binary Search Tree"<<endl;
	cout<<"6.	Red Black Tree"<<endl;
	cout<<"7.	Merge Find Set"<<endl;
	cout<<"8.	Hash Table"<<endl;
	cout<<"98.	Up Layer;"<<endl;
	cout<<"99.	Quit."<<endl;
	cout<<STAR_STRING<<endl;
	cout<<endl;
	int nSelect;
	while(1)
	{
		cin>>nSelect;
		if(cin.fail())
		{
			ClearScreen();
			cout<<"Input Error! Please Select Again!"<<endl;
			cin.clear();
			cin.sync();
			ShowDataStructItems();
		}
		ClearScreen();
		switch(nSelect)
		{
		case 1:
			cout<<dsArrayStack.GetTitle().c_str()<<endl;
			dsArrayStack.Description();
			dsArrayStack.Test();
			ShowDataStructItems();
			break;
		case 2:
			cout<<lsListStack.GetTitle().c_str()<<endl;
			lsListStack.Description();
			lsListStack.Test();
			ShowDataStructItems();
			break;
		case 3:
			cout<<aqArrayQueue.GetTitle().c_str()<<endl;
			aqArrayQueue.Description();
			aqArrayQueue.Test();
			ShowDataStructItems();
			break;
		case 4:
			cout<<dcllCycleDoublyLinkList.GetTitle().c_str()<<endl;
			dcllCycleDoublyLinkList.Description();
			dcllCycleDoublyLinkList.Test();
			ShowDataStructItems();
			break;
		case 5:
			cout<<bstBinarySearchTree.GetTitle().c_str()<<endl;
			bstBinarySearchTree.Description();
			bstBinarySearchTree.Test();
			ShowDataStructItems();
			break;
		case 6:
			cout<<rbtRedBlackTree.GetTitle().c_str()<<endl;
			rbtRedBlackTree.Description();
			rbtRedBlackTree.Test();
			ShowDataStructItems();
			break;
		case 7:
			cout<<mfsMergeFindSet.GetTitle().c_str()<<endl;
			mfsMergeFindSet.Description();
			mfsMergeFindSet.Test();
			ShowDataStructItems();
			break;
		case 8:
			cout<<htHashTable.GetTitle().c_str()<<endl;
			htHashTable.Description();
			htHashTable.Test();
			ShowDataStructItems();
			break;
		case 98:
			goto ShowWelcome;
			break;
		case 99:
			exit(0);
			break;
		default:
			ClearScreen();
			cout<<"Select Error! Please Select Again!"<<endl;
			cin.clear();
			cin.sync();
			ShowDataStructItems();
			break;
		}
	}
ShowWelcome:
	Show();
	return;
}
Exemple #20
0
 /**
  * Copy all items on this queue into the given queue while
  * retaining item order. The items will be inserted at the back of
  * the target queue.
  *
  * @param q the target queue
  **/
 void copyInto(ArrayQueue<T> &q) const {
     for (uint32_t i = 0; i < _used; ++i) {
         q.emplace(peek(i));
     }
 }