utils.h
来自「经典数据结构书籍 数据结构C++语言描述 的源代码 很难找的哦」· C头文件 代码 · 共 47 行
H
47 行
#ifndef __UTILS__
#define __UTILS__
#include <string.h>
// using key, search the n element arrray list for a match.
// if found return index at the match; otherwise return -1
template <class T>
int SeqSearch(T list[], int n, T key)
{
for(int i=0;i < n;i++)
if (list[i] == key)
return i; // return index of matching item
return -1; // search failed. return -1
}
// search array of strings for match with string key
int SeqSearch(char *list[], int n, char *key)
{
for(int i=0;i < n;i++)
// compare using C++ string library function
if (strcmp(list[i],key) == 0)
return i; // return index at match
return -1; // return -1 on failure
}
// sort the n element a using the exchange sort algorithm
template <class T>
void ExchangeSort(T a[], int n)
{
T temp;
int i, j;
// make n-1 passes
for (i = 0; i < n-1; i++)
// put smallest of a[i+1]...a[n-1] in a[i]
for (j = i+1; j < n; j++)
if (a[j] < a[i])
{
// swap a[i] and a[j]
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
#endif // __UTILS__
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?