如何在C ++中将哈希表放入堆栈?

时间:2016-12-10 23:36:49

标签: c++ linked-list hashmap stack hashtable

我试图用push和pop函数创建一堆哈希表。我有推送和弹出,但是有没有办法修改堆栈以将H1H2放入堆栈One?目前,我的堆栈持有用于测试目的的int。

以下是所有标头和源文件。我真的很喜欢这个问题的解决方案。感谢。

Stack.h

#ifndef STACK_H
#define STACK_H

#include <string>

using namespace std;

class Stack
{

private:

    struct item
    {   
        int value;
        item* prev;   
    };

    item* stackPtr;


public:

    void Push(int value);
    void Pop();
    void ReadItem(item* r);
    void Print();

    Stack(); //called when stack is created
    ~Stack();//called when stacked item is ended

};
#endif /* STACK_H */

Stack.cpp

#include <iostream>
#include <cstdlib>
#include "Stack.h"

using namespace std;

Stack::Stack()
{
    stackPtr = NULL;
}

Stack::~Stack()
{
    item* p1; //pointer 1
    item* p2; //pointer 2

    p1 = stackPtr;
    while(p1 != NULL)
    {
        p2 = p1;
        p1 = p1->prev;
        p2->prev = NULL;
        delete p2;
    } 
}

void Stack::Push(int value)
{
    item* n = new item;

    n->value = value;

    if(stackPtr == NULL)
    {
        stackPtr = n;
        stackPtr->prev = NULL;
    }
    else
    {
        n->prev = stackPtr;
        stackPtr = n;
    }
}

void Stack::ReadItem(item* r)
{

    cout << "value: " << r->value << endl;
    cout << "------------------\n";
}

void Stack::Pop()
{
    if(stackPtr == NULL)
    {
        cout << "The stack is empty\n";
    }
    else
    {
        item* p = stackPtr;
        ReadItem(p);
        stackPtr = stackPtr->prev;
        p->prev = NULL;
        delete p;
    }
}
void Stack::Print()
{
    item* p = stackPtr;

    while(p != NULL)
    {
        ReadItem(p);
        p = p->prev;
    }   
}

Hashtable.h

#include <cstdlib>
#include <iostream>
#include <string>


#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 */

Hashtable.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;

}

的main.cpp

#include <iostream>
#include <cstdlib>
#include "Stack.h"
#include "HashTable.h"

using namespace std;

int main(int argc, char** argv) 
{

    Stack One;

    One.Push(3);
    One.Push(0);
    One.Push(4);
    One.Push(5);

    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);


    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);


    One.Print();
    cout << "\n\n\n";
    H1.PrintTable(); 


   /* 
    cout << "1. Push Stack Element" << endl;
    cout << "2. Pop Stack Element" << endl;
    cout << "3. Search Hash Table" << endl;
    */

    return 0;
}

0 个答案:

没有答案