顺序表 增删查改

#include<stdio.h>
#include<windows.h>
#include<assert.h>
#include<stdlib.h>
#define len 3
typedef int Datatype;
typedef struct SeqList
{
    Datatype *a;
    size_t size;
    size_t capacity;
}SeqList;


void SeqInit(SeqList* pSeq)
{
    //初始化顺序表:分配内存,将已用长度置零,全长为len
    assert(pSeq);
    pSeq->a = (Datatype*)malloc(sizeof(Datatype)*len);
    pSeq->size = 0;
    pSeq->capacity = len;
}

void SeqPrint(SeqList* pSeq)
{
    //输出顺序表中每个数
    assert(pSeq);
    for (int i = 0;i<(int)pSeq->size;i++)
    {
        printf("%d ", pSeq->a[i]);
    }
}
void SeqDestory(SeqList* pSeq)
{
    //销毁顺序表:free掉头数据区头指针,置为空,在把已用长度和总长置零
    assert(pSeq);
    free(pSeq->a);
    pSeq->a = NULL;
    pSeq->capacity = 0;
    pSeq->size = 0;
}
void Seqcheckfull(SeqList **pSeq)
{
    //检查顺序表是否满
    if ((*pSeq)->size == (*pSeq)->capacity)
    {
        (*pSeq)->a = (Datatype*)realloc((*pSeq)->a, sizeof(Datatype)*len * 2);
        (*pSeq)->capacity *= 2;
    }
}
void SeqPushBack(SeqList* pSeq, Datatype x)
{
    //判断是否满,满则用realloc多分配一倍内存。未满将数据存在最后,size++
    assert(pSeq);
    Seqcheckfull(&pSeq);
    pSeq->a[pSeq->size] = x;
    pSeq->size++;

}
void SeqPopBack(SeqList* pSeq)
{
    //判断顺序表是否为空,空则报错返回,未空则size--
    assert(pSeq);
    if (pSeq->size == 0)
    {
        printf("SeqList is empty!");
        return;
    }
    pSeq->size--;
}
void SeqPushFront(SeqList* pSeq, Datatype x)
{
    //判断顺序表是否满,满则多分配一倍内存,未满则从最后一个数据到第一个数据,依次向后挪一,size++
    assert(pSeq);
    Seqcheckfull(&pSeq);
    for (int i = pSeq->size - 1;i >= 0;i--)
    {
        pSeq->a[i + 1] = pSeq->a[i];
    }
    pSeq->a[0] = x;
    pSeq->size++;
}

void SeqPopFront(SeqList* pSeq)
{
    //判断顺序表是否为空,空则报错返回,未空则令第二个数据到最后一个数据,依次向前挪一位,size--
    assert(pSeq);
    if (pSeq->size == 0)
    {
        printf("seqlist is empty!");
        return;
    }
    for (int i = 1;i < (int)pSeq->size;i++)
    {
        pSeq->a[i - 1] = pSeq->a[i];
    }
    pSeq->size--;
}

void SeqInsert(SeqList* pSeq, size_t pos, Datatype x)
{
    //判断顺序表是否已满,将最后一个数据至第pos个数据依次向后挪,在将x插入pos处
    assert(pSeq);
    assert(pos < pSeq->size);
    Seqcheckfull(&pSeq);
    for (int i = pSeq->size - 1;i >= (int)pos;i--)
    {
        pSeq->a[i + 1] = pSeq->a[i];
    }
    pSeq->a[pos] = x;
    pSeq->size++;

}
void SeqErase(SeqList* pSeq, size_t pos)
{
    //判断顺序表是否为空,未空则判断是否是最后一个数据,是则直接令size--,不是则令第pos+1个至最后一个数向前挪,再pos--
    assert(pSeq);
    if (pSeq->size == 0)
    {
        printf("seqlist is empty!");
        return;
    }
    if (pos == pSeq->size - 1)
    {
        pSeq->size--;
    }
    else
    {
        for (int i = pos + 1;i < (int)pSeq->size;i++)
        {
            pSeq->a[i - 1] = pSeq->a[i];
        }
        pSeq->size--;
    }
}
int SeqFind(SeqList* pSeq, Datatype x)
{
    //遍历顺序表,查找与x值相同的数据的下标并返回,未找到返回-1
    assert(pSeq);
    for (int i = 0;i < (int)pSeq->size;i++)
    {
        if (pSeq->a[i] == x)
        {
            return i;
        }
    }
    return -1;
}
void SeqAt(SeqList* pSeq, size_t pos, Datatype x)
{
    //现在顺序表中找第pos项,然后替换
    assert(pSeq&&pos < pSeq->size);
    pSeq->a[pos] = x;

}

void BubbleSort(SeqList* pSeq)
{
    assert(pSeq);
    Datatype tmp = 0;
    for (int i = 0;i <(int)pSeq->size;i++)
    {
        int flag = 1;

        for (int j = 0;j < (int)pSeq->size - i - 1;j++)
        {
            if (pSeq->a[j] > pSeq->a[j + 1])
            {
                tmp = pSeq->a[j];
                pSeq->a[j] = pSeq->a[j + 1];
                pSeq->a[j + 1] = tmp;
                flag = 0;
            }
        }
        if (flag == 1)
        {
            break;
        }
    }
}

//void SelectSort(SeqList* pSeq)
//{
//  assert(pSeq);
//  int num = 0;
//  int tmp = 0;
//  for (int i = 0;i < (int)pSeq->size-1;i++)
//  {
//      for (int j = 0;j < (int)pSeq->size - i - 1;j++)
//      {
//          if (pSeq->a[j] > pSeq->a[j + 1])
//          {
//              num = j;
//          }
//      }
//      if (pSeq->a[num] != pSeq->a[pSeq->size - i - 1])
//      {
//          tmp = pSeq->a[num];
//          pSeq->a[num] =pSeq->a[pSeq->size - i - 1] ;
//          pSeq->a[pSeq->size - i - 1] = tmp;
//      }
//  }
//}
void SelectSort(SeqList* pSeq)
{
    //每遍遍历确定最大及最小
    assert(pSeq);

    size_t min = 0;
    size_t max = 0;
    size_t left = 0;
    size_t right = pSeq->size - 1;
    while (left<right)
    {
        for (size_t i = left; i <= right; i++)
        {
            if (pSeq->a[min]>pSeq->a[i])
            {
                min = i;
            }
            if (pSeq->a[max] < pSeq->a[i])
            {
                max = i;
            }
        }
        Datatype x = pSeq->a[min];
        pSeq->a[min] = pSeq->a[left];
        pSeq->a[left] = x;
        if (max == left)
        {
            max = min;
        }
        x = pSeq->a[max];
        pSeq->a[max] = pSeq->a[right];
        pSeq->a[right] = x;
        left++;
        right--;
    }

}
int BinarySearch(SeqList* pSeq,Datatype x)
{
    //二分查找,先看中间,在决定边界改变
    assert(pSeq);
    size_t low = 0;
    size_t high = (int)pSeq->size - 1;
    while (low <= high)
    {
        size_t mid = low + ((high - low) >> 1);
        if (pSeq->a[mid] == x)
        {
            return mid;
        }
        else if (x < pSeq->a[mid])
        {
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    return -1;
}

猜你喜欢

转载自blog.csdn.net/ferlan/article/details/79605249