⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.txt

📁 数据结构中的关于查找基本操作
💻 TXT
字号:
#include<stdio.h>
#define STACK_INIT_SIZE   90 //cun  chu kong jian chu shi fen liang
#define STACKINCREAMENT     10//cun chu kong jian fen pei zheng liang
#define ListLen    13
#define ERROR 0
#define OK 1
#define TURE 1
#define FALSE 0
#define OVERFLOWE  -2
typedef int  ElemType;
typedef  int status;
typedef struct {ElemType key;}ET;
typedef struct
{
  ET  *elem;
  int   length;
}SST;

status InitList_Sq(SST *ST)
{
   ST->elem=( ET*)malloc(ListLen*sizeof(ET));
   if(!ST->elem)
   exit(-1);
   ST->length=0;
   return   OK;
}

status  Insert(SST *ST)
 {
  int i,e;
  printf("Input the elemtype<=listsize:\n");
   for(i=1;i<ListLen;i++)
  {
  scanf("%d",&e);
  ST->elem[i].key=e;
  ST->length++;
  }
  return  OK;
 }

status  Search1_Sq(SST ST,ElemType key)
{
 int i;
 ST.elem[0].key=key;//设置哨兵
 for(i=ST.length;key!=ST.elem[i].key;i--);
 return i;
}

status search2_Sq(SST ST,ElemType key)
{
 int low=1,high=ST.length,mid;
 while(low<=high)
  {        mid=(low+high)/2;
   if(key==ST.elem[mid].key)
     return mid;
    else if(key<ST.elem[mid].key)
    high=mid-1;
    else
    low=mid+1;
  }
  return OK;
}
status print(SST ST)
{
  int i;
  for(i=1;i<ListLen;i++)
  {printf("%5d",ST.elem[i].key);}
  return OK;
}
void main()
{
  SST ST;
  int i,j,key1,key2;
  InitList_Sq(&ST);
  printf("input the number:\n ");
  Insert(&ST);
  printf("input the Number:\n");
  print(ST);
  printf("\n");
  key1=4;
  i=Search1_Sq(ST,key1);
  printf("Search the elem 4 location:\n");
  printf("%d\n",i);
  key2=5;
  j=search2_Sq(ST,key2);
   printf("Search the elem 5 location:\n");
  printf("%d\n",j);
}
















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -