zoukankan      html  css  js  c++  java
  • 对动态数组的操作与算法

    直接贴程序:

    /*
    	2016年9月12日15:00:49
    	对动态数组的操作与算法
    */
    #include<stdio.h>
    #include<malloc.h>    //malloc函数在此头文件
    #include<stdlib.h>   //使用exit函数需要此头文件
    
    struct Array
    {
    	int len;    //数组的长度
    	int cnt;    //数组有效数值的个数
    	int * pBase;   //存储数组第一个元素的地址
    };
    
    //函数声明
    void init_arr(struct Array * pArr, int length);    
    void show_arr(struct Array * pArr);
    bool is_empty(struct Array * pArr);
    bool is_full(struct Array * pArr);
    bool append_arr(struct Array * pArr, int val);
    bool insert_arr(struct Array * pArr, int pos, int val);
    bool delete_arr(struct Array * pArr, int pos, int * val);
    int get(struct Array * pArr, int pos);
    void inversion_arr(struct Array * pArr);
    void sort_arr(struct Array * pArr); 
    
    
    int main(void)    //主函数用来测试各个函数的正确性
    {
    	struct Array arr;
    	int val;
    	
    	init_arr(&arr, 6);     //初始化	
    	show_arr(&arr);            //输出数组的值
    	append_arr(&arr, 0);
    	append_arr(&arr, 8);
    	append_arr(&arr, 3);
    	append_arr(&arr, 6);
    	append_arr(&arr, -2);
    	show_arr(&arr);
    	sort_arr(&arr);
    	show_arr(&arr);
    	
    	
    	// printf("%d
    ",get(3));
    	// insert_arr(&arr, 1, 99);
    	// show_arr(&arr);
    	// if(delete_arr(&arr, 3, &val))
    	// {
    		// printf("删除的值为:%d
    ", val);
    	// }
    	// else 
    	// {
    		// printf("删除失败!
    ");
    	// }
    	// show_arr(&arr);
    	
    	
    	return 0;
    }
    
    void init_arr(struct Array * pArr, int length)   //初试化,即创建一个数组
    {
    	pArr->pBase = (int *)malloc( sizeof(int) * length);   //动态数组
    	if( NULL == pArr->pBase)
    	{
    		printf("内存分配失败,程序终止!
    ");
    		exit(-1);
    	}
    	
    	pArr->len = length;
    	pArr->cnt = 0;
    	
    	return;
    }
    
    void show_arr(struct Array * pArr)       //输出数组的值
    {
    	int i;
    	if( 0 == pArr->cnt)      //先判断数组是否为空
    	{
    		printf("该数组为空!
    ");
    	}
    	else
    	{
    		for(i = 0; i < pArr->cnt; ++i)
    		{
    			printf("  %d  
    ", pArr->pBase[i]);
    		}
    		printf("
    ");
    	}
    }
    
    bool is_empty(struct Array * pArr)     //判断数组是否空
    {
    	if( 0 == pArr->cnt )
    		return true;
    	else 
    		return false;
    }
    
    bool is_full(struct Array * pArr)     //判断数组是否满
    {
    	if( pArr->len == pArr->cnt )
    		return true;
    	else 
    		return false;
    }
    
    bool append_arr(struct Array * pArr, int val)   //在数组末尾追加新元素val
    {
    	if( is_full(pArr) )   //先判断数组是否满
    	{
    		return false;
    	}		
    	else
    	{
    		pArr->pBase[pArr->cnt] = val;
    		++(pArr->cnt);
    		
    		return true;
    	}
    }
    
    bool insert_arr(struct Array * pArr, int pos, int val)   //在位置pos上插入一个新元素val,pos从1开始
    {
    	int i;
    	
    	if( is_full(pArr))        //先判断数组是否满
    	{
    		return false;
    	}
    	else if(pos<0 || pos>(pArr->cnt)+1)   //判断插入的位置是否有效
    	{
    		return false;
    	}
    	else
    	{
    		for(i = (pArr->cnt)-1; i >= pos-1; --i)     //将要插入位置右边的数值整体向后移一个位置
    		{
    			pArr->pBase[i+1] = pArr->pBase[i];
    		}
    		pArr->pBase[pos-1] = val;
    		++(pArr->cnt);
    		
    		return true;
    	}
    }
    
    bool delete_arr(struct Array * pArr, int pos, int * val)      //将位置pos上的元素删除,并用val接收删除的值
    {
    	int i;
    	
    	if( is_empty(pArr))        //先判断数组是否空
    	{
    		return false;
    	}
    	else if(pos<0 || pos>pArr->cnt)   //判断删除的数值是否有效范围
    	{
    		return false;
    	}
    	else
    	{
    		(*val) = pArr->pBase[pos-1];
    		for(i = pos; i<pArr->cnt; ++i)        //将要删除的元素右边的数值整体向前移一个位置
    		{
    			pArr->pBase[i-1] = pArr->pBase[i];
    		}
    		--(pArr->cnt);	
    		
    		return true;
    	}	
    }
    
    int get(struct Array * pArr, int pos)     //获取数组pos位置上的数值,pos从1开始
    {
    	return pArr->pBase[pos-1];
    }
    
    void inversion_arr(struct Array * pArr)    //将数组的元素倒置
    {
    	int i, j, t;
    	i = 0;
    	j = pArr->cnt-1;
    	while(i < j)
    	{
    		t = pArr->pBase[i];
    		pArr->pBase[i] = pArr->pBase[j];
    		pArr->pBase[j] = t;
    		++i;
    		--j;
    	}
    	
    	return;
    }
    
    void sort_arr(struct Array * pArr)     //将数组的元素从小到大排序,使用的是冒泡排序
    {
    	int i, j, t;
    	for(i = 0; i < pArr->cnt-1; ++i)
    	{
    		for(j = 0; j < pArr->cnt-1-i; ++j)
    		{
    			if(pArr->pBase[j] > pArr->pBase[j+1])
    			{
    				t = pArr->pBase[j];
    				pArr->pBase[j] = pArr->pBase[j+1];
    				pArr->pBase[j+1] = t;
    			}
    
    		}
    	}
    	
    	return;	
    }
    
    
    
    
    


  • 相关阅读:
    php 三元运算符简洁用法
    Linux 获取网卡信息
    《构建之法》读后感
    毫秒数据字符串转换为DateTime
    POST请求——HttpWebRequest
    多个不同类对象传输思路
    WCF 大文件传输配置
    WCF配置文件详解
    C# 元组、匿名对象、ref&out
    C#控制台关闭之前做一些操作
  • 原文地址:https://www.cnblogs.com/yzy-blogs/p/6597333.html
Copyright © 2011-2022 走看看