#include <stdio.h>
#include <stdlib.h>
#define DATATYPE_DIC_KEY int
#define DATATYPE_DIC_VALUE int
typedef struct dicelement
{
DATATYPE_DIC_KEY key;
DATATYPE_DIC_VALUE value;
} dicnode;
typedef struct dictionary
{
int max_length;
int length;
dicnode *list;
} *pdictionary;
pdictionary createNewDictionary(int max)
{
pdictionary newdic = (pdictionary)malloc(sizeof(struct dictionary));
if(newdic != NULL)
{
newdic->list = (dicnode *)malloc(sizeof(dicnode)*max);
if(newdic->list != NULL)
{
newdic->max_length = max;
newdic->length = 0;
}
else
printf("Memory overflow!\n");
}
else
printf("Memory overflow!\n");
return newdic;
}
int addinfo(pdictionary dic, dicnode node)
{
if(dic->length < dic->max_length)
{
dic->list[dic->length].key = node.key;
dic->list[dic->length].value = node.value;
dic->length++;
}
else
return 0;
return 1;
}
int search_seq(pdictionary dic, DATATYPE_DIC_KEY target)
{
int i = 0;
for(i = 0; i < dic->length; i++)
{
if(dic->list[i].key == target)
return i;
}
return -1;
}
int search_bin(pdictionary dic, DATATYPE_DIC_KEY target)
{
int low,high,mid;
low=0;high=dic->length-1;
while(low<=high)
{
mid=(low+high)/2;
if(target==dic->list[mid].key)
return mid;
else
{
if(target<dic->list[mid].key)
high=mid-1;
else
low=mid+1;
}
}
return -1;
}
int sort_dic(pdictionary dic)
{
int i, j;
DATATYPE_DIC_KEY tmp_key;
DATATYPE_DIC_VALUE tmp_value;
for(i = dic->length-1; i > 0; i--)
{
for(j = 0; j < i; j++)
{
if(dic->list[j].key > dic->list[j+1].key)
{
tmp_key = dic->list[j].key;
tmp_value = dic->list[j].value;
dic->list[j].key = dic->list[j+1].key;
dic->list[j].value = dic->list[j+1].value;
dic->list[j+1].key = tmp_key;
dic->list[j+1].value = tmp_value;
}
}
}
return 0;
}
int deleteinfo(pdictionary dic, DATATYPE_DIC_KEY target)
{
int x=search_seq(dic,target);
int q=0;
if(x==-1){
printf("error: the value can't find!!!!!!!");
}
else{
for(q=x;q<dic->length-1;q++)
{
dic->list[q].key=dic->list[q+1].key;
dic->list[q].value=dic->list[q+1].value;
}
dic->length-=1;
printf("\ndelete success!");
}
}
void print_dic(pdictionary dic)
{
int i;
printf("\nposition");
for(i = 0; i<dic->length; i++)
{
printf("%5d ", i);
}
printf("\nkey\t");
for(i = 0; i<dic->length; i++)
{
printf("%5d ", dic->list[i].key);
}
printf("\nvalue\t");
for(i = 0; i<dic->length; i++)
{
printf("%5d ", dic->list[i].value);
}
}
int isEmptyDic(pdictionary dic)
{
if(dic == NULL || dic->length == 0)
return 1;
return 0;
}
int main()
{
int in,c;
pdictionary dic = NULL;
dicnode node;
while(1)
{
printf("\n");
printf("\t\t\t ==============================\n");
printf("\t\t\t ============Dictionary Menu ==========\n");
printf("\t\t\t ==============================\n");
printf("\t\t\t|| (1)create dic ||\n");
printf("\t\t\t|| (2)insert ele ||\n");
printf("\t\t\t|| (3)delete ele ||\n");
printf("\t\t\t|| (4)sequence search ||\n");
printf("\t\t\t|| (5)binary search ||\n");
printf("\t\t\t|| (6)sort ||\n");
printf("\t\t\t|| (7)print ||\n");
printf("\t\t\t|| (0)exit ||\n");
printf("\t\t\t ==============================\n");
printf(">>>Please enter command code : ");
scanf("%d", &in);
switch(in)
{
case 1:
printf("\nPlease input the length dic:");
scanf("%d", &in);
dic = createNewDictionary(in);
printf("\nCreate Success!!!");
break;
case 2:
printf("\nkey:");
scanf("%d", &node.key);
printf("\nvalue:");
scanf("%d", &node.value);
addinfo(dic, node);
printf("\ninsert success!");
break;
case 3:
printf("\nThe delete key is:");
scanf("%d", &in);
deleteinfo(dic, in);
break;
case 4:
printf("\nThe search key is:");
scanf("%d", &in);
in = search_seq(dic, in);
if(in > -1)
{
printf("\nsearch success!");
printf("\nthe search result: %d", in);
}
else
printf("\nThe key not founded!");
break;
case 5:
printf("\nThe search key is:");
scanf("%d",&in);
c=search_bin(dic,in);
if(c==-1)
printf("not found!!!!!");
else
printf("\nThe position of key is: %d",c);
break;
case 6:
sort_dic(dic);
printf("\nThe sort result is:");
print_dic(dic);
printf("\nThe search key is:");
scanf("%d", &in);
in = search_bin(dic, in);
if(in > -1)
{
printf("\nsearch success");
printf("the search result: %d", in);
}
else
printf("\nThe key not founded!");
break;
case 7:
printf("\nThe dic is:\n");
print_dic(dic);
break;
case 0:
return 0;
break;
default:
printf("illegal command!\n");
}
}
}
qq:1351006594