void rmv(int *ptr,int in,const unsigned int n)
{
unsigned int i
for (i=ini<n-1i++) {
*(ptr+i)=*(ptr+i+1)
}
realloc(ptr,sizeof(int)*(n-1))
}
-----------------------------------------------
插入函数,在整形数组ptr中的ptr[in]之前插入x,n是原数组的长度
void rin(int *ptr,int in,int x,const unsigned int n)
{
unsigned int i
realloc(ptr,sizeof(int)*(n+1))
for (i=ni>ini--) {
*(ptr+i)=*(ptr+i-1)
}
ptr[in]=x
}
需要包含stdlib.h头文件。
示例:
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
void rmv(int *ptr,int in,const unsigned int n)
{
unsigned int i
for (i=ini<n-1i++) {
*(ptr+i)=*(ptr+i+1)
}
realloc(ptr,sizeof(int)*(n-1))
}
void rin(int *ptr,int in,int x,const unsigned int n)
{
unsigned int i
realloc(ptr,sizeof(int)*(n+1))
for (i=ni>ini--) {
*(ptr+i)=*(ptr+i-1)
}
ptr[in]=x
}
int main(int argc, char* argv[])
{
int *p,i
p=calloc(6,sizeof(int))
for (i = 0i<6i++) {
p[i]=i
}
*(p+2)=124
for (i=0i<6i++) {
printf("%d ",p[i])
}
putchar('\n')
rmv(p,2,6)
for (i=0i<6i++) {
printf("%d ",p[i])
}
rin(p,2,258,5)
putchar('\n')
for (i=0i<6i++) {
printf("%d ",p[i])
}
system("pause")
free(p)
return 0
}
//---------------------------------------------------------------------------
bool binary_search(int * arr, int size, int v){if((arr == NULL) || (size <= 0)){
return false
}
int bIndex = 0
int eIndex = size - 1
for(int curIndex = size / 2 eIndex != bIndex curIndex = (bIndex + eIndex) /2){
if(v < arr[curIndex]){
eIndex = curIndex
}else if(v > arr[curIndex]){
bIndex = curIndex
}else{
for(int j = curIndex + 1 j < size ++j){
arr[j - 1] = arr[j]
}
return true
}
}
return false
}
int _tmain(int argc, _TCHAR* argv[]){
int arr[20] = {0}
for(int i = 0 i < 20 ++i){
arr[i]=i
}
for(int i = 0 i < 20 ++i){
cout << arr[i] << "\t"
}
if(binary_search(arr, 20, -116)){
for(int i = 0 i < 19 ++i){
cout << arr[i] << "\t"
}
}else{
cout << "not found" << endl
}
system("pause")
//AGCDB::uninstallAPI()
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)