我正在尝试制作一堆哈希表。堆栈用于保存哈希表。我想要一个哈希表的堆栈(在本例中为List)。是否有人将 $scope.ingresarNuevoEmpleador = function(){
console.log("Nombre: " + $scope.objEmpleador_.nombreEmpleador_); //OK
if($scope.frmEmpleadorDirecto.nempleador.$valid) {
console.log('valid!');
}else {
console.log('invalid!');
}
};
和H1
放入我的H2
?一旦我能够完成这个,我打算为一堆哈希表创建一个make pop()和push()函数。我也不允许在这个程序上使用STL代码。
我可能会过度思考或遗漏一些重要的东西。我不确定我是否会采用正确的方式。我一直想弄清楚,但我迷路了。我对这一切都很新鲜。我真的很喜欢解决问题,谢谢。
List.h
List One
List.cpp
#include "Hashtable.h"
#ifndef LIST_H
#define LIST_H
class List
{
private:
typedef struct node
{
//stores the data in each node
int data;
//this creates a node pointer to point to another node in the list
node* next;
}*nodePtr;
//the code above simplifies the code in the comment below
//The code below code allows you to type "nodePtr"
//instead of node* to make node
//typedef struct node* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
//functions in public access and manipulate the private section
//you don't actually access the private section
public:
//constructor sets default values for our values: head, curr, and temp
//putting the class name here let's the program know that we will set those
//values when the list is created.
List();//constructor
//"void" so it returns no data
void AddNode(int addData);
//whatever value we pass through delData,
//the DeleteNode will fgo through the list and search for data that matches
//it and delete the node.
void DeleteNode(int delData);
void PrintList();
};
#endif /* LIST_H */
Hashtable.h此外,#include <iostream>
#include <cassert>
#include <iomanip>
#include <cstdlib>
#include "List.h"
using namespace std;
//now here you define the function from the class
//start out with class name "::" then the things you want to define from List.h
List::List()
{
//values from Link.h
head = NULL;
curr = NULL;
temp = NULL;
}
void List::AddNode(int addData)
{
nodePtr n = new node;
//creating a new node pointer called "n" and a new node.
// our new node pointer points to new node
n->next = NULL;
//find the node n is pointing to access it's next element and make
//that point to nothing
n->data = addData;
if(head != NULL)
{
//make the current pointer point to head(front of the list)
curr = head;
while(curr->next != NULL)
{
curr = curr->next;
}
//if it's not at the end of the list it will exit the while loop
//and point to "n" or the new node
curr->next = n;
}
else
{
//if we don't have a list,the new node will be the front of the list.
head = n;
}
}
void List::DeleteNode(int delData)
{
//now delPtr is the deletion pointer and points to nothing to start with
nodePtr delPtr = NULL;
temp = head;
curr = head;
//while the current node isn't at the end of the list and isn't the data
//that needs to be deleted
while(curr != NULL && curr->data != delData)
{
temp = curr;
curr = curr->next;
}
if(curr == NULL)
{
//if we reach the end of the list
cout << delData << " was not in the list \n";
delete delPtr;
}
else
{
delPtr = curr;
curr = curr->next;
temp->next = curr;
if(delPtr == head)
{
head = head->next;
temp = NULL;
}
delete delPtr;
cout << "The value " << delData << " was deleted \n";
}
}
void List::PrintList()
{
curr = head;
while(curr != NULL)
{
cout << curr->data << endl;
curr = curr->next;
//it'll keep moving to the next node until the end of the list
}
}
是放置在哈希表中的项目的数据。
name
Hashtable.cpp
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#ifndef HASHTABLE_H
#define HASHTABLE_H
class HashTable
{
private:
static const int tableSize = 10;
//everything in the braces are what makes the item
struct obj
{
int name;
obj* next;
};
obj* HASHTBL[tableSize];
public:
HashTable();
//Hash is the function represents where in the hash table
//we will store the key
//take a string stored in variable
int Hash( int key);
void AddObj(int name);
int ItemsinBucket(int index);
void PrintTable();
};
#endif /* HASHTABLE_H */
最后,main.cpp。两个哈希表是测试#include <cstdlib>
#include <iostream>
#include <string>
#include "HashTable.h"
using namespace std;
//Takes from the HashTable class in HashTable.h
HashTable::HashTable()
{
for(int x = 0; x < tableSize; x++)
{
HASHTBL[x] = new obj;
HASHTBL[x]->name = NULL;
HASHTBL[x]->next = NULL;
}
}
void HashTable::AddObj( int name)
{
int index = Hash(name);
if(HASHTBL[index]->name == NULL)
{
HASHTBL[index]->name = name;
}
else
{
obj* Ptr = HASHTBL[index];
obj* n = new obj;
n->name = name;
n->next = NULL;
while(Ptr->next !=NULL)
{
Ptr = Ptr->next;
}
Ptr->next = n;
}
}
int HashTable::ItemsinBucket(int index)
{
int count = 0;
if (HASHTBL[index]->name == NULL)
{
return count;
}
else
{
count++;
obj* Ptr = HASHTBL[index];
while(Ptr->next != NULL)
{
count++;
Ptr = Ptr->next;
}
}
return count;
}
void HashTable::PrintTable()
{
int number;
for(int x = 0; x< tableSize; x++)
{
number = ItemsinBucket(x);
cout << "------------------------\n";
cout << "index = " << x << endl;
cout << HASHTBL[x]->name << endl;
cout << "The num of items in index = "<< number << endl;
cout << "------------------------\n";
}
}
int HashTable::Hash(int key)
{
//defining HashTable function:
int HashTable = 0;
int index;
//will return integer value...or length of string you pass in
for(int x = 0; x < key-1; x++)
{
HashTable = HashTable + x;
}
//so the hash table will take a number and mod it to return the remainder
//the remainder is the index
index = HashTable % tableSize;
return index;
}
和H1
。 H2
意味着两个哈希表的堆栈。
List One
这是输出。顶部是列表的测试和#include <iostream>
#include <cassert>
#include <iomanip>
#include <cstdlib>
#include "List.h"
#include "Hashtable.h"
using namespace std;
int main(int argc, char** argv)
{
List One;
One.AddNode(3);
One.AddNode(5);
One.AddNode(7);
One.PrintList();
One.DeleteNode(3);
One.PrintList();
HashTable H1;
H1.AddObj(4);
H1.AddObj(23);
H1.AddObj(200);
H1.AddObj(10);
H1.AddObj(15);
H1.AddObj(42);
H1.AddObj(33);
H1.AddObj(44);
H1.AddObj(55);
H1.AddObj(5);
H1.AddObj(9);
H1.AddObj(90);
H1.PrintTable();
HashTable H2;
H2.AddObj(10);
H2.AddObj(90);
H2.AddObj(99);
H2.AddObj(34);
H2.AddObj(88);
H2.AddObj(14);
H2.AddObj(87);
H2.AddObj(18);
H2.AddObj(54);
H2.AddObj(56);
H2.AddObj(6);
H2.AddObj(2);
}
函数的测试。
DeleteNode