[郝斌]数据结构C语言—数组

[郝斌]数据结构C语言—数组,第1张

[郝斌]数据结构C语言—数组 算法的定义

狭义的算法是与数据的存数方式密切相关

广义的算法是与数据的存储方式无关

泛型:利用某种技术达到的效果就是:不同的存数方式,执行的 *** 作是一样的。

数组

线性结构:可以把所有节点用一根直线串起来,包括数组和链表。

数据结构中的数组并不是我们印象中的编程语言中包含的具体数据结构

数组,在内存中以一组连续的数据集合的形式存储相同数据类型的数据。数组内的数据可以随机访问。

我们对数组进行二次封装,在内存上申请10个int长度进行数据的 *** 作,并封装初始化、添加、插入、显示、删除、排序和反序接口。

#include  
#include  
#include 

typedef unsigned int BOOL;

typedef struct ArrayList
{
    int * pArraybaseAddr;
    int length;
    int currentLeng;
}ARRAY,*PARRAY;

void init_arr(PARRAY pArray, int arrlength);
BOOL is_fullarr(PARRAY pArray);
BOOL is_emptyarr(PARRAY pArray);
BOOL append_arr(PARRAY pArray, int addNumber);
BOOL insert_arr(PARRAY pArray, int addNumber, int port); //port位置标号从1开始
void show_arr(PARRAY pArray);
BOOL delete_arr(PARRAY pArray, int port, int *pval);
BOOL sort_arr(PARRAY pArray, int mode);
void inversion_arr(PARRAY pArray);

int main()
{
    PARRAY parraylist;
    int length = 10;
    int val = 0;

    init_arr(parraylist, length);
    append_arr(parraylist, 19);
    append_arr(parraylist, 2);
    append_arr(parraylist, 90);
    append_arr(parraylist, 76);
    append_arr(parraylist, 34);
    append_arr(parraylist, 82);
    append_arr(parraylist, 100);
    show_arr(parraylist);

    printf("n");
//    insert_arr(parraylist, 8, 8);

    sort_arr(parraylist, 1);
//    inversion_arr(parraylist);
    show_arr(parraylist);


    return 0;
}

void init_arr(PARRAY pArray, int arrlength)
{
    pArray->pArraybaseAddr = (int *)malloc(sizeof(int)* arrlength);
    if(pArray->pArraybaseAddr != NULL)
        {
            printf("ArrayList init is OK!n");
            pArray->length = arrlength;
            pArray->currentLeng = 0;
        }
    else
        {
            printf("ArrayList init is Error!n");
        }
}

BOOL is_fullarr(PARRAY pArray)
{
    if (pArray ->currentLeng == pArray ->length)
        {
            return true;
        }
    else
        {
            return false;
        }
}

BOOL is_emptyarr(PARRAY pArray)
{
    if (pArray ->currentLeng == 0)
        {
            return true;
        }
    else
        {
            return false;
        }
}

BOOL append_arr(PARRAY pArray, int addNumber)
{
    if (   is_fullarr( pArray )  )  //不需要==true
    {
        printf ("Array is Full!!n");
        return false;
    }
    else
    {
        (pArray->pArraybaseAddr)[pArray->currentLeng] = addNumber;
        pArray->currentLeng ++ ;
        return true;
    }
}

BOOL  insert_arr(PARRAY pArray, int addNumber, int port)
{
    if (   is_fullarr( pArray )  )
        {
            printf ("Array is Full!!n");
            return false;
        }
    else if(   (port < 0)  ||  (port > (pArray->currentLeng +1)) )
        {
            printf("the port is error!n");
            return false;
        }
    else
        {
            for(int i = pArray->currentLeng; i >= port; i-- )
            {
                (pArray->pArraybaseAddr)[ i ]  = (pArray->pArraybaseAddr)[ i-1 ];
            }
            (pArray->pArraybaseAddr)[port-1] = addNumber;
            pArray->currentLeng ++ ;

            return  true;
        }
}

void show_arr(PARRAY pArray)
{
    if ( is_emptyarr( pArray ) )
        {
            printf(" Array is Empty!!n ");
        }
    else
        {
            for(int i = 0; i < (pArray->currentLeng); i++)
            {
                printf("num %d is %dn", i+1, (pArray->pArraybaseAddr)[i]);
            }
        }
}

BOOL delete_arr(PARRAY pArray, int port,  int *pval)
{
    if (is_emptyarr(pArray))
    {
        printf(" Array is Empty!!n ");
        return false;
    }
    else if(   (port < 0)  ||  (port > (pArray->currentLeng)) )
    {
        printf("the port is error!n");
        return false;
    }
    
    else
    {
        *pval = (pArray->pArraybaseAddr)[port -1];
        for (int i =port; i < pArray->currentLeng; i++)
        {
            (pArray->pArraybaseAddr)[i-1] = (pArray->pArraybaseAddr)[i];
        }
        pArray->currentLeng -- ;
        return true;
    }
}

BOOL sort_arr(PARRAY pArray, int mode)
{
    int swap = 0;
    if (is_emptyarr(pArray))
    {
        printf("Array is empty!!n");
        return false;
    }
    else if (mode == 0)
    {
        for(int i = 0; i < pArray->currentLeng; i ++)
        {
            for(int j = i + 1; j < (pArray->currentLeng); j++)
            {
                if( (pArray->pArraybaseAddr)[i] > (pArray->pArraybaseAddr)[j] )
                {
                      swap = (pArray->pArraybaseAddr)[j];
                      (pArray->pArraybaseAddr)[j] = (pArray->pArraybaseAddr)[i];
                      (pArray->pArraybaseAddr)[i] = swap;
                }
            }
        }
        return true;
    }
    else if (mode == 1)
    {
        for(int i = 0; i < pArray->currentLeng; i ++)
        {
            for(int j = i + 1; j < (pArray->currentLeng); j++)
            {
                if( (pArray->pArraybaseAddr)[i] < (pArray->pArraybaseAddr)[j] )
                {
                      swap = (pArray->pArraybaseAddr)[j];
                      (pArray->pArraybaseAddr)[j] = (pArray->pArraybaseAddr)[i];
                      (pArray->pArraybaseAddr)[i] = swap;
                }
            }
        }
        return true;
    }
}

void inversion_arr(PARRAY pArray)
{
    int i = 0;
    int j = pArray->currentLeng -1;
    int swap = 0;

    while(ipArraybaseAddr)[i];
        (pArray->pArraybaseAddr)[i] = (pArray->pArraybaseAddr)[j];
        (pArray->pArraybaseAddr)[j] = swap;
        i ++;
        j --;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存