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 transcribedimage text in transcribedimage text in transcribedimage 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

int IntSLList::find_min() {

return 0; }

IntSLList IntSLList::sort_list(){ IntSLList list; return list; }

void IntSLList::swap_nodes(int val) {

}

---intSLList.h---

//singly-linked list class to store integers

#ifndef INT_LINKED_LIST #define INT_LINKED_LIST

class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; };

class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; } void addToHead(int); void addToTail(int); int deleteFromHead(); // delete the head and return its info; int deleteFromTail(); // delete the tail and return its info; void deleteNode(int); bool isInList(int) const; void printAll() const; IntSLList sort_list(); void swap_nodes(int); int find_min(); private: IntSLLNode *head, *tail; };

#endif

---main.cpp---

#include #include

// Well, power operation in C++ requires this library. // I'm pretty sure you'll find out why the power operation would be required. #include

#include "intSLList.h"

using namespace std;

// This one is our hash table IntSLList hashTable[10];

// Also we need to store our input types to differentiate strings and large integers // Using 0 for Int, 1 for String, basically? IntSLList hashType[10];

// Function initial declarations void pleaseHelpMeWithHashValues(); void insertIntoHashTable(int input); void insertIntoHashTable(const char* input); bool searchInHashTable(int input); bool searchInHashTable(const char* input); void printHashTable();

// Hashing function for integer inputs. // This function should insert an element only // if there are no duplicates within the table. void insertIntoHashTable(int input) {

int hashAddress = input % 10;

// We need to check whether the value is in hash or not if (!searchInHashTable(input)) { // We need to input for both our Hash Table and Hash Type tables }

return; }

// This may be helpful for your understanding. void pleaseHelpMeWithHashValues() { cout

// Hint: The largest integer that C++ supports is 2147483647 // These characters are 2-digit each. // Let's go with 4-character string support then. // Listed values are greater than 21, that's why we aren't using 5 digits.

return; }

// Hashing function for string inputs. // Remember, one of the best features of C++ is function overloading, // as long as the function signatures are different // you can have two different functions with the same name. void insertIntoHashTable(const char* input) { // Now... Strings. How would you store a string within an integer list? // IntSLList, as the "Int" in its name indicates, only stores Integers. // Then, what magic allows us to somehow do it?.. // Hashing itself! Hashing your string into an integer! // So you should hash your value to hash it. // "ifte kavrulmu" hash? Sort of...

// To treat const char* as a string, you should use string's standard constructor string s = input;

// And you need to hash elements into 2-digits, each, right? // Therefore you need to multiply values to add 0's and separate them.

return; }

// Function to return whether a value is in our hash table or not. bool searchInHashTable(int input) {

// This one is simple. Simply get input's hash value and search input within HashTable[hash]. // There is a function that scans a linked list implemented already. // If the value exists, this function returns true. False otherwise. }

// The same as above, overloaded as hash table insertion. bool searchInHashTable(const char* input) {

// Same as above. However, now you should first hash string to integer for lookup first. }

// You already know printing. No need to take your time with this function. void printHashTable() { for (int i = 0; i

int main() { // ASCII helper function call pleaseHelpMeWithHashValues();

// Sample test lines, comment/delete them initially to be able to run your code. insertIntoHashTable(1); insertIntoHashTable(11); insertIntoHashTable(6); insertIntoHashTable(121); insertIntoHashTable("AAA"); insertIntoHashTable("Y"); insertIntoHashTable("30E"); printHashTable(); cout

return 0; }

C++ please.

Aim: To learn and implement basics of hashing and to improve your understanding of working within constraints (ie, being have to use the asked function types, learning from the comments...) What is hashing? Hashing is the process of converting a given key into another value. It is used for a fast-access data storage in implementations called "Hash Tables. Let's assume that you have values 5,9,23, 4, 78, 8, 42, 7, 1, 271, 251, 420 in a linked list and you want to find 251. Traditionally, you iterate from the beginning and only access 251 in your 11th iteration. However, a hash table with key value of 10 stores data like this: 0:420 1:1, 271, 251 2:42 3:23 4:4 5:5 6: 7:7 8: 78,8 9:9 Simply, takes modulus of these variables and puts them accordingly. Therefore, if you look up for 251, you simply 251 % 10 and get 1 and only check 1's to find 251 for a much higher search speed. This is a simple hash table application. Another implementation of hashing is for validating data within different data types. Such as encryption and svirtographic operations. There will be some future details given after your assignment explanation. So, here is an execution snippet for helping you to visualize: insertintoHashTable(1); insertintoHashTable(11); insertintoHashTable(6); insert IntoHashTable(121); insertintoHashTable("AAA"); insertIntoHashTable("Y"); insertintoHashTable("30"); printHashTable(); cout ? @ABCDEFGHIJKLMNO PQRSTUVWXYZ ASCII val. of 'A' is: 65 Value 65 equals to character: A 1:-/Desktop/cse211as95$

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

Step: 3

blur-text-image

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

How do rules guide verbal communication?

Answered: 1 week ago