哈希表打印略有乱序

时间:2018-04-17 01:23:17

标签: c

我正在尝试在c中创建一个接受整数的基本哈希表。我正试图按顺序打印数字1-10。当我输入整数时,一切都编译好了,但是当我使用我制作的printlist函数时,它会打印10,1,2,3,4,5,6,7,8,9。我已经尝试了几个小时,但我无法弄清楚是什么导致它。

#include <stdlib.h>
#include <stdio.h>


typedef struct node {
    struct node* next;
    int data;
}node;




void printlist(node *ht[]);
node* insert_node(node *ht[]);

int main() {
    int index = 0;
    int input;
    int option;
    node* ht[10] = { NULL };



    do {
        printf("Press 1 to input a node\n");
        printf("Press 2 to print the list\n");
        printf("Press 3 to exit:\n");
        printf("Select Option: ");
        scanf("%i", &option);

        if (option == 1) {
            insert_node(ht);
            continue;
        }
        if (option == 2) {
            printlist(ht);
        }
       } while (option != 3);

}


void printlist(node* ht[]) {
    node *prvptr;
    node *curptr;
    int j = 9;
    int index = 0;



    for (index = 0; j >= index; index++) {
        curptr = ht[index];
        curptr = curptr->next;
        while (curptr != NULL) {
            printf("%i\n", curptr->data);
            curptr = curptr->next;
            break;
        }
    }
}




node* insert_node(node* ht[]) {
    node *newptr;
    node *curptr;
    node *prvptr;
    node *head;
    int count = 0;
    int choice;
    int index = 0;
    int input = 0;


    printf("Input negative 1 to cancel\n");
    printf("What number would you like to add to the list?: ");
    scanf("%i", &input);
    index = input % 10;

    while (input != -1) {
        newptr = (node*)malloc(sizeof(node)); // creates a node for the input data
        newptr->data = input;
        newptr->next = NULL;

        if (ht[index] == NULL) { // if there is no head
            ht[index] = (node*)malloc(sizeof(node));
            ht[index]->data = NULL;
            ht[index]->next = newptr;
            count++;
        }
        else {
            curptr = ht[index];
            while (curptr->next != NULL) {
                prvptr = curptr;
                curptr = curptr->next;
                if (curptr->data > input) { // if in-between two nodes
                    prvptr->next = newptr;
                    newptr->next = curptr;
                    break;
                }

                else if (curptr->next == NULL) { // if at the end of the list
                    curptr->next = newptr;
                    break;
                }

            }
        }
        printf("Input negative 1 to cancel\n");
        printf("What number would you like to add to the list?: ");
        scanf("%i", &input);
        index = input % 10;
    }
    return ht;
}

2 个答案:

答案 0 :(得分:0)

您的index计算结果为index = input % 10;。数字10,模10,是0,所以它在你的表的第一个索引(索引0)中,在1-9之前(这将在索引1到9中)。由于您的打印机按照索引0到9的顺序打印存储桶,因此首先输出10。

哈希表的性质是有限的排序(大多数语言和哈希表库在哈希表的迭代顺序上完全不提供保证);我不会因为10出现在输出中的第一个而感到困扰。

答案 1 :(得分:0)

就像@ShadowRanger提到的那样,10先出现并没有错。

相反,我在您的<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ProjectData.ProjectData" android:versionCode="1" android:versionName="1.0"> <!--suppress UsesMinSdkAttributes--> <uses-sdk android:minSdkVersion="21" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:label="@string/app_name" android:name="android.app.Application" android:icon="@drawable/dataicon" android:debuggable="true"> <receiver android:name=".NetworkReceiver"> android:exported=true <intent-filter><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter></receiver> <receiver android:name="md5f626beda2f87a7aeee80e4f58cdf3b35.NetworkReceiver" /> <activity android:icon="@drawable/dataicon" android:label="ProjectData" android:name="md5f626beda2f87a7aeee80e4f58cdf3b35.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="2147483647" android:authorities="ProjectData.ProjectData.mono.MonoRuntimeProvider.__mono_init__" /> <!--suppress ExportedReceiver--> <receiver android:name="mono.android.Seppuku"> <intent-filter> <action android:name="mono.android.intent.action.SEPPUKU" /> <category android:name="mono.android.intent.category.SEPPUKU.ProjectData.ProjectData" /> </intent-filter> </receiver> </application> </manifest> 代码中看到了另一个错误:

printlist

while (curptr != NULL) { printf("%i\n", curptr->data); curptr = curptr->next; break; } 语句不应该存在,您最终只会在每个哈希值中打印一个元素。