• 动态顺序表


    seqlist.h

    #pragma once
    #define  _SEQ_LIST_
    #ifdef  _SEQ_LIST_
    #include<stdio.h>
    #include<assert.h>
    #include<string.h>
    
    #define DEFAULT_CAPACITY  3
    typedef int DataType;
    
    typedef struct SeqList
    {
        DataType *arr;
        size_t size;
        size_t capacity;
    
    }SeqList;
    typedef enum TAG
    {
        TRUE,
        FALSE,
    }TAG;
    typedef struct Findret
    {
        TAG Isfind;  //是否找到下标
        size_t index;  //找到数据的下标
    }Findret;
    
    
    void Cheekcapacity(SeqList* pSeq);//检查容量
    void InitSeqList(SeqList* pSeq); //初始化链表
    void PrintSeqList(SeqList* pSeq);//输出链表
    void PushBack(SeqList* pSeq, DataType x);//在链表后面加入数据
    void PopBack(SeqList* pSeq); //删除链表最后一个元素
    void PushFront(SeqList* pSeq, DataType x);//在链表开头插入一个元素。其它元素依次向后移
    void PopFront(SeqList* pSeq); //删除第一个元素
    void Insert(SeqList* pSeq, size_t index, DataType x);//在index位置插入x
    void Modified(SeqList* pSeq, size_t index, DataType x);//将index位置的元素改动为x
    void removed(SeqList* pSeq, size_t index); //移除index位置的元素
    Findret  Find(SeqList* pSeq, DataType x, size_t index);//查找数据x,并返回下标
    TAG Erase(SeqList* pSeq, DataType x, TAG all); //all等于TURE表示删除全部x
                                                  //all等于FALSE表示删除第一个x
    void Destorycapacity(SeqList* pSeq);
    void BubbleSort(SeqList* pSeq); //冒泡排序
    void SelectSort(SeqList* pSeq); //选择排序
    Findret BinarySearch(SeqList* pSeq, DataType x);//二分搜索。在已将排好序的数组中进行搜索
    
    #endif  //_SEQ_LIST

    seqlist.c

    #include"seqlist.h"
    
    
    void InitSeqList(SeqList* pSeq)
    {
        assert(pSeq);
        pSeq->arr = (DataType*)malloc(DEFAULT_CAPACITY * sizeof(DataType));
        pSeq->size = 0;
        pSeq->capacity = DEFAULT_CAPACITY;
    }
    void PrintSeqList(SeqList* pSeq)
    {
        int i = 0;
        assert(pSeq);
        for (; i < pSeq->size; i++)
        {
            printf("%d ", pSeq->arr[i]);
        }
    
        printf("
    ");
    }
    void Cheekcapacity(SeqList* pSeq)
    {
        if ((pSeq->size) == pSeq->capacity) //推断链表是否已满
        {
            DataType* tmp = malloc(pSeq->capacity * 2 * sizeof(DataType));
            pSeq->capacity = pSeq->capacity * 2;
            memcpy(tmp, pSeq->arr, sizeof(DataType)* pSeq->size);
            free(pSeq->arr);
            pSeq->arr = tmp;
        }
    }
    void PushBack(SeqList* pSeq, DataType x)
    {
        assert(pSeq);
        Cheekcapacity(pSeq);
    
        pSeq->arr[(pSeq->size)++] = x;
    }
    
    void PopBack(SeqList* pSeq)
    {
        assert(pSeq);
        if (pSeq->size == 0)
        {
            printf("SeqList is empty
    ");
            return;
        }
        pSeq->arr[--(pSeq->size)] = 0; //或者 --(pSeq->size);
    }
    void PushFront(SeqList* pSeq, DataType x)
    {
        int i = 0;
        assert(pSeq);
        Cheekcapacity(pSeq);
        for (i = pSeq->size; i > 0; i--)
        {
            pSeq->arr[i] = pSeq->arr[i - 1];
        }
        pSeq->arr[0] = x;
        ++(pSeq->size);
    }
    void PopFront(SeqList* pSeq)
    {
        int i = 0;
        assert(pSeq);
        if (pSeq->size == 0)
        {
            printf("SeqList is empty
    ");
            return;
        }
        for (; i < pSeq->size; i++)
        {
            pSeq->arr[i] = pSeq->arr[i + 1];
    
        }
        (pSeq->size)--;
    }
    void Insert(SeqList* pSeq, size_t index, DataType x)
    {
        int i = 0;
        assert(pSeq);
        assert(index <= pSeq->size);
        Cheekcapacity(pSeq);
    
        for (i = pSeq->size; i > index; i--)
        {
            pSeq->arr[i] = pSeq->arr[i - 1];
        }
        pSeq->arr[index] = x;
        (pSeq->size)++;
    }
    void Modified(SeqList* pSeq, size_t index, DataType x)
    {
        assert(pSeq);
        assert(index < pSeq->size);
        pSeq->arr[index] = x;
    }
    void removed(SeqList* pSeq, size_t index)
    {
    
        assert(pSeq);
        assert(index <= pSeq->size);
        for (; index < pSeq->size; index++)
        {
            pSeq->arr[index] = pSeq->arr[index + 1];
        }
        (pSeq->size)--;
    }
    
    Findret  Find(SeqList* pSeq, DataType x, size_t index)
    {
        Findret ret;
        assert(pSeq);
        for (; index < pSeq->size; index++)
        {
            if (pSeq->arr[index] == x)
            {
                ret.Isfind = TRUE;
                ret.index = index;
                return ret;
            }
        }
        ret.Isfind = FALSE;
        return ret;
    }
    TAG Erase(SeqList* pSeq, DataType x, TAG all)
    {
        TAG success = FALSE;
        Findret ret;
        assert(pSeq);
        ret = Find(pSeq, x, 0);
        while (ret.Isfind == TRUE)
        {
            success = TRUE;
            removed(pSeq, ret.index);
            pSeq->size--;
            if (all == FALSE)
            {
                break;
            }
            ret = Find(pSeq, x, ret.index);
        }
        return success;
    
    }
    
    void swap(DataType* l, DataType* r)
    {
        DataType tmp = *l;
        *l = *r;
        *r = tmp;
    }
    void BubbleSort(SeqList* pSeq)
    {
        int i = 0;
        int j = 0;
        int exchange = 0;  //优化降低循环次数
        assert(pSeq);
        for (i = 0; i < pSeq->size; i++)
        {
            for (j = 0; j < pSeq->size - i - 1; j++)
            {
                exchange = 0;
                if (pSeq->arr[j] > pSeq->arr[j + 1])
                {
                    swap(&pSeq->arr[j], &pSeq->arr[j + 1]);
                    exchange = 1;
                }
    
            }
            if (exchange == 0) //没有进行一次交换。已经排好序
                {
                    break;
                }
        }
    }
    void SelectSort(SeqList* pSeq)
    {
        size_t minindex, index, begin;
        for (begin = 0; begin < pSeq->size - 1; begin++)
        {
            minindex = begin;
            for (index = begin + 1; index < pSeq->size; index++)
            {
                if (pSeq->arr[minindex] > pSeq->arr[index])
                {
                    minindex = index; //找到最小的数的下标
                }
            }
            if (minindex != begin)
            {
                swap(&pSeq->arr[minindex], &pSeq->arr[begin]);//把最小的数放到最前面
            }
    
        }
    }
    //Findret BinarySearch(SeqList* pSeq, DataType x)
    //{
    //  size_t left, right, mid;
    //  Findret ret ;
    //  ret.Isfind = FALSE;
    //  left = 0;
    //  right = pSeq->size - 1;
    //  while (left <= right)
    //  {
    //      //mid = (left + right) / 2; 
    //      // 当left&right都大于无符号整形的一半的时候,相加就溢出了
    //      mid = left + (right - left) / 2; //or mid = left + (right - left) >> 1;
    //      if (pSeq->arr[mid] == x)
    //      {
    //          ret.Isfind = TRUE;
    //          ret.index = mid;
    //          return ret;
    //      }
    //      else if (pSeq->arr[mid] > x)
    //      {
    //          right = mid - 1;
    //      }
    //      else
    //      {
    //          left = mid + 1;
    //      }
    //  }
    //  return ret;
    //}
    
    Findret BinarySearch(SeqList* pSeq, size_t begin, size_t end, DataType x)//递归二分搜索
    {
        size_t mid = begin + (end - begin) / 2;
        Findret ret;
        ret.Isfind = FALSE;
        if (begin == end && pSeq->arr[begin] != x)//没找到x
        {
            return ret;
        }
        if (pSeq->arr[mid] > x)
        {
            BinarySearch(pSeq, begin, mid - 1, x);
        }
        else if (pSeq->arr[mid] < x)
        {
            BinarySearch(pSeq, mid + 1, end, x);
        }
        else //pSeq->arr[mid] = x 找到x
        {
            ret.Isfind = TRUE;
            ret.index = mid;
            return ret;
        }
    }
    
    void Destorycapacity(SeqList* pSeq)
    {
        assert(pSeq);
        free(pSeq->arr);
        pSeq->size = 0;
        pSeq->capacity = 0;
    }

    test.c

    #include"seqlist.h"
    
    void test()
    {
        SeqList s;
        InitSeqList(&s);
        PushBack(&s, 1);
        PushBack(&s, 2);
        PushBack(&s, 3);
        PushBack(&s, 4);
        PopBack(&s);
        PushFront(&s, 10);
        PopFront(&s);
        Insert(&s, 2, 10);
        Modified(&s, 3, 11);
        removed(&s, 1);
        PrintSeqList(&s);
    
    }
    
    void test2()
    {
            SeqList s;
            InitSeqList(&s);
            Insert(&s, 0, 0);
            Insert(&s, 1, 1);
            Insert(&s, 2, 2);
            Insert(&s, 3, 2);
            Insert(&s, 4, 3);
            Insert(&s, 5, 2);
            PrintSeqList(&s);
    
            Erase(&s, 2, FALSE);
            PrintSeqList(&s);
    
            Erase(&s, 2, TRUE);
            PrintSeqList(&s);
            Destorycapacity(&s);
    }
    void test3()
    {
        SeqList s;
        Findret ret;
        int x;
        InitSeqList(&s);
        Insert(&s, 0, 0);
        Insert(&s, 1, 1);
        Insert(&s, 2, 2);
        Insert(&s, 3, 2);
        Insert(&s, 4, 3);
        Insert(&s, 5, 2);
        BubbleSort(&s);
        //SelectSort(&s);
        PrintSeqList(&s);
        x = 1;
        ret = BinarySearch(&s,0,s.size - 1 ,1);
        if (ret.Isfind == TRUE)
        {
            printf("find %d,index=%d
    ", x, ret.index);
        }
        else
        {
            printf("not find
    ");
        }
    
    
    }
    
    int main()
    {
    
        test();
        test2();
        test3();
        getchar();
        return 0;
    }
  • 相关阅读:
    Excel VB Script
    Excel Text Converter as C# Format
    快捷键
    如何使用 MasterPage
    Excel 오른쪽버튼 윗주
    Oracle Hints
    ASP.NET 弹出窗口
    Log4Net
    word 修改 表宽度
    While 나가는 법
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5347786.html
Copyright © 2020-2023  润新知