#include <iostream>
#include <windows.h>
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
struct node *next;
}no;
FILE *fp;
void goxy(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {
0 };
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void setcolor(unsigned short ForeColor,unsigned short BackGroundColor)
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle,ForeColor+BackGroundColor*0x10);
}
void f(no *n){
if((fp=fopen("C:\\a.txt","r"))==NULL)
{
cout <<"not find"<< endl;
exit(1);
}
int key,h;
for(int i=0;i<13;i++)
n[i].next=NULL;
while(!feof(fp))
{
fscanf(fp,"%d ",&key);
h=key%13;
no* q=new no;
q->data=key;
q->next=n[h].next;
n[h].next=q;
}
/*for(int i=0;i<13;i++){
cout<<i<<':';
no *p=n[i].next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}*/
fclose(fp);
}
void insert(){
no n[100];
f(n);
int key,h;
cout<<"please input the num that you want to insert"<<endl;
cin>>key;
h=key%13;
no* q=new no;
q->data=key;
q->next=n[h].next;
n[h].next=q;
if((fp=fopen("C:\\a.txt","w"))==NULL)
{
cout <<"not find"<< endl;
exit(1);
}
for(int i=0;i<13;i++){
no *p=n[i].next;
while(p!=NULL){
fprintf(fp,"%d ",p->data);
p=p->next;
}
fprintf(fp,"\n");
}
fclose(fp);
//while(1){
//if(fgets(str,1024,fp)==0) break;
//cout<<str;}
}
void search(){
no n[100];
f(n);
cout<<"please input the num that you want to search"<<endl;
int key,h;
cin>>key;
h=key%13;
no *p=&n[h];
no *q=n[h].next;
while(q){
if(q->data==key){
cout<<"succefully find "<<key<<endl;
break;
}
p=p->next;
q=q->next;
}
if(!q) cout<<"not find"<<key<<endl;
}
void delet(){
no n[100];
f(n);
cout<<"please input the num that you want to delete"<<endl;
int key,h;
cin>>key;
h=key%13;
no *p=&n[h];
no *q=n[h].next;
while(q){
if(q->data==key){
p->next=q->next;
cout<<"succefully delete"<<endl;
break;
}
p=p->next;
q=q->next;
}
if(!q) cout<<"not find,can not delete"<<key<<endl;
else{
if((fp=fopen("C:\\a.txt","w"))==NULL)
{
cout <<"not find"<< endl;
exit(1);
}
for(int i=0;i<13;i++){
no *p=n[i].next;
while(p!=NULL){
fprintf(fp,"%d ",p->data);
p=p->next;
}
fprintf(fp,"\n");
}
fclose(fp);
free(q);
}
}
void caidan()
{
goxy(30,10);
printf("***********************************************************");
goxy(30,12);
printf("欢迎进入外拉链表解决冲突的散列表建立,查找,删除的小程序");
goxy(30,14);
printf("**********************************************************");
goxy(30,16);
printf("请选择");
goxy(30,18);
printf("1.增加一个数 2.查找一个数 3.删除一个数");
goxy(30,20);
printf("4.退出");
goxy(30,22);
}
int main()
{
setcolor(7,2);
system("cls");
no n[100];
int key,h;
for(int i=0;i<13;i++)
n[i].next=NULL;
cout<<"please input some num to creat a list,0 respect the end"<<endl;
cin>>key;
while(key){
h=key%13;
no* q=new no;
q->data=key;
q->next=n[h].next;
n[h].next=q;
cin>>key;
}
if((fp=fopen("C:\\a.txt","w"))==NULL)
{
cout <<"not find"<< endl;
exit(1);
}
for(int i=0;i<13;i++){
no *p=n[i].next;
while(p!=NULL){
fprintf(fp,"%d ",p->data);
p=p->next;
}
fprintf(fp,"\n");
}
fclose(fp);
system("cls");
int i;
t:caidan();
//setcolor(7,2);
scanf("%d",&i);
switch (i){
case 1:system("cls");insert();system("pause");system("cls");goto t; break;
case 2:system("cls");search();system("pause");system("cls");goto t;break;
case 3:system("cls");delet();system("pause");system("cls");goto t;break;
case 4:exit(1);break;
default:system("cls");goto t;
}
return 0;
}
Insertion, deletion and search of hashed files
Guess you like
Origin blog.csdn.net/changbaishannefu/article/details/111354085
Recommended
Ranking