静态顺序查找
#include <stdio.h> #include <stdlib.h> #define OK 1 #define error 0 typedef int Status; typedef int KeyType; typedef struct{ KeyType * elem; int length; }SequenceList; Status FindPosition(SequenceList list,int data) { int i; list.elem[0]=data; for(i=list.length;!(list.elem[i]==data);i--); return i; } int main(int argc, char *argv[]) { SequenceList list; int i,data,position; list.elem=(KeyType *)malloc(sizeof(KeyType)); printf("请输入表的长度:\n"); scanf("%d",&list.length); printf("请输入%d个数据:\n",list.length); for(i=1;i<=list.length;i++) { scanf("%d",&list.elem[i]); } for(i=1;i<=list.length;i++) { printf("%d ",list.elem[i]); } printf("请输入要查找的数据:\n"); scanf("%d",&data); position=FindPosition(list,data); if(position==0) { printf("要查找的数据不存在!\n"); } else{ printf("要查找的数据在%d个位置!\n",position); } } ***************************** #include<stdio.h> int Search_seq(int R[],int length,int key) { int i; R[0]=key; for(i=length-1;R[i]!=key;--i); return i; } int main() { int R[11]={0,13,29,18,27,10,15,34,33,2,1}; int k=Search_seq(R,11,15); printf("%d ",k); } ****************************888 #include <stdio.h> #include <stdlib.h> typedef int KeyType; typedef struct{ KeyType * elem; int length; }SequenceList; int FindPosition(SequenceList list,int data) { int i; list.elem[0]=data; for(i=list.length;!(list.elem[i]==data);i--); return i; } int main(int argc, char *argv[]) { SequenceList list; int i,data,position; list.elem=(KeyType *)malloc(sizeof(KeyType)); printf("请输入表的长度:\n"); scanf("%d",&list.length); printf("请输入%d个数据:\n",list.length); for(i=1;i<=list.length;i++) { scanf("%d",&list.elem[i]); } for(i=1;i<=list.length;i++) { printf("%d",list.elem[i]); } printf("请输入要查找的数据:\n"); scanf("%d",&data); position=FindPosition(list,data); if(position==0) { printf("要查找的数据不存在!\n"); } else{ printf("要查找的数据在%d个位置!\n",position); } } **********************88 #include <iostream> using namespace std; typedef struct { int key; }elemtype; typedef int Status; #define OK 1 #define error 0 typedef struct { elemtype *base; int length; }Thing; Status print(elemtype x) { cout<<x.key<<endl; } Status Destory(Thing &head) { head.length=NULL; free(head.base); return OK; } Status create(Thing &head,int n) { int i=0; head.length=n; head.base=(elemtype*)malloc((n+1)*sizeof(elemtype)); head.base[i].key=n; i++; while(i<=head.length) { cin>>head.base[i].key; i++; } return OK; } Status search(Thing head,elemtype key) { head.base[0].key=key.key; for(head.length;head.length>0;head.length--) if(head.base[head.length].key==key.key)return head.length; return head.length; } Status tarevster(Thing head,Status (*visit)(elemtype x)) { int i=1; while(i<=head.length) { visit(head.base[i]); i++; } return OK; } int main(int argc, char *argv[]) { int n; Thing head; elemtype key; cin>>n; create(head,n); cin>>key.key; cout<<search(head,key)<<" ******************"<<endl; tarevster(head,print); Destory(head); return 0; }