源程序:
#include <iostream>
#include <stdlib.h>
const int MaxSize = 8;
using namespace std;
typedef struct
{
int stuno;
string stuname;
int stuage;
}TableElem;
TableElem stu[] = { {1001,"zhangsan",20},{1002,"lisi",19},{1003,"wangwu",21},
{1004,"zhaofeng",20},{1005,"ligang",21},{1006,"wangjun",19},
{1007,"chenguang",20},{1008,"zhangli",19} };
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]; //给elem[]数组初始化
}
seq.n = MaxSize;
int num, mm;
printf("请输入要查找的学号:");
cin >> num;
mm = searchsqtable(seq,num);
cout << "该学号的姓名为:" << seq.elem[mm].stuname << "
" <<"年龄为:"<< seq.elem[mm].stuage << endl;
return 1;
}
运行结果: