c语言 顺序表

头文件
#ifndef SEQ_LIST
#define SEQ_LIST
#include <stdio.h>


typedef int DataType;
#define  MAX_SIZE  10


typedef struct SeqList
{
    DataType arry[MAX_SIZE];
    size_t Size;
}SeqList, *PSeqList;
#endif
源文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "SeqList.h"

// 初始化顺序表
void InitSeqList(SeqList* seqList)
{
    /*for (int iBegin = 0; iBegin < MAX_SIZE; iBegin++)
        seqList->arry[iBegin] = 0;*/
    memset(seqList->arry,0,MAX_SIZE*sizeof(DataType));
    seqList->Size = 0;
}

// 在顺序表的尾部插入元素data
void PushBack(SeqList* seqList, DataType data)
{
    assert(seqList);
    if (seqList->Size > MAX_SIZE)
    {
        printf("顺序表中已满,请删除后再插入元素");
        return;
    }
    seqList->arry[seqList->Size] = data;
    seqList->Size++;
}

// 将顺序表尾部的元素抛出
void PopBack(SeqList* seqList)
{
    assert(seqList);
    if (seqList->Size == 0)
    {
        printf("顺序表为空,请插入元素后再删除");
        return;
    }
    seqList->Size--;
}

// 顺序表的头部插入元素data
void PushFront(SeqList* pSeqList, DataType data)
{
    int iBegin = 0;
    assert(pSeqList);
    if (pSeqList->Size == MAX_SIZE)
    {
        printf("顺序表已满,现插入会覆盖最后一个数据");
        for (iBegin = pSeqList->Size - 1 ; iBegin > 0; iBegin--)
        {
            pSeqList->arry[iBegin] = pSeqList->arry[iBegin - 1];
        }
        pSeqList->arry[0] = data; 
        return;
    }
    else
    {
        for (iBegin = pSeqList->Size ; iBegin >= 0; iBegin--)
        {
            pSeqList->arry[iBegin + 1] = pSeqList->arry[iBegin ];
        }
        pSeqList->arry[0] = data;
    }
    pSeqList->Size++;
}

// 顺序表的头部的元素删除
void PopFront(SeqList* pSeqList)
{
    int iBegin = 0;
    assert(pSeqList);
    if (pSeqList->Size == 0)
    {
        printf("该顺序表为空,请插入元素后再删除元素");
        return;
    }
    for (; iBegin <= pSeqList->Size - 1; iBegin++)
    {
        pSeqList->arry[iBegin] = pSeqList->arry[iBegin + 1];
    }
    pSeqList->Size--;
}

// 顺序表中查找数据data,返回该元素在顺序表中的位置
int Find(SeqList* pSeqList, DataType data)
{
    int ipos = 0;
    //int iBegin = 0;
    assert(pSeqList);
    if (pSeqList->Size == 0)
    {
        printf("该顺序表为空");
        return 0;
    }
    for (; ipos < pSeqList->Size - 1; ipos++)
    {
        if (pSeqList->arry[ipos] == data)
        {
            return (ipos + 1);
        }
    }
    printf("没有找到该元素");
    return 0;
}

// 在顺序表的ipos位置上插入元素data
void Insert(SeqList* pSeqList, size_t pos, DataType data)
{
    int iBegin = 0;
    assert(pSeqList);
    if (pos > MAX_SIZE)
    {
        printf("所输入位置超出了顺序表范围,请重新输入\n");
        return;
    }
    if (pSeqList->Size == MAX_SIZE)
    {
        printf("顺序表元素已满,插入操作会丢失一个元素\n");
        for (iBegin = pSeqList->Size - 1; iBegin >= pos - 1; iBegin--)
        {
            pSeqList->arry[iBegin + 1] = pSeqList->arry[iBegin];
        }
        pSeqList->arry[pos - 1] = data;
        return;
    }
    else if ( pos <= pSeqList->Size )
    {
        for (iBegin = pSeqList->Size ; iBegin > pos - 1; iBegin--)
        {
            pSeqList->arry[iBegin ] = pSeqList->arry[iBegin - 1];
        }
        pSeqList->arry[pos - 1] = data;
    }
    else
    {
        printf("输入位置不合法\n");
        return;
    }
    pSeqList->Size++;
}

// 删除顺序表pos位置上的元素
void Erase(SeqList* pSeqList, size_t pos)
{
    int iBegin = 0;
    assert(pSeqList);
    if (pos > pSeqList->Size)
    {
        printf("输入位置超过顺序表范围\n");
        return;
    }
    if (pSeqList->Size == 0)
    {
        printf("顺序表为空,请先插入元素再删除元素\n");
        return;
    }
    for (iBegin = pos - 1; iBegin < pSeqList->Size; iBegin++)
    {
        pSeqList->arry[iBegin] = pSeqList->arry[iBegin + 1];
    }
    pSeqList->Size--;
}

// 移除顺序表中的元素data
void Remove(SeqList* pSeqList, DataType data)
{
    int iBegin = 0;
    int ipos = 0;
    assert(pSeqList);
    if (pSeqList->Size == 0)
    {
        printf("顺序表为空,请先插入元素再删除元素\n");
        return;
    }
    for (; iBegin < pSeqList->Size; iBegin++)
    {
        if (pSeqList->arry[iBegin] == data)
        {
            for (ipos = iBegin; ipos < pSeqList->Size; ipos++)
            {
                pSeqList->arry[ipos] = pSeqList->arry[ipos + 1];
            }
            pSeqList->Size--;
            return;
        }
    }
    printf("该顺序表中没有值为%d的元素", data);
}

// 移除顺序表中的所有值为data的元素
void RemoveAll(SeqList* pSeqList, DataType data)
{
    int iBegin = 0;
    int iCount = 0;
    assert(pSeqList);
    if (pSeqList->Size == 0)
    {
        printf("顺序表为空,请先插入元素再删除元素\n");
        return;
    }
    for (; iBegin < pSeqList->Size; iBegin++)
    {
        if (pSeqList->arry[iBegin] == data)
        {
            iCount++;
        }
        else
        {
            if (iCount == 0)
            {

            }
            else
            {
                pSeqList->arry[iBegin - iCount] = pSeqList->arry[iBegin];
            }
        }
    }
    pSeqList->Size -= iCount;
}

// 选择排序
void SelectSort(SeqList* pSeqList)
{
    int iIn = 0;
    int iOut = 0;
    int iCount = 0;
    int iMaxPos = 0;
    int iMinPos = 0;
    int tempMax = pSeqList->arry[0];
    int tempMin = pSeqList->arry[0];

    for (; iOut < pSeqList->Size/2 + 1; iOut++)
    {
        tempMax = pSeqList->arry[iCount];
        tempMin = pSeqList->arry[iCount];
        for (iIn = iCount; iIn < pSeqList->Size - iCount; iIn++)
        {
            if (pSeqList->arry[iIn] >= tempMax)
            {
                tempMax = pSeqList->arry[iIn];
                iMaxPos = iIn;
            }
            if (pSeqList->arry[iIn] <= tempMin)
            {
                tempMin = pSeqList->arry[iIn];
                iMinPos = iIn;
            }
        }
        tempMin = pSeqList->arry[iCount];
        pSeqList->arry[iCount] = pSeqList->arry[iMinPos];
        pSeqList->arry[iMinPos] = tempMin;
        tempMax = pSeqList->arry[pSeqList->Size - iCount - 1];
        pSeqList->arry[pSeqList->Size - iCount - 1 ] = pSeqList->arry[iMaxPos];
        pSeqList ->arry[iMaxPos] = tempMax;
        iCount++;
    }
}

// 冒泡排序
void BorbbleSort(SeqList* pSeqList)
{
    int iIn = 0;
    int iOut = 0;
    int temp = 0;
    int iCount = 0;
    int iFlag = 0;
    for (; iOut < pSeqList->Size; iOut++)
    {
        iFlag = 0;
        iCount++;
        for (iIn = 0; iIn < pSeqList->Size - iCount; iIn++)
        {
            if (pSeqList->arry[iIn] > pSeqList->arry[iIn + 1])
            {
                temp = pSeqList->arry[iIn];
                pSeqList->arry[iIn] = pSeqList->arry[iIn + 1];
                pSeqList->arry[iIn + 1] = temp;
                iFlag = 1;
            }
            else;
        }
        if (iFlag == 0)
            return;
    }
}

// 查找已序顺序表中的元素data
//升序
int BinarySearch(SeqList* pSeqList, DataType data)
{
    int iBegin = 0;
    int iLeft = 0;
    int iRight = pSeqList->Size ;
    int iMid = 0;
    int iFlag = 1;
    assert(pSeqList);
    if (pSeqList->Size == 0)
    {
        printf("该顺序表为空\n");
        return;
    }
    if ( data > pSeqList->arry[iRight - 1] )
        {
            printf("没有这个元素");
            return -1;
        }
    while ( iFlag )
    {
        iMid = iLeft + (iRight - iLeft) / 2;    //会考数据溢出
        //当ileft,iright很大时,(ileft+iright)/2会溢出int范围
        if (pSeqList->arry[iMid] > data)
            iRight = iMid + 1;
        else if (pSeqList->arry[iMid] < data)
            iLeft = iMid ;
        else
        {
            iFlag = 0;
            return iMid + 1;
        }
    }
    return 0;
}

// 打印顺序表
void PrintSeqList(SeqList* pSeqList)
{
    int iBegin = 0;
    assert(pSeqList);
    if (pSeqList->Size == 0)
    {
        printf("顺序表为空\n");
        return;
    }
    for (; iBegin < pSeqList->Size; iBegin++)
    {
        printf("%d \t", pSeqList->arry[iBegin]);
    }
    printf("\n");
}
int main()
{
    void InitSeqList(seqList);
	void PrintSeqList( pSeqList);
	return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页