Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//************************ intSLList.cpp ************************** #include #include intSLList.h using namespace std; IntSLList::~IntSLList() { for (IntSLLNode *p; !isEmpty(); ) { p = head->next; delete head; head = p;

image text in transcribed

//************************ intSLList.cpp ************************** #include  #include "intSLList.h" using namespace std; IntSLList::~IntSLList() { for (IntSLLNode *p; !isEmpty(); ) { p = head->next; delete head; head = p; } } void IntSLList::addToHead(int el) { head = new IntSLLNode(el,head); if (tail == 0) tail = head; } void IntSLList::addToTail(int el) { if (tail != 0) { // if list not empty; tail->next = new IntSLLNode(el); tail = tail->next; } else head = tail = new IntSLLNode(el); } int IntSLList::deleteFromHead() { int el = head->info; IntSLLNode *tmp = head; if (head == tail) // if only one node on the list; head = tail = 0; else head = head->next; delete tmp; return el; } int IntSLList::deleteFromTail() { int el = tail->info; if (head == tail) { // if only one node on the list; delete head; head = tail = 0; } else { // if more than one node in the list, IntSLLNode *tmp; // find the predecessor of tail; for (tmp = head; tmp->next != tail; tmp = tmp->next); delete tail; tail = tmp; // the predecessor of tail becomes tail; tail->next = 0; } return el; } void IntSLList::deleteNode(int el) { if (head != 0) // if non-empty list; if (head == tail && el == head->info) { // if only one delete head; // node on the list; head = tail = 0; } else if (el == head->info) { // if more than one node on the list IntSLLNode *tmp = head; head = head->next; delete tmp; // and old head is deleted; } else { // if more than one node in the list IntSLLNode *pred, *tmp; for (pred = head, tmp = head->next; // and a non-head node tmp != 0 && !(tmp->info == el);// is deleted; pred = pred->next, tmp = tmp->next); if (tmp != 0) { pred->next = tmp->next; if (tmp == tail) tail = pred; delete tmp; } } } bool IntSLList::isInList(int el) const { IntSLLNode *tmp; for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next); return tmp != 0; } void IntSLList::printAll() const { for (IntSLLNode *tmp = head; tmp != 0; tmp = tmp->next) cout info  

#include #include #include

using namespace std;

template class HeapNode { public: T data; HeapNode *left; HeapNode *right; HeapNode *parent;

HeapNode(const T &new_data) { data = new_data; left = NULL; right = NULL; parent = NULL; } };

template class Heap { private: HeapNode *root; int node_count; public: Heap();

// These functions will be implemented recursively. The following are driver functions // for the programmer to use. void insert(const T&); void print_inorder() const; void print_levelorder() const;

private: HeapNode* insert(const T&, HeapNode*, stack&); void print_inorder(HeapNode*) const; void print_levelorder(HeapNode*, int) const; void swap(T*, T*); };

template Heap::Heap() { root = NULL; node_count = 0; }

template void Heap::insert(const T &new_data) { node_count++; if (root == NULL) { root = new HeapNode(new_data); } else { int tmp = node_count; stack dir; if (tmp > 2) { while (tmp != 1) { dir.push(tmp % 2); tmp = floor(tmp / 2); } } else { dir.push(0); } HeapNode *tmp_node = insert(new_data, root, dir); // Fix min-heap if the newly inserted element breaks min-heap property // percolate up ( Repeatedly exchange node with its parent if needed ) while (tmp_node != root) { if (tmp_node->data parent->data) { int temp = tmp_node->data; tmp_node->data = tmp_node->parent->data; tmp_node->parent->data = temp; } tmp_node= tmp_node->parent; } } }

template HeapNode* Heap::insert(const T &new_data, HeapNode *node, stack &dir) { HeapNode* tmp = new HeapNode(new_data); while (dir.size() > 1) { if (dir.top() == 0) { node = node->left; } else { node = node->right; } dir.pop(); } if (dir.top() == 0) { node->left = tmp; } else { node->right = tmp; } tmp->parent = node;

return tmp; }

template void Heap::print_inorder() const { print_inorder(root); std::cout

template void Heap::print_inorder(HeapNode *node) const { if (node) { print_inorder(node->left); std::cout data right); } } int tempLevel; int tempSpace; int elC; template void Heap::print_levelorder() const { int h = floor(log2(node_count)) + 1; int i; for (i = 1; i

template void Heap::print_levelorder(HeapNode *node, int level) const { if (node == NULL) return; if (level == 1) { elC++; cout data; int a = tempSpace - tempLevel; int spaceNum = 1; while (a > 0) { spaceNum *= 2; a--; } if (elC % 2 == 0) cout left, level - 1); print_levelorder(node->right, level - 1); }

template void Heap::swap(T *x, T *y) { T temp = *x; *x = *y; *y = temp; }

*******************intDrGraph.h************************

#ifndef INTDRGRAPH_H #define INTDRGRAPH_H #include  #include "intSLList.h" #include  #include  #include "queue.h" using namespace std; class intDrGraph { private: bool error; IntSLList **listoflist; int arraysize; public: intDrGraph(string filename) { ifstream file; file.open(filename.c_str()); if (file.is_open() == false) { cout > arraysize; listoflist = new IntSLList *[arraysize]; for (int i = 0; i > from; file >> to; listoflist[from - 1]->addToTail(to - 1); } } } ~intDrGraph() { if (error == true) return; for (int i = 0; i  "; IntSLList *cur = listoflist[i]; for (int j = 1; j GetSize(); j++) { cout GetAt(j) + 1); if (j GetSize()) { cout  "; } } cout  q; visited[starting_index - 1] = true; q.enqueue(starting_index - 1); while (!q.isEmpty()) { starting_index = q.firstElement(); cout GetSize(); i++) { if (!visited[listoflist[starting_index]->GetAt(i)]) { visited[listoflist[starting_index]->GetAt(i)] = true; q.enqueue(listoflist[starting_index]->GetAt(i)); } } } } bool isConnected(int src, int dest) { bool *visited = new bool[arraysize]; queue q; visited[src - 1] = true; q.enqueue(src - 1); while (!q.isEmpty()) { int v = q.firstElement(); q.dequeue(); if (v == dest - 1) { return true; } for (int i = 1; i GetSize(); i++) { if (!visited[listoflist[v]->GetAt(i)]) { visited[listoflist[v]->GetAt(i)] = true; q.enqueue(listoflist[v]->GetAt(i)); } } } return false; } }; #endif

Q (100pts): Write a hash implementation for both integer and up-to-4-digit string type inputs into the main with the prototypes given in the source file main.cpp. Also, you should use linked list implementation given to you previously this semester: IntSL List. The functions you are asked to build are: void insertInto HashTable(int input); void insertInto HashTable(const char* input); bool searchinHash Table(int input); bool searchinHashTable(const char* input); Important: Your work should compile & run along with the example main file provided to you. You can compile multiple spr files using: g++ main.cpp intSlList.cpp Also, make no changes to the given main class except corresponding areas for your functions. You can use existing source and header files intSLList.cpp and intSLList.h which are totally complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions