Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:38:53+00:00 2026-06-18T00:38:53+00:00

I am trying to make a simple linked list from scratch that is used

  • 0

I am trying to make a simple linked list from scratch that is used by another class.
The error seems to be that sometimes the Head is not set to NULL.
I say sometimes before it does not seg fault all the time. The times that it does not seg fault, it goes to the else statement. Note I am only adding 1 string. Maybe you guys can spot something I am not seeing, cheers!

LinkedList.h:

#include <string>
#include "Link.h"

using namespace std;

class LinkedList {
  Link *head;

public:
  LinkedList();
  void addFront(string key);
  void printList();
  //void addBack(string *);     

};

LinkedList.cpp:

#include <cstring>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "LinkedList.h"

using namespace std;

LinkedList::LinkedList() {
   head = NULL;
}

void LinkedList::addFront(string key) {
  //creates the new list segment                                                
  Link *l = new Link(key);

  cout << "Made new Link " << key << endl;
  // if the list is empty                                                 
  if (head == NULL){
    cout << "Going to set Head " << key << endl;
    head = l;
    cout << "Set Head to new link " << key << endl;
 }                                            
  else {             
     cout << "Else statement " << key << endl;                  
    l->setNext(head);

    head = l;
  }

}

void LinkedList::printList() {
 //Check if list is empty
  if(head == NULL)
   cout << "NULL" << endl;
  else {
   Link *l = head; 

   for(;l != NULL; l=l->getNext())
     cout << l->getValue() << endl;
  }


}

// void LinkedList::addBack(string *f) {
//   Link *l = new Link(f);

// }

Link.h

#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;

class Link {
string key;
Link *next;

public:
    Link(string key);

    void setValue(char);
    void setNext(Link *next);
    string getValue();
    Link *getNext();
    void printList();

};

Link.cpp

#include <cstdlib>
#include <string>
#include <iostream>
#include "Link.h"

using namespace std;

Link::Link(string key) {
    this->key = key;
next = NULL;

}

void Link::setNext(Link *l) {
cout << "setting new link "<<endl;
next = l;

cout<< "New link was set" << endl;
}

string Link::getValue() {
return key;

}

Link *Link::getNext() {
return next;
}

Hash.h

#include <iostream>
#include <cstring>
#include <string>
#include "LinkedList.h"

using namespace std;

class Hash{
    //100 slot array for hash function
    LinkedList *hashFN[100];

    //preset prime number                                                               
  int prime = 101;
    int key;
  unsigned int location;

public:
    //Note: Both the key & values are the same 
    void insert(string key, string value);
    // void deleteItem(int key);
    // char* find(int key);


};

Hash.cpp:

#include <iostream>
#include <cstring>
#include <string>
#include "Hash.h"

using namespace std;

void Hash::insert(string k, string v){
    //Get Hash for argv[2] aka value                                                  
  size_t key = std::hash<string>()(k);
   unsigned int location;

//check 1                                                                         
  cout << "Hash: " << key << endl;

  //Find location
  location = key % prime;

  //check 2                                                                         
  cout << "Mod 101 Hash: " << location << endl;

    hashFN[location]->addFront(k);
    cout << "Success!" << endl;

}

// void Hash::deleteItem(int key){
//  return;

// }

// char* Hash::find(int key){
//  return;

// }

main.cpp

#include <iostream>
#include <functional>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "Hash.h"                                                                   

using namespace std;

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

  Hash HashTable;                                                                   


  string Insert = string("insert");
  string Delete = string("delete");
  string Find   = string("find");
  string Argv(argv[2]);  //Makes the argv[2] into string type

  // check for Request & string parameters                                            
  if(argc != 3) {
    cout << "Run program with 2 parameters. [Lower Case]" << endl;
    cout << "[1] insert, find, or delete" << endl;
    cout << "[2] string" << endl;
    exit(1);
  }

  //Check for "insert"                                                                
  if(strcmp(argv[1], "insert") == 0) {


  HashTable.insert(Argv, Argv);                                                

  }

  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:38:54+00:00Added an answer on June 18, 2026 at 12:38 am

    Your immediate problem: Your linked list array in your hash table is a pointer array. Declared as:

    LinkedList *hashFN[100];
    

    You never seem to allocate any of these objects. so you just have a pointer array of 100 indeterminate pointers to garbage. Either allocate them, or better still, just instance them directly:

    LinkedList hashFn[100];
    

    This will require you change the references to them, such as

    hashFN[location]->addFront(k);
    

    Becomes this:

    hashFN[location].addFront(k);
    

    Next, you’re using non-const member variable prime as the modulus of the hash function. Since this is hard-coded to 101, your table needs to be the same size. (and in fact, I’d lose the member variable entirely and just a static const in your Hash.cpp file as far as that goes). But apart from that, the issue still remains. Your table is one slot too small. Your modulo can be from 0..100, which means you need a table [101] in size to address all those slots. Remember, arrays in C/C++ are [0...(n-1)] addressed for an array of size n

    So at least declare your table like so:

    LinkedList hashFN[101];
    

    Last thing I’m posting. Your linked list leaks like the Titanic. You need to clean up all those nodes when the list is destroyed. Declare a destructor in your class:

    virtual ~LinkedList();
    

    And implement it like this in your .cpp file:

    LinkedList::LinkedList()
    {
        while (head)
        {
            Link *victim = head;
            head = head->getNext();
            delete victim;
        }
    }
    

    OK. I lied. One more thing, Until you are comfortable in both reading and understanding the Rule of Three, you should make your class non-copyable. Put the following in a private section of your LinkedList class declaration:

    class LinkedList
    {
       ... other code...
    
    private:
        LinkedList(const LinkedList&);
        LinkedList& operator =(const LinkedList&);
    };
    

    This will hide the things that can cause you major issues until you are ready to implement them properly. If you find yourself with an “error of something to the effect of “cannot access private member LinkedList(const LinkedList&)” then you’re trying to make a copy, and without proper protection of your head head pointer thats a no no. read the Rule of Three.

    The rest I leave to you.

    Note: There are ways of doing a LOT of this easier using the standard library (std::vector<>, std::unordered_map<>, etc.) In fact, std::unordered_map<> is a drop-in replacement for all of this. But if you’re just interested in working with the language to get familiar again, its not bad to get down to the bare metal for awhile. Just focus on relearning, then learn to use all that beautiful code the standard library has to offer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make simple website with content background combined from 3 images: top
im trying to make a simple search on my database from the index action
I am trying to make some simple libraries that require database access. What I
I'm trying to make a list view in a page that is liked form
Trying to make simple minesweeper game in python, but have one problem. I have
I am trying to make simple notepad application for learning android development. So, to
I'm trying to make simple script using jquery $post function to pass data to
I am fairly new to iOS development and trying make a simple app which
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At
Trying to make a simple program to catalogue books. Something like this, for example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.