search_seq_1.cpp

来自「《数据结构》所有相关程序的算法。有图、数组以及二叉数的问题。附有程序及结果。」· C++ 代码 · 共 44 行

CPP
44
字号
//Search_Seq.cpp
//This function is to find the element in SSTbale by sequence search
# include <malloc.h>
# include <iostream.h>
# include <conio.h>

# define MAX_LENGTH 100
typedef int KeyType;

typedef struct			//define structure SSTable
{   KeyType *elem;
    int length;
}SSTable;

int Search_Seq(SSTable ST,KeyType key)	//Search_Seq function
{  int i;
   ST.elem[0]=key;
   for(i=ST.length;!(ST.elem[i]==key);--i)
      ;
   return (i);				//if not find,return i=0
}

void main()  			   	//main function
{  int i,key;
   SSTable ST;
   ST.elem=(KeyType *)malloc(sizeof(KeyType));
   cout<<endl<<endl<<"Search_Seq.cpp";
   cout<<endl<<"==============";
   cout<<endl<<endl<<"Please input the length of array (eg,5) :";
   cin>>ST.length;
   for(i=1;i<=ST.length;++i)
   {  cout<<"Please input the "<<i<<"th element (eg,58) :   ";
      cin>>ST.elem[i];
   }
   cout<<endl<<"The SSTable ST is : ";
   for(i=1;i<=ST.length;i++)
      cout<<ST.elem[i]<<"  ";		//output ST.elem[]
   cout<<endl<<"Please input the data to find : ";
   cin >>key;				//input the element to find
   cout<<"The location of "<<key<<" is (0 for none): ";
   cout<<Search_Seq(ST,key);		//call Search_Seq()
   cout<<endl<<endl<<"...OK!...";
   getch();
} //main() end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?