编程语言:c++

截图展示:

 

 

代码如下:

  1 #include <iostream>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #define M 13
  5 using namespace std;
  6 struct hash
  7 {
  8     int value;
  9     hash *next;
 10 };
 11 hash* map[M];
 12 int search(int k)
 13 {
 14     int x=k%M;
 15     hash *p;
 16     p=map[x];
 17     while(NULL!=p)
 18     {
 19         if(p->value==k)
 20         return x;
 21         p=p->next;
 22     }
 23     return -1;
 24 }
 25 void insert_hash(int k)
 26 {
 27     hash *p,*q;
 28     int x=k%M;
 29     p=map[x];
 30     if(-1==search(k))
 31     {
 32         q=(hash*)malloc(sizeof(hash));
 33         q->value=k;
 34         map[x]=q;
 35         q->next=p;
 36     }
 37 }
 38 void delt(int k)
 39 {   int x=k%M;
 40     hash *p=map[x];
 41     if(-1==search(k))
 42     cout<<"表中没有"<<k<<endl;
 43     else
 44     {
 45         while(p->next!=NULL)
 46         {
 47             if(p->next->value==k)
 48             {
 49                 p->next=p->next->next;
 50             }
 51         }
 52         if(p->value==k)
 53         {
 54             p->next=NULL;
 55         }
 56         cout<<"删除成功"<<endl;
 57 
 58     }
 59 }
 60 void chu()
 61 {   for(int i=0;i<M;i++)
 62     map[i]=NULL;
 63     cout<<"输入元素的个数为"<<endl;
 64     int n,x;
 65     cin>>n;
 66     cout<<"输入值"<<endl;
 67     for(int i=0;i<n;i++)
 68     {
 69         cin>>x;
 70         insert_hash(x);
 71     }
 72 }
 73 void show()
 74 {
 75     cout<<"散列表元素的值分别为:"<<endl;
 76     hash *p;
 77     for(int i=0;i<M;i++)
 78     {
 79         p=map[i];
 80         cout<<"map["<<i<<"]"<<"";
 81         while(NULL!=p)
 82         {
 83             cout<<p->value<<" ";
 84             p=p->next;
 85         }
 86         cout<<endl;
 87     }
 88 }
 89 void menu()
 90 {
 91   printf("%c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
 92   printf("%c                                                                       %c\n",4,4);
 93   printf("%c             1、初始                 2、插入                           %c\n",4,4);
 94   printf("%c                                                                       %c\n",4,4);
 95   printf("%c             3、删除                 4、查找                           %c\n",4,4);
 96   printf("%c                                                                       %c\n",4,4);
 97   printf("%c             5、显示                 6、退出                           %c\n",4,4);
 98   printf("%c                                                                       %c\n",4,4);
 99   printf("%c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
100   cout<<"----直接从键盘输入选项"<<endl;
101   cout<<endl;
102 }
103 int main()
104 {
105   int n,x;
106   while(1)
107   {   menu();
108       cin>>n;
109       if(n==1)
110       {
111           system("cls");
112           chu();
113           cout<<"---按任意键返回"<<endl;
114           getchar();
115           getchar();
116           system("cls");
117           continue;
118       }
119       else if(n==2)
120       {
121           cout<<"输入要插入的值:"<<endl;
122           cin>>x;
123           insert_hash(x);
124            cout<<"---按任意键返回"<<endl;
125           getchar();
126           getchar();
127           system("cls");
128           continue;
129       }
130       else if(n==3)
131       {
132           cout<<"输入要删除的值:"<<endl;
133           cin>>x;
134           insert_hash(x);
135            cout<<"---按任意键返回"<<endl;
136           getchar();
137           getchar();
138           system("cls");
139           continue;
140       }
141       else if(n==4)
142       {  cout<<"输入要查找的值:"<<endl;
143           cin>>x;
144           if(search(x)==-1)
145           cout<<"没找到"<<endl;
146           else
147           cout<<"map["<<search(x)<<"]:"<<x<<endl;
148            cout<<"---按任意键返回"<<endl;
149           getchar();
150           getchar();
151           system("cls");
152           continue;
153 
154       }
155       else if(n==5)
156       {   system("cls");
157           show();
158           cout<<"---按任意键返回"<<endl;
159           getchar();
160           getchar();
161           system("cls");
162           continue;
163       }
164       else if(n==6)
165       {
166           break;
167       }
168       else
169       {
170           cout<<"输入错误"<<endl;
171           cout<<"---按任意键返回"<<endl;
172           getchar();
173           getchar();
174           system("cls");
175           continue;
176       }
177   }
178 
179 
180 }

 

 posted on 2017-08-21 19:58  几缕清风依旧  阅读(249)  评论(0编辑  收藏  举报