链表

发布时间: 更新时间: 总字数:661 阅读时间:2m 作者: IP属地: 分享 复制网址
专栏文章
  1. 顺序表的基本操作
  2. 链表(当前)
  3. C++ 单链表的基本操作
  4. 栈和队列的基本操作
  5. C++ 二叉树的基本操作
  6. 图的基本操作
  7. C++ 哈夫曼编码
  8. C++ 排序的基本操作
  9. C++ 双向链表的基本操作
  10. 顺序表的基本操作
  11. MFC中如何用ADO连接SQL Server 2005
  12. MFC使用ADO连接SQL Server数据库
  13. VC6 MFC中ClassView视图中无法显示某个类的问题
  14. rose生成C++源代码
  15. 解决<compilation debug="true" targetFramework="4.0"> 问题
  16. Cpp
  17. Clang 介绍
  18. Cpp
  19. CMake 使用介绍

链表源码

源码

# include <stdio.h>
# include <stdlib.h>  //包含了exit

//定义了一个数据类型,该数据类型的名字叫做struct Arr, 该数据类型含有三个成员, 分别是pBase, len, cnt;
struct Arr
{
    int *pBase;//存储的是数组的第一个元素的地址
    int len;//数组所能容纳的最大元素的个数
    int cnt;//当前数组有效元素的个数
};


void init_arr(struct Arr * pArr, int length);
bool append_arr(struct Arr * pArr, int val);//追加
bool insert_arr(struct Arr * pArr, int pos, int val);//pos的值从1开始
bool delete_arr(struct Arr * pArr, int pos, int * val);
int get();
bool is_empty(struct Arr * pArr);
bool is_full(struct Arr * pArr);
void sort_arr(struct Arr * pArr);
void show_arr(struct Arr * pArr);
void inversion_arr(struct Arr *pArr);


int main(void)
{
    struct Arr arr;
    int val;

    init_arr(&arr, 6);
//printf("%d\n", arr.len);
    show_arr(&arr);
    append_arr(&arr, 1);
    append_arr(&arr, 5);
    append_arr(&arr, -3);
    append_arr(&arr, 2);
    append_arr(&arr, 10);
    append_arr(&arr, -1);


    show_arr(&arr);

    printf("倒置之后的数组内容是:");
    inversion_arr(&arr);
    show_arr(&arr);

    sort_arr(&arr);
    show_arr(&arr);

    return 0;
}

void init_arr(struct Arr * pArr, int length)
{
    pArr->pBase = (int *)malloc(sizeof(int) * length);
    if (NULL == pArr->pBase)
    {
        printf("动态内存分配失败!\n");
        exit(-1);//终止整个函数
    }
    else
    {
        pArr->len = length;
        pArr->cnt = 0;
    }
    return ;
}

bool is_empty(struct Arr * pArr)
{
    if (pArr->cnt == 0)
        return true;
    else
        return false;
}

bool is_full(struct Arr * pArr)
{
    if (pArr->cnt == pArr->len)
        return true;
    else
        return false;
}

void show_arr(struct Arr * pArr)
{
    if ( is_empty(pArr) )
    {
        printf("数组为空!\n");
    }
    else
    {
        for (int i = 0; i < pArr->cnt; i++)
            printf("%d ", pArr->pBase[i]);
        printf("\n");
    }
}

bool append_arr(struct Arr * pArr, int val)
{
    if ( is_full(pArr) )
        return false;
    pArr->pBase[pArr->cnt] = val;
    pArr->cnt++;
    return true;
}

bool insert_arr(struct Arr * pArr, int pos, int val)
{
    int i;

    if (is_full(pArr))
        return false;

    if (pos < 1 || pos > pArr->cnt + 1)
        return false;

    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 Arr * pArr, int pos, int *pVal)
{
    if (is_empty(pArr))
        return false;
    if (pos < 1 || pos > pArr->cnt)
        return false;

    *pVal = pArr->pBase[pos - 1];
    for (int i = pos; i < pArr->cnt; i++)
        pArr->pBase[i - 1] = pArr->pBase[i];

    (pArr->cnt)--;
    return true;
}

void inversion_arr(struct Arr *pArr)
{
    int i = 0;
    int j = pArr->len - 1;

    int t;

    while (i < j)
    {
        t = pArr->pBase[i];
        pArr->pBase[i] = pArr->pBase[j];
        pArr->pBase[j] = t;
        i++;
        j--;
    }

    return ;
}

void sort_arr(struct Arr * pArr)
{
    int i, j, t;

    for (i = 0; i < pArr->cnt; i++)
    {
        for (j = i + 1; j < pArr->cnt; j++)
        {
            if (pArr->pBase[i] > pArr->pBase[j])
            {
                t = pArr->pBase[i];
                pArr->pBase[i] = pArr->pBase[j];
                pArr->pBase[j] = t;
            }
        }
    }

}
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数