Hash table on top of linked list

So I implemented a linked list and now I want to place a hash table on top. Am I going in the right direction?

I purposely avoided templates just to make this simpler for now. And some things are hard coded as well just to reduce how big my main is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>

using namespace std;

class List{

private:

    struct Node{
    int data;
    Node * next;
    };
    Node * head;
    Node * current;

public:
    List();
    ~List();
    void push_front(int x);
    void push_back(int x);
    int pop_front();
    int pop_back();
    void print();

};

List::List()
{
    head = nullptr;
}

List::~List()
{
    Node * temp;
    current = head;
    while(current != nullptr)
    {
        temp = current;
        current = current->next;
        delete temp;
    }

}

void List::push_front(int x)
{
    Node *n = new Node;
    n->data = x;

    if(head == nullptr)
    {
        head = n;
    }
    else
    {
        current = head;
        n->next = current;
        head = n;
    }
}

void List::push_back(int x)
{
    Node *n = new Node;
    n->data = x;
    n->next = nullptr;

    if(head == nullptr)
    {
        head = n;
    }
    else
    {
        current = head;
        while(current->next != nullptr)
        {
            current = current->next;
        }
        current->next = n;
    }

}

void List::print()
{
    current = head;

    if(head == nullptr)
    {
        cout << "list is empty!" << endl;
    }
    else
    {
        while(current->next!= nullptr)
        {
            cout << current->data << " ";
            current = current->next;
        }
        cout << current->data << endl;
    }

}

int List::pop_back()
{
    Node * temp;
    current = head;
    int data;

    if(head == nullptr)
    {
        cout << "Error" << endl;
        return 0;
    }

    else{
        while(current->next != nullptr)
        {
            temp = current;
            current = current->next;
        }
        if(head == current)
        {
            head = nullptr;
            data = current->data;
            delete current;
            return data;
        }

        temp->next = nullptr;
        data = current->data;
        delete current;
        return data;
    }
}

int List::pop_front()
{
    current = head;
    int data;


    if(head == nullptr)
    {
        cout << "list is empty pf" << endl;
        return 0;
    }
    else
    {
     head = head->next;
     data = current->data;
     delete current;
     return data;
    }

}

class HashT{

private:
    List object[10];
    int hash(int x)
    {
        return x %10;
    }

public:
    HashT();
    void insert(int x);

};

HashT::HashT()
{
}

void HashT::insert(int x)
{
    object[hash(x)].push_back(x);
}

int main()
{
    HashT one;
    one.insert(5);
}
Last edited on
Node::next remains uninitialized after lines 52, 70, 79.

There is no need for current to be a member of class List. Just use local variables where you need it.

Unless you want to add a tail pointer, I'd get rid of push_back() and pop_back() because they are hugely expensive if the list is large.

You will probably need to add find() and remove() methods to class List to find a Node by value and delete a node by value.


Thanks for the feedback dhayden. I coded this pretty quick and guess I missed a few things. You're also right on those methods getting expensive. I just didn't think about adding the new integers to the top of the hash table.

ASIDE

Does Node * current; need to be a class member? Given the way it's used it might be better as a local variable.

(And some of you variables could be defined in tighter scope!)

Andy
Topic archived. No new replies allowed.