C代码中的哈希表! ! ! ! [英] Hash table in C code! ! ! !

查看:82
本文介绍了C代码中的哈希表! ! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

typedef struct node_type{
	int data;
	struct node_type *link;
}node;

typedef node *list;

void add_to_list(int,int);
void show();

list bucket[10];

main()
{
	int number;
	list tet;
	char ch;
	printf("Enter y if u want to input:\n");
	tet=bucket[0];
	scanf("%c",&ch);
	while(ch=='y'){
		printf("Enter numbers\n");
		scanf("%d\n",&number);
		add_to_list(number/10,number);
		scanf("%c",&ch);
	}
	show();
}


void  add_to_list(int index,int cut){
	list q,r,temp;
	int i;
	if (bucket[index] == NULL){
		q = bucket[index] =(list)malloc(sizeof (node));
		q->data=cut;
		q->link=NULL;
	}
	else{
		while(q->link!=NULL) q=q->link;
		if(q->link==NULL){
			temp=(list)malloc(sizeof(node));
			temp->data=cut;
			temp->link=NULL;
			q->link=temp;
		}
	}
}
/*To show every element in the table*/

void show(){

	int i;
	list pemp;
	for(i=0;i<10;i++){
		pemp= (list)malloc(sizeof(node));
		pemp=bucket[i];
		while(pemp!=NULL){
			printf("%d ",pemp->data);
			pemp=pemp->link;
		}
	}
}





我尝试了什么:



任何人都可以建议我在这里可以做些什么?问题是,当我给出相同的输入两次或两次以上时,程序崩溃了。但是当我得到两个不同的值或甚至更多的值时,该程序运行良好......



What I have tried:

Could anyone please suggest me what can I do here? The problem is when I gave same input for twice or, more than twice, the program crashed. But when I got two distinct value or even more value the program worked well......

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于C代码中的哈希表! ! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆