查找 | 顺序查找

余生长醉 提交于 2019-12-05 03:57:46

#include <stdio.h>
#define MaxSize 8
typedef struct
{
  int stuno;
  char stuname[20];
}TableElem;

TableElem stu[]={{1001,"zhang"},{1009,"wang"},{2005,"sun"},{2008,"liu"},{3001,"zheng"},{3005,"lai"},{4003,"qin"},{4400,"ren"}};

typedef struct
{
  TableElem elem[MaxSize];
  int n;
}SqTable;

int searchsqtable(SqTable T, int key)//查找函数
{
  T.elem[0].stuno=key;
  int i=T.n;
  while(T.elem[i].stuno!=key)
    i--;
  return i;
}

int main()
{
  SqTable seq;
  for(int i=0;i<MaxSize;i++)
  {
    seq.elem[i]=stu[i];//用stu[8]8个人初始化 elem[8]数组
  }
  seq.n=MaxSize;
  int kk,mm;
  printf("请输入要查找的学号:");
  scanf("%d",&kk);
  mm=searchsqtable(seq,kk);//调用查找函数
  printf("此人在顺序表中的位置是:%d\n",mm+1);
  printf("%d号的姓名为:%s\n",mm+1,seq.elem[mm].stuname);
  return 1;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!