Question

Instructions Download the files towardsHashTables.cpp, and Queue.h. This is the incomplete example from class last week,...

Instructions

Download the files towardsHashTables.cpp, and Queue.h.

This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues.

In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there is a need for a destructor. To implement it, you can just keep popping elements off the queue until there are no elements left.

Implement the 3 functions required in Queue.h and upload it here

Queue.h file

#ifndef Queue_h
#define Queue_h

#include <iostream>

struct Link {
    long data;
    Link* next;
    
    Link(){
        data = 0;
        next = NULL;
    }
    
    Link (long d){
        data = d;
        next = NULL;
    }
};

struct Queue {
    Link* front;
    Link* back;
    
    Queue (){
        front = NULL;
        back = NULL;
    }
    
    long peek () {
        return front->data;
    }
    
    void push(long value){
        if (isEmpty()){
            front = new Link(value);
            back = front;
        }
        else {
            back->next = new Link(value);
            back = back->next;
        }
    }
    
    bool find (long value){
        // Provide your code here

    }
    
    bool isEmpty(){
        return (front == NULL);
    }
    
    long pop(){
        long val = front->data;
        
        Link* oldFront = front;
        front = front->next;
        
        delete oldFront;
        
        return val;
    }
    
    void print() {
        // Provide your code here

    }
    
    ~Queue(){
        // Provide your code here

    }
};

#endif

hashtable.cpp

#include <iostream>
#include <vector>
#include "Queue.h"

using namespace std;

long k = 10;

long f (long x){
    return x % k;
}

int main() {
    
    // Declare a vector of Queue pointers
    vector<Queue*> hashtable;
    
    // Initialize the vector with k empty Queues
    for (long i = 0; i < k; i++) {
        hashtable.push_back(new Queue());
    }
    
    // This is the value I want to insert
    long value = 234;
    
    // Run it through the hash function to determine
    // to which queue we need to push it
    long index = f(value);
    
    // Push value to the index-th Queue
    hashtable[index]->push(value);
    
    // One more time
    value = 23;
    
    index = f(value);
    hashtable[index]->push(value);
    
    // One more time
    value = 23453;
    
    index = f(value);
    hashtable[index]->push(value);
    
    
    // Let's do some searching
    
    // This is what I am searching for
    value = 7;
    
    // Which queue should I be looking at
    index = f(value);
    
    // Look for it in the queue pointed to by index
    cout << hashtable[index]->find(value) << endl << endl;
    
    // Print contents of hash table
    for (long i = 0; i < k; i++) {
        Queue* current = hashtable[i];
        
        cout << i << ": ";
        
        current->print();
        cout << endl;
    }
    
    for (long i = 0; i < k; i++) {
        delete hashtable[i];
    }
    
    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#ifndef Queue_h
#define Queue_h
struct Link {
long data;
Link* next;
  
Link(){
data = 0;
next = NULL;
}
  
Link (long d){
data = d;
next = NULL;
}
};

struct Queue {
Link* front;
Link* back;
  
Queue (){
front = NULL;
back = NULL;
}
  
long peek () {
return front->data;
}
  
void push(long value){
if (isEmpty()){
front = new Link(value);
back = front;
}
else {
back->next = new Link(value);
back = back->next;
}
}
  
bool find (long value){
// Provide your code here
Link* curr = back;
while(curr != NULL){
  
if(curr->data==value)
return true;
  
curr = curr->next;
  
}
return false;

}
  
bool isEmpty(){
return (front == NULL);
}
  
long pop(){
long val = front->data;
  
Link* oldFront = front;
front = front->next;
  
delete oldFront;
  
return val;
}
  
void print() {
// Provide your code here
  
Link* curr = back;
while(curr != NULL){
  
cout<<curr->data<<" ";
curr = curr->next;
  
}
}
  
~Queue(){
// Provide your code here
Link* curr = back, *temp;
while(curr != NULL){
  
temp = curr;
curr = curr->next;
delete temp;
  
  
}

  

}
};

#endif

================================
gcc version 4.6.3 share Files saved main.cpp 42 43 main.cpp back->next new Link (value); back back->next; 3: 23453 4: 234 45

Add a comment
Know the answer?
Add Answer to:
Instructions Download the files towardsHashTables.cpp, and Queue.h. This is the incomplete example from class last week,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is the incomplete example from class last week, where we started implementing a hash table...

    This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues. In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there is a need for a destructor. To...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • //This is the implementation file queue.cpp. //This is the implementation of the template class Queue. //The...

    //This is the implementation file queue.cpp. //This is the implementation of the template class Queue. //The interface for the template class Queue is in the header file queue.h. #include <iostream> #include <cstdlib> #include <cstddef> #include "queue.h" using std::cout; namespace QueueSavitch { //Uses cstddef: template<class T> Queue<T>::Queue( ) : front(NULL), back(NULL) { //Intentionally empty. } //Uses cstddef: template<class T> bool Queue<T>::isEmpty( ) const { return (back == NULL);//front == NULL would also work } //Uses cstddef: template<class T> void Queue<T>::add(T item)...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • Please answer in C++ Derive a class called Queue from the linked list described in Assignment...

    Please answer in C++ Derive a class called Queue from the linked list described in Assignment 2 (list of Dates). This means the Queue class will inherit all the properties (data and functions) of the linked list. But, since a queue allows pushing only at the back and popping at the front of the list, you will need to prevent the addition in the front and removal at the back. To do this, you must derive the Queue class in...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT