实现静态顺序表和动态顺序表

其实顺序表的静态实现以及动态实现和通讯录的实现原理基本上是一致的,在这里就不做过多的解释,直接贴代码。
静态通讯录:http://blog.csdn.net/quinn0918/article/details/71937767
动态通讯录:http://blog.csdn.net/quinn0918/article/details/72182598
文件流通讯录:http://blog.csdn.net/quinn0918/article/details/72526886

静态顺序表:

Seqlist.h头文件

#ifndef __SEQLIST_H__

#include
#include
#include
#define MAX 100
typedef int DataType;

typedef struct Seqlist
{
    DataType Data[MAX];
    int sz;
}Seqlist,*pseqlist;

void InitSeqlist(pseqlist ps);//初始化顺序表
void PushBack(pseqlist ps,DataType num);//尾部压入
void PrintSeqlist(const pseqlist ps);//打印顺序表
void PopBack(pseqlist ps);//尾部删除
void PushFront(pseqlist ps, DataType num);//头部插入
void PopFront(pseqlist ps);//头部删除
void Insert(pseqlist ps, int pos, DataType num);//指定位置插入
int Find(pseqlist ps, DataType num);//查找下标
void Remove(pseqlist ps, DataType num);//删除所有指定元素
void ReverseSeqlist(pseqlist ps);//逆序
void SortSeqlist(pseqlist ps);//排序
int BinarySearch(pseqlist ps, DataType num);//二分查找
#endif //__SEQLIST_H__

Seqlist.h实现部分

#include "Seqlist.h"

void InitSeqlist(pseqlist ps)
{
    assert(ps);
    ps->sz = 0;
    memset(ps->Data, 0, sizeof(ps->Data));
}

void PushBack(pseqlist ps, DataType num)
{
    assert(ps);
    if (ps->sz == MAX)
    {
        return;
    }
    ps->Data[ps->sz] = num;
    ps->sz += 1;
}

void PrintSeqlist(const pseqlist ps)
{
    assert(ps);
    int i = 0;
    for (i = 0; i < ps->sz; i++)
    {
        printf("%d ",ps->Data[i]);
    }
    printf("\n");
}

void PopBack(pseqlist ps)
{
    assert(ps);
    if (ps->sz == 0)
    {
        return;
    }
    ps->sz--;
}

void PushFront(pseqlist ps, DataType num)
{
    assert(ps);
    int i = 0;
    if (ps->sz == MAX)
    {
        return;
    }
    for (i = ps->sz; i > 0; i--)
    {
        ps->Data[i] = ps->Data[i-1];
    }
    ps->Data[0] = num;
    ps->sz++;
}


void PopFront(pseqlist ps)
{
    assert(ps);

    if (ps->sz)
    {
        int i = 0;
        for (i = 0; i < ps->sz-1; i++)
        {
            ps->Data[i] = ps->Data[i + 1];
        }
        ps->sz--;
    }
}

void Insert(pseqlist ps, int pos, DataType num)
{
    assert(ps);
    int i = 0;
    if (ps->sz == MAX)
    {
        return;
    }
    for (i = ps->sz; i > pos; i--)
    { 
        ps->Data[i] = ps->Data[i - 1];
    }
    ps->Data[pos] = num;
    ps->sz++;
}

int Find(pseqlist ps, DataType num)
{
    assert(ps);
    int i = 0;
    for (i = 0; i < ps->sz; i++)
    {
        if (ps->Data[i] == num)
        {
            return i;
        }
    }
    return -1;
}


void Remove(pseqlist ps, DataType num)
{
    assert(ps);
    int i = 0;
    int ret = 0;
    if (ps->sz == 0)
    {
        return;
    }
    while ((ret = Find(ps, num))!=-1)
    {
        for (i = ret; i < ps->sz - 1; i++)
        {
            ps->Data[i] = ps->Data[i + 1];
        }
        ps->sz--;
    }
}

void ReverseSeqlist(pseqlist ps)
{
    assert(ps);
    int left = 0;
    int right = ps->sz - 1;
    while (left < right)
    {
        DataType tmp = ps->Data[left];
        ps->Data[left] = ps->Data[right];
        ps->Data[right] = tmp;
        left++;
        right--;
    }
}

void SortSeqlist(pseqlist ps)
{
    assert(ps);
    int i = 0;
    int j = 0;
    for (i = 0; i < ps->sz; i++)
    {
        for (j = 0; j < ps->sz-i-1; j++)
        {
            if (ps->Data[j] > ps->Data[j + 1])
            {
                DataType tmp = ps->Data[j+1];
                ps->Data[j+1] = ps->Data[j];
                ps->Data[j] = tmp;
            }
        }
    }
}

int BinarySearch(pseqlist ps, DataType num)
{
    assert(ps);
    int left = 0;
    int right = ps->sz - 1;
    while (left <= right)
    {
        int mid = left - (left - right);
        if (ps->Data[mid] < num)
        {
            left = mid + 1;
        }
        else if (ps->Data[mid] > num)
        {
            right = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}

test.c测试部分

#include "Seqlist.h"

void test()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushBack(&mylist, 1);//尾部插入元素
    PushBack(&mylist, 2);
    PushBack(&mylist, 3);
    PushBack(&mylist, 4);
    PushBack(&mylist, 5);
    PrintSeqlist(&mylist);
    PopBack(&mylist);//尾部删除
    PrintSeqlist(&mylist);
}

void test1()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushFront(&mylist, 6);//头部插入
    PushFront(&mylist, 7);
    PushFront(&mylist, 8);
    PushFront(&mylist, 9);
    PushFront(&mylist, 10);
    PrintSeqlist(&mylist);
    PopFront(&mylist);//头部删除
    PrintSeqlist(&mylist);
}

void  test2()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushFront(&mylist, 6);//头部插入
    PushFront(&mylist, 7);
    PushFront(&mylist, 8);
    PushFront(&mylist, 9);
    PushFront(&mylist, 10);
    int num = Find(&mylist, 10);//查找
    if (num !=-1)
        Insert(&mylist, num, 11);//指定位置插入
    PrintSeqlist(&mylist);
}

void test3()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushFront(&mylist, 11);//头部插入
    PushFront(&mylist, 7);
    PushFront(&mylist, 8);
    PushFront(&mylist, 10);
    PushFront(&mylist, 9);
    PushFront(&mylist, 10);
    PrintSeqlist(&mylist);
    Remove(&mylist, 10);//删除所有指定元素
    PrintSeqlist(&mylist);
    ReverseSeqlist(&mylist);//逆序
    PrintSeqlist(&mylist);
    SortSeqlist(&mylist);//排序
    PrintSeqlist(&mylist);
    int num = BinarySearch(&mylist,11);//二分查找
    if (num != -1)
        Insert(&mylist, num, 2);
    PrintSeqlist(&mylist);
}

int main()
{
    //test();
    //test1();
    //test2();
    test3();
    system("pause");
    return 0;
}

本人亲测,一点问题也没有!!!动态顺序表马上就来!!!!

动态顺序表

Seqlist_D.h头文件

#ifndef __SEQLIST_D_H__


#include
#include
#include
#define INIT 2
#define INIC 3
typedef int DataType;
typedef struct Seqlist
{
    DataType *Data;
    int sz;
    int capa;
}Seqlist,*pseqlist;

void InitSeqlist(pseqlist ps);//初始化
void DestorySeqlist(pseqlist ps);//释放内存
void PushBack(pseqlist ps, DataType num);//尾部压入
void PrintSeqlist(const pseqlist ps);//打印顺序表
void PopBack(pseqlist ps);//尾部删除
void PushFront(pseqlist ps, DataType num);//头部插入
void PopFront(pseqlist ps);//头部删除
void Insert(pseqlist ps, int pos, DataType num);//指定位置插入
int Find(pseqlist ps, DataType num);//查找下标
void Remove(pseqlist ps, DataType num);//删除所有指定元素
void ReverseSeqlist(pseqlist ps);//逆序
void SortSeqlist(pseqlist ps);//排序
int BinarySearch(pseqlist ps, DataType num);//二分查找
#endif  //__SEQLIST_D_H__

Seqlist_D.c实现部分

#include "Seqlist_D.h"

void InitSeqlist(pseqlist ps)
{
    assert(ps);
    ps->Data = (DataType*)malloc(sizeof(DataType)*INIT);
    ps->sz = 0;
    ps->capa = INIT;
}

void DestorySeqlist(pseqlist ps)
{
    assert(ps);
    if (ps->Data != NULL)
    {
        free(ps->Data);
        ps->Data = NULL;
    }
}

void check(pseqlist ps)//检测动态内存分配
{
    if (ps->sz == ps->capa)
    {
        DataType *tmp = (DataType *)realloc(ps->Data, (ps->capa + INIC)*sizeof(DataType));
        //当实际个数和初始化的容量相等时用realloc增容  
        if (tmp == NULL)
        {
            printf("out of menory\n");
            exit(1);
        }
        else
        {
            ps->Data = tmp;
            ps->capa += INIC;
        }
    }
}

void PushBack(pseqlist ps, DataType num)
{
    assert(ps);
    check(ps);
    ps->Data[ps->sz] = num;
    ps->sz += 1;
}

void PrintSeqlist(const pseqlist ps)
{
    assert(ps);
    int i = 0;
    for (i = 0; i < ps->sz; i++)
    {
        printf("%d ", ps->Data[i]);
    }
    printf("\n");
}

void PopBack(pseqlist ps)
{
    assert(ps);
    if (ps->sz == 0)
    {
        return;
    }
    ps->sz--;
}

void PushFront(pseqlist ps, DataType num)
{
    assert(ps);
    check(ps);
    int i = 0;
    for (i = ps->sz; i > 0; i--)
    {
        ps->Data[i] = ps->Data[i - 1];
    }
    ps->Data[0] = num;
    ps->sz++;
}


void PopFront(pseqlist ps)
{
    assert(ps);

    if (ps->sz)
    {
        int i = 0;
        for (i = 0; i < ps->sz - 1; i++)
        {
            ps->Data[i] = ps->Data[i + 1];
        }
        ps->sz--;
    }
}

void Insert(pseqlist ps, int pos, DataType num)
{
    assert(ps);
    check(ps);
    int i = 0;
    for (i = ps->sz; i > pos; i--)
    {
        ps->Data[i] = ps->Data[i - 1];
    }
    ps->Data[pos] = num;
    ps->sz++;
}

int Find(pseqlist ps, DataType num)
{
    assert(ps);
    int i = 0;
    for (i = 0; i < ps->sz; i++)
    {
        if (ps->Data[i] == num)
        {
            return i;
        }
    }
    return -1;
}


void Remove(pseqlist ps, DataType num)
{
    assert(ps);
    int i = 0;
    int ret = 0;
    if (ps->sz == 0)
    {
        return;
    }
    while ((ret = Find(ps, num)) != -1)
    {
        for (i = ret; i < ps->sz - 1; i++)
        {
            ps->Data[i] = ps->Data[i + 1];
        }
        ps->sz--;
    }
}

void ReverseSeqlist(pseqlist ps)
{
    assert(ps);
    int left = 0;
    int right = ps->sz - 1;
    while (left < right)
    {
        DataType tmp = ps->Data[left];
        ps->Data[left] = ps->Data[right];
        ps->Data[right] = tmp;
        left++;
        right--;
    }
}

void SortSeqlist(pseqlist ps)
{
    assert(ps);
    int i = 0;
    int j = 0;
    for (i = 0; i < ps->sz; i++)
    {
        for (j = 0; j < ps->sz - i - 1; j++)
        {
            if (ps->Data[j] > ps->Data[j + 1])
            {
                DataType tmp = ps->Data[j + 1];
                ps->Data[j + 1] = ps->Data[j];
                ps->Data[j] = tmp;
            }
        }
    }
}

int BinarySearch(pseqlist ps, DataType num)
{
    assert(ps);
    int left = 0;
    int right = ps->sz - 1;
    while (left <= right)
    {
        int mid = left - (left - right);
        if (ps->Data[mid] < num)
        {
            left = mid + 1;
        }
        else if (ps->Data[mid] > num)
        {
            right = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}

test.c测试部分

#include "Seqlist_D.h"

void test()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushBack(&mylist, 1);//尾部插入元素
    PushBack(&mylist, 2);
    PushBack(&mylist, 3);
    PushBack(&mylist, 4);
    PushBack(&mylist, 5);
    PrintSeqlist(&mylist);
    PopBack(&mylist);//尾部删除
    PrintSeqlist(&mylist);
    DestorySeqlist(&mylist);//释放内存
}

void test1()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushFront(&mylist, 6);//头部插入
    PushFront(&mylist, 7);
    PushFront(&mylist, 8);
    PushFront(&mylist, 9);
    PushFront(&mylist, 10);
    PrintSeqlist(&mylist);
    PopFront(&mylist);//头部删除
    PrintSeqlist(&mylist);
    DestorySeqlist(&mylist);//释放内存
}

void  test2()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushFront(&mylist, 6);//头部插入
    PushFront(&mylist, 7);
    PushFront(&mylist, 8);
    PushFront(&mylist, 9);
    PushFront(&mylist, 10);
    int num = Find(&mylist, 10);//查找
    if (num != -1)
        Insert(&mylist, num, 11);//指定位置插入
    PrintSeqlist(&mylist);
    DestorySeqlist(&mylist);//释放内存
}

void test3()
{
    Seqlist mylist;
    InitSeqlist(&mylist);//初始化顺序表
    PushFront(&mylist, 11);//头部插入
    PushFront(&mylist, 7);
    PushFront(&mylist, 8);
    PushFront(&mylist, 10);
    PushFront(&mylist, 9);
    PushFront(&mylist, 10);
    PrintSeqlist(&mylist);
    Remove(&mylist, 10);//删除所有指定元素
    PrintSeqlist(&mylist);
    ReverseSeqlist(&mylist);//逆序
    PrintSeqlist(&mylist);
    SortSeqlist(&mylist);//排序
    PrintSeqlist(&mylist);
    int num = BinarySearch(&mylist, 11);//二分查找
    if (num != -1)
        Insert(&mylist, num, 2);
    PrintSeqlist(&mylist);
    DestorySeqlist(&mylist);//释放内存
}

int main()
{
    //test();
    //test1();
    //test2();
    test3();
    system("pause");
    return 0;
}

到这里静态和动态的顺序表就结束了,在代码中我都加入了好几个测试环节,贴测试结果的话图会很多,又由于代码有点长,程序测试结果就不往上贴了,读者要是不放心的话尽管测一下。

你可能感兴趣的:(C程序典型示例,C语言知识库)