查找的普通应用实例

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "SeqList.h"  //调用库函数

#define SIZE 20
/*  打印数组 */
void print_array(int a[], int len)
{
    int i = 0;
    
    for(i=0; i<len; i++)
    {
        printf("%d, ", a[i]);
    }
    
    printf(" ");
}
//静态查找
int static_search(int a[], int len, int key)
{
    int ret = -1;
    int i = 0;
    
    for(i=0; i<len; i++)
    {
        if( a[i] == key )
        {
            ret = i;
            break;
        }
    }
    
    return ret;
}
//打印链表
void print_list(SeqList* list)
{
    int i = 0;
    
    for(i=0; i<SeqList_Length(list); i++)
    {
        printf("%d, ", (int)SeqList_Get(list, i));
    }
    
    printf(" ");
}
//动态查找
int dynamic_search(SeqList* list, int key)
{
    int ret = -1;
    int i = 0;
    
    for(i=0; i<SeqList_Length(list); i++)
    {
        if( (int)SeqList_Get(list, i) == key )
        {
            ret = i;
            
            SeqList_Delete(list, i);
            
            break;
        }
    }
    
    return ret;
}

int main(int argc, char *argv[])
{
    SeqList* list = SeqList_Create(SIZE);
    int a[SIZE] = {0};
    int i = 0;
    int key = 0;
    int index = 0;
    //获取当前的时间
    srand((unsigned int)time(NULL));
    //获取随机数
    for(i=0; i<SIZE; i++)
    {    
        a[i] = rand() % 100;
        SeqList_Insert(list, (SeqListNode*)(rand() % 100), i);
    }
    
    key = rand() % 100;
    
    printf("Static Search Demo ");
    printf("Key: %d ", key);
    printf("Array: ");
    print_array(a, SIZE);
    
    index = static_search(a, SIZE, key);
    
    if( index >= 0 )
    {
        printf("Success: a[%d] = %d ", index, a[index]);
    }
    else
    {
        printf("Failed! ");
    }
    
    printf("Dynamic Search Demo ");
    printf("Key: %d ", key);
    printf("List: ");
    print_list(list);
    
    index = dynamic_search(list, key);
    
    if( index >= 0 )
    {
        printf("Success: list[%d] = %d ", index, key);
    }
    else
    {
        printf("Failed! ");
    }
    
    print_list(list);
    
    return 0;
}
 

对以上程序的改进:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 20
/*    */

void print_array(int a[], int begin, int end)
{
    int i = 0;
    
    for(i=begin; i<=end; i++)
    {
        printf("%d, ", a[i]);
    }
    
    printf(" ");
}

int another_search(int a[], int len, int key)
{
    int ret = len;
    
    a[0] = key;
    
    while( a[ret] != key )
    {
        ret--;
    }
    
    return ret;
}

int main(int argc, char *argv[])
{
    int a[SIZE + 1] = {0};
    int i = 0;
    int key = 0;
    int index = 0;
    
    srand((unsigned int)time(NULL));
    
    for(i=1; i<=SIZE; i++)
    {
        a[i] = rand() % 100;
    }
    
    key = rand() % 100;
    
    printf("Another Search Demo ");
    printf("Key: %d ", key);
    printf("Array: ");
    print_array(a, 1, SIZE);
    
    index = another_search(a, SIZE, key);
    
    if( index > 0 )
    {
        printf("Success: a[%d] = %d ", index, a[index]);
    }
    else
    {
        printf("Failed! ");
    }
    
    return 0;
}

原文地址:https://www.cnblogs.com/wxb20/p/6184150.html