簡體   English   中英

使用冒泡排序C ++的哈希表排序

[英]Hashtable sorting using bubble sort c++

我在對哈希表進行排序時遇到問題。 我有比較數字,字母和冒泡排序算法的方法。 排序輸出不是我想要的,因為它按插入順序(首先插入-先打印)打印哈希表,而我想要的是按鍵升序或按值升序排序。 鍵是整數,值是字符串。 這是代碼:

#include<iostream>
#include<string>

using namespace std;
const int size = 100;

class Binding
{
    public:
        int key;
        string value;
        Binding *next;

        Binding(int key, string value)
        {
            this->key = key;
            this->value = value;
            this->next = NULL;
        }
};

class HashTable
{
    private:
        Binding** tarray;

    public:
        HashTable()
        {
            array = new Binding*[size];
            for(int i = 0; i < size; i++)
            {                
                array[i] = NULL;
            }
        }

        int insert(int key, string value)           
        {
            int hash = (key % size);
            Binding *record = array[hash];
            Binding *previous = NULL;

            if(record != NULL)
            {
                previous = record;
                record = record->next;
            }
            else if(record == NULL)
            {
                record = new Binding(key, value);

                if (previous == NULL)
                {
                    array[hash] = record;
                }
                else
                {
                    previous->next = record;
                }
            }
            else
            {
                record->value = value;
            }
        }

        int compareLetters(const void *a, const void *b)
        {
             Binding *A = (Binding*)a;
             Binding* B = (Binding*)b;
             int compare = strcmp(A->letter, B->letter);

             if(compare == 0)
                  return 0;
             else if(compare > 0)
                 return 1;
             else if(compare < 0)
                 return -1;
        }

        int compareNumbers(const void *a, const void *b)
        {
            Binding *A = (Binding*)a;
            Binding *B = (Binding*)b;
            if(A->key > B->key)
                return 1;
            else if(A->key < B->key)
                return -1;
            else 
                return 0;
        }

        void bubble_sort()
        {
            Binding *temp;
            for(int i=1; i<size; i++)
            {
                for(int j=0; j<size - i; j++)
                {
                    if(compareNumbers(&array[j], &array[j+1]) == 1)
                    {
                        temp = array[j];
                        array[j] = array[j+1];
                        array[j+1] = temp;
                    }
                }   
            }
        } 

您獲取Binding指針的地址,並在此行compareNumbers它們傳遞給compareNumbers函數:

                if(compareNumbers(&array[j], &array[j+1]) == 1)

這是錯誤的,因為compareNumbers函數除了可轉換為Binding *作為參數的值外,而不是現在傳遞它們時的Binding **

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM