I'm trying to create a Hash-table by using an array on linked-nodes (making a linked list).
But I'm having difficulties inserting a value into the Hash-table. When I run it, I get this:
http://gyazo.com/3a28a70e66b3ea34e08223e5948f49c0.png
Here is my code:
#include <iostream>
using namespace std;
class Node {
public:
int num;
Node * next;
};
class intHashTable {
private:
int size;
Node ** table;
public:
intHashTable(int size); // construct a new hash table with size elements
~intHashTable(); // delete the memory for all internal components
void insert(int num); // insert num into the hash table, no effect
// if num is already in table
void remove(int num); // remove num from the hash table, no effect if not in table
int lookup(int num); // return 1 if num is already in table, 0 otherwise
void print(void); // print the elements of the hash table to the screen
};
// construct a new hash table with nelements elements
intHashTable::intHashTable(int nelements)
{
size = nelements;
table = new Node*[size];
for ( int i = 0; i < size; i++ ) {
table[i] = NULL;
}
}
intHashTable::~intHashTable()
{
for(int i=0; i<size; i++)
{
Node* temp = table[i];
while(temp != NULL)
{
Node* next = temp->next;
delete temp;
temp = next;
}
}
size = 0;
delete[] table;
}
void intHashTable::insert(int num){
int location = ((unsigned)num) % size;
Node *runner = table[location];
if(runner == NULL ){
runner->num = num;
}else{
while(runner != NULL ){
runner = runner->next;
}
runner->num = num;
}
}
int main(){
intHashTable a (10);
a.insert(2);
return 0;
}
After construction of intHashTable, all the elements of table are still NULL. However, in the function insert, one element is dereferenced:
Node *runner = table[location];
runner = runner->next;
This makes the program crash, because it is illegal to dereference a null pointer.
the logic here is wrong
int location = ((unsigned)num) % size;
Node *runner = table[location];
if(runner == NULL ) // if null u dereference it!
{
runner->num = num;
}
else
{
while(runner != NULL ) { // u loop until null
runner = runner->next;
}
runner->num = num; // once u reach null u dereference it!
}
i would suggest instead:
first a ctor for your Node
class Node {
public:
int num;
Node * next;
Node( int _n ) : num(_n), next(NULL) { }
};
and then
if ( runner != NULL )
{
while ( runner->next != NULL )
{
runner = runner->next;
}
runner->next = new Node( num );
}
else
{
table[location] = new Node( num );
}
This code certainly won't work:
if(runner == NULL ){
runner->num = num;
If runner is NULL, then you should never dereference it (using * or -> on it).
Node *runner = table[location];
runner = runner->next;
if(runner == NULL )
You never verified whether table[location] is null. But during construction of your hashtable, there are no nodes inside the node table (you set yourself every entry to null).
The problem with your code is that you never think about allocating your node. You should be doing
Node* toInsert = new Node;
toInsert->next= NULL;
toInsert->num = num;
if(table[location]==NULL){
table[location] = toInsert;
}
else{
Node *runner = table[location];
while(runner->next != NULL){
runner = runner->next;
}
runner->next = toInsert;
}
Related
#include <iostream>
class LinkedList {
// too lazy to make stuff private and write getters and setters.
public:
struct Node {
unsigned int ival = 0;
struct Node* link = nullptr;
};
struct Node Node;
LinkedList(unsigned int ival) {
auto temp = new struct Node();
temp->ival = ival;
Node.ival++;
Node.link = temp;
}
void addNode(unsigned int pos, int ival) {
struct Node* newNode = new struct Node();
newNode->ival = ival;
switch (pos)
{
// create a new root node
case 0:
newNode->link = Node.link;
Node.link = newNode;
break;
default:
auto temp = Node.link;
// go to the node before
for (unsigned int i = 0; i < Node.ival - 1 && i < pos - 1; i++) {
temp = temp->link;
}
// link the new Node
newNode->link = temp->link;
// insert node
temp->link = newNode;
}
// increment only at the end
Node.ival++;
}
void deleteNode(unsigned int pos) {
// decrement before to indicate the node will be deleted or the for loop won't work;
Node.ival--;
struct Node* leak = nullptr;
switch (pos)
{
case 0:
// store for deletion to prevent leak
leak = Node.link;
// correct the links to omit the node to be deleted
Node.link = Node.link->link;
break;
default:
auto temp = Node.link;
// go to the node before
for (unsigned int i = 0; i < Node.ival - 1 && i < pos - 1; i++) {
temp = temp->link;
}
// store for deletion to prevent leak
leak = temp->link;
// correct the links to omit the node to be deleted
temp->link = temp->link->link;
break;
}
delete leak;
}
void reverse(unsigned int begin, unsigned int end) {
}
void reverse() {
// root node stored in prev
auto prev = Node.link;
// root->next in next
auto next = prev->link;
while (next != nullptr) {
auto temp_store = next->link;
next->link = prev;
prev = next;
next = temp_store;
}
Node.link->link = nullptr;
Node.link = prev;
}
};
int main() {
LinkedList linkedList(10);
linkedList.addNode(3, 20);
linkedList.addNode(1, 15);
linkedList.addNode(0, 5);
linkedList.addNode(-1, 25);
linkedList.addNode(-1, 30);
linkedList.reverse();
return 0;
}
All the functions have been tested. Warning is received at line 81, namely Node.link->link = nullptr;. What have I missed?
EDIT:
It seems Node is misleading. The structure of the list is supposed to be on the following lines:
Node->node0->node1->node2->....
Node although in itself a full-fledged object is used to store count of nodes and link to the root node.
so i want to implement a queue data structure in c++ and use some special methods like the delete_at() putting into consideration the constraints of the queue data structure, so I made it using the dequeue method and took all the data that are not equal to the index the user want to delete and stored in array in order to enqueue it all back in but without the index that the user want to delete, however nothing gets deleted , so here is the code :
#include <list>
using namespace std;
class Queue{
private:
struct node {
int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
public:
void Enqueue(int d) {
struct node* tmp=new node;
if (rear == NULL) {
tmp->next = NULL;
tmp->data = d;
rear=tmp;
front = rear;
}
else {
rear->next = tmp;
tmp->data = d;
tmp->next = NULL;
rear = tmp;
}
}
int Dequeue() {
struct node* tmp = front;
int data=front->data;
if (front == NULL) {
return 0;
}
else{
if (tmp->next != NULL) {
tmp=front;
front = front->next;
delete tmp;
}
else {
tmp=front;
delete tmp;
front = NULL;
rear = NULL;
}
}
return data;
}
void Display() {
struct node* temp = front;
if (front == NULL) {
cout<<"Queue is empty"<<endl;
return;
}
while (temp != NULL) {
cout<<temp->data<<"\n";
temp = temp->next;
}
}
int Size() {
struct node* temp = front;
int cnt=0;
while (temp != NULL) {
cnt++;
temp = temp->next;
}
return cnt;
}
void Delete_at(int index){
int i=0;
int ar_size=Size();
int data_arr[ar_size-1];
if(index > Size()){
cout<<"\n"<<"Error: out of bounds !";
return;
}
while(i<ar_size){
if (i==(index-1)){
Dequeue();
}
else{
data_arr[i]=Dequeue();
}
i++;
}
i=0;
while(i<ar_size){
Enqueue(data_arr[i]);
i++;
}
}
};
int main() {
int i=0;
Queue q;
q.Enqueue(2);
q.Enqueue(6);
q.Enqueue(7);
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(4);
q.Delete_at(2);
q.Display();
return 0;
}
You have a two primary problems with your code generally, and Delete_at() cannot simply call Dequeue(). As a general note, your code contains duplicated expressions that can simply be consolidated. For example, Enqueue() can be written succinctly as:
void Enqueue(int d) {
struct node *tmp = new node;
tmp->next = nullptr;
tmp->data = d;
if (rear == nullptr) {
front = rear = tmp;
size = 1;
}
else {
rear->next = tmp;
rear = tmp;
size += 1;
}
}
Your Dequeue() function will segfault checking front->data BEFORE checking if front == nullptr. You must check before you dereference, e.g.
int Dequeue() {
struct node *tmp = front;
int data;
if (front == nullptr) { /* (must check before dereference (front->data) */
return 0;
}
data = front->data;
size -= 1;
if (tmp->next != nullptr) {
front = front->next;
}
else {
front = nullptr;
rear = nullptr;
}
delete tmp;
return data;
}
Your Delete_at() function must remove the node at a specific index. This requires that you maintain your ->next links throughout your list, updating the prev->next before the deleted node to point to the node after the one you are deleting. You do that by iterating with both the address of the node and a pointer to node. When you reach the index to remove, you simply replace what is currently at the address of that node with the next node and delete the current, see: Linus on Understanding Pointers
void Delete_at (size_t index) {
struct node *pnode = front, /* pointer to node */
**ppnode = &front; /* address of node */
if (index >= Size()) { /* validate with >= Size() */
std::cerr << '\n' << "Error: out of bounds !";
return;
}
while (index--) { /* loop index times */
ppnode = &pnode->next; /* address of next node */
pnode = pnode->next; /* pointer to next node */
}
*ppnode = pnode->next; /* replace struct at address with next */
delete pnode; /* delete removed node */
size -= 1;
}
Your Size() function simply reduces to a "getter" function:
size_t Size() {
return size;
}
Updating your example a bit and being mindful of Why is “using namespace std;” considered bad practice? your full code could now be:
#include <list>
#include <iostream>
class Queue{
private:
struct node {
int data;
struct node *next;
};
struct node *front = nullptr;
struct node *rear = nullptr;
size_t size;
public:
void Enqueue(int d) {
struct node *tmp = new node;
tmp->next = nullptr;
tmp->data = d;
if (rear == nullptr) {
front = rear = tmp;
size = 1;
}
else {
rear->next = tmp;
rear = tmp;
size += 1;
}
}
int Dequeue() {
struct node *tmp = front;
int data;
if (front == nullptr) { /* (must check before dereference (front->data) */
return 0;
}
data = front->data;
size -= 1;
if (tmp->next != nullptr) {
front = front->next;
}
else {
front = nullptr;
rear = nullptr;
}
delete tmp;
return data;
}
void Display() {
struct node *temp = front;
if (front == nullptr) {
std::cout << "Queue is empty" << '\n';
return;
}
while (temp != nullptr) {
std::cout << temp->data << '\n';
temp = temp->next;
}
}
size_t Size() {
return size;
}
void Delete_at (size_t index) {
struct node *pnode = front, /* pointer to node */
**ppnode = &front; /* address of node */
if (index >= Size()) { /* validate with >= Size() */
std::cerr << '\n' << "Error: out of bounds !";
return;
}
while (index--) { /* loop index times */
ppnode = &pnode->next; /* address of next node */
pnode = pnode->next; /* pointer to next node */
}
*ppnode = pnode->next; /* replace struct at address with next */
delete pnode; /* delete removed node */
size -= 1;
}
};
int main() {
Queue q;
q.Enqueue(2);
q.Enqueue(6);
q.Enqueue(7);
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(4);
q.Display();
std::cout << "\nq.Delete_at(2)\n\n";
q.Delete_at(2);
q.Display();
}
Example Use/Output
$ ./bin/queue_delete_at
2
6
7
1
2
4
q.Delete_at(2)
2
6
1
2
4
Look things over and let me know if you have further questions.
Edit With Additional Constraints From Comments
Per you comments, you have constraints of only being able to use Dequeue() and Enqueue() in Delete_at() and no pointers, etc... You can do that, but understand it will be horribly inefficient compared to simply removing the node at the index. You will essentially have to save (Dequeue()) your entire queue data in an allocated block of memory, omitting the index to remove. You will then need to iterate over all saved values calling Enqueue() to repopulated your list.
You can do that as:
void Delete_at (size_t index) {
if (index >= Size()) { /* validate with >= Size() */
std::cerr << '\n' << "Error: out of bounds !";
return;
}
size_t nelem = Size();
int *arr = new int [nelem],
n = 0;
for (size_t i = 0; i < nelem; i++) {
int tmp = Dequeue();
if (i != index && tmp)
arr[n++] = tmp;
}
for (size_t i = 0; i < (size_t)n; i++)
Enqueue (arr[i]);
delete[] arr;
}
(same output)
For a less readable more C++'ized presentation, you can replace the first loop with:
for (int i = 0, j = Dequeue(); j; i++, j = Dequeue())
if (static_cast<size_t>(i) != index)
arr[n++] = j;
It would be nice to have utilized at least a separate list pointer, so you could build the new list while simultaneously deleting the old, but your class/struct isn't setup to use additional pointers. So you are basically left with buffering all values except the index to remove and then recreating your queue.
//method for printing list
void printList(const ReservationList& ls){
for(int i = 0; i < ls.getLength(); i++){ std::cout<<"ls["<<i<<"]== "<<i+1<<ls.retrieve(i,i+1)<<std::endl; }
}
//main method
int main(){
ReservationList r;
std::cout<<"program starts!"<<std::endl;
std::cout<<"before printing empty list"<<std::endl;
printList(r);
std::cout<<"after printing empty list"<<std::endl;
std::cout<<"inserting starts!"<<std::endl;
r.insert(0,1);
std::cout<<"after inserting 1"<<std::endl;
r.insert(0,2);
std::cout<<"after inserting 2"<<std::endl;
r.insert(0,3);
std::cout<<"after inserting 3"<<std::endl;
r.insert(0,4);
std::cout<<"after inserting 4"<<std::endl;
r.insert(0,5);
std::cout<<"after inserting 5"<<std::endl;
printList(r);
return 0;
}
This the head of ReservationList class(ReservationList.h)
#ifndef RESERVATION_H
#define RESERVATION_H
#include <iostream>
class ReservationList {
public:
ReservationList();/*
ReservationList( const ReservationList& aList );
~ReservationList();*/
bool isEmpty() const;
int getLength() const ;
bool retrieve(int index, int resCode) const;
bool insert(int index, int resCode);
bool remove(int index);
private:
struct ReservationNode {
int Code;
ReservationNode* next;
};
int size;
ReservationNode *head;
ReservationNode *find(int index) const;
};
#endif
And these are the methods I have called so far constructor and insert methods
//empty constructor
ReservationList::ReservationList() {
head = NULL;
size = 0;
}
//insert method
bool ReservationList::insert(int index, int resCode) {
if(index < 0 || index > size) { return 0; }
//making node to be added
ReservationNode* tmp;
std::cout<<"inside insert 1"<<std::endl;
tmp->Code = resCode;/*mistake is hear */
std::cout<<"inside insert 2"<<std::endl;
tmp->next = NULL;
std::cout<<"inside insert 3"<<std::endl;
if ( (index == 0) && (size == 0) ) {
std::cout<<"inside insert 4"<<std::endl;
head = tmp;
size++;
return 1;
}
else if ( (index == 0) && (size == 1) ){
tmp->next = head;
head = tmp;
size++;
return 1;
}
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
tmp->next = curr;
prev->next = tmp;
size++;
return 1;
}
This is the output
program starts!
before printing empty list
after printing empty list
inserting starts!
inside insert 1
[Done] exited with code=3221225477 in 0.622 seconds
with the "std::cout" i tracked the error it is at tmp->Code = resCode; part of the insert method
the problem is at after std::cout<<"inside insert 1"<<std::endl; however when I comment the tmp->Code = resCode; it gives error at the line just after. As I understand there is problem with accessing the variables inside struct or assigning them.
This code snippet
ReservationNode* tmp;
std::cout<<"inside insert 1"<<std::endl;
tmp->Code = resCode;/*mistake is hear */
std::cout<<"inside insert 2"<<std::endl;
tmp->next = NULL;
invokes undefined behavior because the pointer tmp is uninitialized and does not point to a valid object of the type ReservationNode.
It seems you forgot to call the operator new to create an object of the type ReservationNode.
Also calling the function find two times
ReservationNode *curr , *prev;
curr = find( index );
prev = find( index - 1 );
is inefficient.
The function can be defined simpler without a duplicated code.
bool ReservationList::insert( int index, int resCode )
{
bool success = not ( index < 0 || index > size );
if ( success )
{
ReservationNode **current = &head;
while ( index-- )
{
current = &( *current )->next;
}
ReservationNode *new_node = new ReservationNode { resCode, *current };
*current = new_node;
++size;
}
return success;
}
Pay attention to that it would be much better if the data member size will have the unsigned integer type size_t instead of the signed integer type int. The same is valid for the function parameter index.
I have implemented a method to insert a new node before a specific node.
#ifndef FORWARD_SINGLY_LINKED_LIST_H
#define FORWARD_SINGLY_LINKED_LIST_H
#include <cstdlib>
#include <string.h>
#include <iostream>
namespace forward_singly_linked_list {
typedef struct Node {
std::string data;
struct Node *nextPointer;
} Node;
typedef Node *NodeP;
class LinkedList {
private:
int elementsCount;
Node *head;
public:
LinkedList() {
head = NULL;
elementsCount = 0;
}
int get_length() const
{
return elementsCount;
}
// ... ... ...
void add_before(std::string value, std::string before)
{
// empty an area in the memory, and
// save the address of the empty area in 'newNode'
Node *newNode = new Node();
// assign 'value' to the 'data' section of the
// area pointed by 'newNode'
newNode->data = value;
Node * copyOfHead = head;
if (copyOfHead == nullptr)
{
// the list is empty.
// there is no possibility to find 'before'.
// so, return.
return;
}
else
{
bool found = false;
Node * previousNode = nullptr;
while (copyOfHead != nullptr)
{
if (copyOfHead->data == before)
{
found = true;
break;
}
else
{
previousNode = copyOfHead;
copyOfHead = copyOfHead->nextPointer;
}
}
if (!found)
{
return;
}
if (previousNode != nullptr)
{
newNode->nextPointer = previousNode->nextPointer;
previousNode->nextPointer = newNode;
}
else
{
newNode->nextPointer = head;
head = newNode;
}
}
elementsCount++;
}
// ... ... ...
void print() {
Node *copyOfHead = head;
while (copyOfHead != NULL) {
std::cout << copyOfHead->data;
copyOfHead = copyOfHead->nextPointer;
}
std::cout<<"\n\n";
}
public:
static int Test() {
forward_singly_linked_list::LinkedList list;
list.print();
// list.add_at_tail("A");
// list.add_at_tail("B");
// list.add_at_tail("C");
list.print();
list.add_at("-XXX-", 1);
list.print();
return 0;
}
};
}
#endif
Personally, I don't like it because it uses an extra pointer previousNode. I have a feeling that it could be improved.
How can I improve the implementation?
The idea is about link the previous node to input/target node, then link the target/input node to current node.
Here is my code using simple loop index(i) instead of using extra pointer
void Sll::add_at_index( int ind , int value ){
Node *target = new Node( value ) ;
if( ind == 1){
target->next = head ;
head = target ;
}else if( ind == length){
tail->next = target ;
tail = target ;
tail->next = nullptr ;
}else{
Node *curr =head ;
for( int i = 1 ; i < length ; i++ ){
if( i+1 == ind){
Node *Mynext = curr->next ;
curr->next = target ;
target->next = Mynext ;
break ;
}
curr = curr->next ;
}
}
length++ ;
}
Become a two-star programmer:
void add_before(std::string value, std::string_view before) {
auto p = &head;
while (*p && (*p)->data != before)
p = &(*p)->nextPointer;
if (!*p)
return;
*p = new Node {
.data = std::move(value),
.nextPointer = *p,
};
++elementsCount;
}
The code for my implementation of a hashmap can be found below. I'm not exactly sure why it's seg. faulting. I believe it's something to do with the constructor or destructor, but can't seem to figure exactly what.
typedef struct _node
{
char *key;
int value; /* For this, value will be of type int */
struct _node *next; /* pointer to the next node in the list */
} node;
/* HashMap class */
class HashMap
{
private:
node ** hashTable;
int numSlots;
public:
/* Initializes a hashmap given its set size */
HashMap(int size)
{
numSlots = size;
hashTable = new node*[size] ;
for (int i = 0; i < size; i++)
{
hashTable[i] = NULL;
}
}
/* Deconstructor */
~HashMap()
{
for (int i = 0; i < numSlots; i++)
{
node *temp = hashTable[i];
if (temp != NULL)
{
delete temp;
}
}
delete [] hashTable;
}
/*** Hash function. ***/
int hash(char *s)
{
int i;
int sum = 0;
for (i = 0; * (s + i) != '\0'; i++)
{
sum += *(s + i);
}
return (sum % numSlots);
}
/*
*Free all the nodes of a linked list. Helper method for *deconstructor
*/
void free_list(node *list)
{
node *temp;
char *tempKey;
int tempValue;
while (list != NULL)
{
temp = list;
tempKey = temp->key;
tempValue = temp->value;
list = list->next;
if (tempKey != NULL)
{
delete(tempKey);
}
delete(temp);
}
}
/* Create a single node. */
node *create_node(char *key, int value)
{
node *result = new node();
result->key = key;
result->value = value;
result->next = NULL;
return result;
}
/*
*Stores given key/value pair in hashmap
*returns boolean for success/failure
*/
void set (char* key, int value)
{
int keyValue = hash(key);
node *current = hashTable[keyValue];
node *original = current;
node *newNode;
if (current == NULL)
{
hashTable[keyValue] = create_node(key, value);
}
else
{
while (current != NULL)
{
current = current -> next;
}
if (current == NULL)
{
newNode = create_node(key, value);
newNode -> next = original;
hashTable[keyValue] = original;
}
}
}
/* Return a float value representing the load factor
*(item in hash map)/(size of hash map) of the data structure.
*/
float load()
{
float numUsed = 0.0;
for (int i = 0; i < numSlots; i++)
{
if (hashTable[i] != NULL)
{
numUsed++;
}
}
return (numUsed / numSlots);
}
/* Removes value corresponding to inputted key from table */
int remove (char* key)
{
int keyValue = hash(key);
node *listOfInterest = hashTable[keyValue];
if (listOfInterest == NULL)
{
return -999;
}
int toReturn = listOfInterest -> value;
delete(listOfInterest);
return toReturn;
}
/*
* Look for a key in the hash table. Return -999 if not found.
* If it is found return the associated value.
*/
int get(char *key)
{
int keyValue = hash(key);
node *listOfInterest = hashTable[keyValue];
while (listOfInterest != NULL)
{
if (listOfInterest != NULL)
{
return listOfInterest->value;
}
listOfInterest = listOfInterest -> next;
}
return -999;
}
/* Prints hash table */
void print_hash_table()
{
int i;
node *listIterator = NULL;
for (i = 0 ; i < numSlots ; i++)
{
listIterator = hashTable[i];
while (listIterator != NULL)
{
printf("%s %d\n", listIterator->key, listIterator -> value);
listIterator = listIterator -> next;
}
}
}
};
One likely crash is caused by create_node which stores an arbitrary pointer to a string via
result->key = key;
I'll be that you call it with some pointer to char that is destined to be soon inappropriately out of scope. Perhaps struct node should use a std::string to make it more likely to work. Or at least copy the string and create a destructor for node to dispose of that memory if you prefer C-style pointers.
Also, try Valgrind. I'm pretty sure that the problem will be noted.