Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Double linked list is overwriting string items

Double linked list is overwriting string items

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structurescryptography
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I made a program which hashes string items in a table. The table has 5 index positions. When the table is full with string items, the linked list is overwriting the existing string items. This is my code from Table: Table::Table(int listSize) { size = listSize; list = new List[size]; } // hash function int Table::hash(string item) { unsigned int hval, g; char *str = &item[0]; /* Compute the hash value for the given string. */ hval = 0; while (*str != '\0') { hval <<= 4; hval += (unsigned int) *str++; g = hval & ((unsigned int) 0xf << (HASHWORDBITS - 4)); if (g != 0) { hval ^= g >> (HASHWORDBITS - 8); hval ^= g; } } return hval%size; } Node* Table::find(string item) { int hashValue = hash(item); return list[hashValue].find(item); } Node* Table::insert(string item) { int hashValue = hash(item); return list[hashValue].insert(item); } bool Table::isMember(string item) { int hashValue = hash(item); return (list[hashValue].find(item) != NULL); } void Table::remove(string item) { int hashValue = hash(item); list[hashValue].remove(item); } void Table::print() { for(int index = 0; index < size; index++) { cout << "Index: " << index << ": "; list[index].print(); cout << endl; } } The double linked list class: List::List() { head = NULL; } List::~List() { Node* previousNode; Node* node = head; while(node != NULL) { previousNode = node; node = node->next; delete previousNode; } } Node* List::insert(string item) { head = new Node(head, item); return head; } void List::remove(string item) { Node* previousNode; Node* node = head; while(node != NULL) { if(head->value == item) { // Link previous node to next node if(previousNode != NULL) previousNode->next = node->next; else head = node->next; delete node; break; } previousNode = node; node = node->next; } } Node* List::find(string item) { Node* previousNode; Node* node = head; while(node != NULL) { if(head->value == item) return node; previousNode = node; node = node->next; } return NULL; } void List::print() { Node* node = head; if(node != NULL) { cout << node->value << " "; node = node->next; } cout << endl; } Node class: Node::Node(Node* n, string s) { next = n; valu

    C 1 Reply Last reply
    0
    • Y Yustme

      Hi, I made a program which hashes string items in a table. The table has 5 index positions. When the table is full with string items, the linked list is overwriting the existing string items. This is my code from Table: Table::Table(int listSize) { size = listSize; list = new List[size]; } // hash function int Table::hash(string item) { unsigned int hval, g; char *str = &item[0]; /* Compute the hash value for the given string. */ hval = 0; while (*str != '\0') { hval <<= 4; hval += (unsigned int) *str++; g = hval & ((unsigned int) 0xf << (HASHWORDBITS - 4)); if (g != 0) { hval ^= g >> (HASHWORDBITS - 8); hval ^= g; } } return hval%size; } Node* Table::find(string item) { int hashValue = hash(item); return list[hashValue].find(item); } Node* Table::insert(string item) { int hashValue = hash(item); return list[hashValue].insert(item); } bool Table::isMember(string item) { int hashValue = hash(item); return (list[hashValue].find(item) != NULL); } void Table::remove(string item) { int hashValue = hash(item); list[hashValue].remove(item); } void Table::print() { for(int index = 0; index < size; index++) { cout << "Index: " << index << ": "; list[index].print(); cout << endl; } } The double linked list class: List::List() { head = NULL; } List::~List() { Node* previousNode; Node* node = head; while(node != NULL) { previousNode = node; node = node->next; delete previousNode; } } Node* List::insert(string item) { head = new Node(head, item); return head; } void List::remove(string item) { Node* previousNode; Node* node = head; while(node != NULL) { if(head->value == item) { // Link previous node to next node if(previousNode != NULL) previousNode->next = node->next; else head = node->next; delete node; break; } previousNode = node; node = node->next; } } Node* List::find(string item) { Node* previousNode; Node* node = head; while(node != NULL) { if(head->value == item) return node; previousNode = node; node = node->next; } return NULL; } void List::print() { Node* node = head; if(node != NULL) { cout << node->value << " "; node = node->next; } cout << endl; } Node class: Node::Node(Node* n, string s) { next = n; valu

      C Offline
      C Offline
      cp9876
      wrote on last edited by
      #2

      Have you tried to debug it? Perhaps single stepping through the insert function when it overwrites?


      Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

      Y 1 Reply Last reply
      0
      • C cp9876

        Have you tried to debug it? Perhaps single stepping through the insert function when it overwrites?


        Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        Hi cp9876, I've done debugging and searching the net.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups