정적 순서 찾기

3857 단어 정태
#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(" :
"); scanf("%d",&list.length); printf(" %d :
",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(" :
"); scanf("%d",&data); position=FindPosition(list,data); if(position==0) { printf(" !
"); } else{ printf(" %d !
",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(" :
"); scanf("%d",&list.length); printf(" %d :
",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(" :
"); scanf("%d",&data); position=FindPosition(list,data); if(position==0) { printf(" !
"); } else{ printf(" %d !
",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; }

좋은 웹페이지 즐겨찾기