1.順序表的查找運算
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define ElemType int #define MAXSIZE 100 /*此處的宏定義常量表示線性表可能達到的最大長度*/ typedef struct { ElemType elem[MAXSIZE]; /*線性表占用的數組空間*/ int last; /*記錄線性表中最後一個元素在數組elem[ ]中的位置(下標值),空表置為-1*/ }SeqList; /*查找函數實現*/ int Locate(SeqList L, ElemType e) { int i=0; /*i為掃描計數器,初值為0,即從第一個元素開始比較*/ while ((i<=L.last)&&(L.elem[i]!=e)) i++; /*順序掃描表,直到找到值為key的元素, 或掃描到表尾而沒找到*/ if (i<=L.last)return(i+1); /*若找到值為e的元素,則返回其序號*/ else return(-1); /*若沒找到,則返回空序號*/ } void main() { SeqList l; int p,q,r; int i; printf("Please input the length of SeqList:"); scanf("%d",&r); l.last = r-1; printf("Please input the each number of SeqList:/n"); for(i=0; i<=l.last; i++) { scanf("%d",&l.elem[i]); } printf("please input the number which you want to find:/n"); scanf("%d",&q); p=Locate(l,q); if(p == -1) printf("there is no number in the SeqList!/n"); else printf("The number which you want to find is at the place of :%d/n",p); }
最後更新:2017-04-02 03:42:38
上一篇:
一步一步SEO 之 網站內容SEO
下一篇:
javascript&JS代碼防止複製、另存為的代碼
機器學習:如何在安卓上集成TensorFlow
《Docker容器:利用Kubernetes、Flannel、Cockpit和Atomic構建和部署》——2.3 小結
Android SDK 更新不成功的解決方法
IBM WebSphere Application Server V6.1 Fix Pack 37於2011.04.04發布
BAT shell 隨記
《計算機存儲與外設》----1.4 Cache設計中要考慮的因素
qq安全原理
高曉鬆談管理:自嘲總被員工管
org.springframework.core.enums類圖
PostgreSQL 為什麼不要濫用unlogged table & hash index