溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 線性表的基本功能實(shí)現(xiàn)

發(fā)布時(shí)間:2020-04-05 16:20:05 來(lái)源:網(wǎng)絡(luò) 閱讀:344 作者:sonissa 欄目:編程語(yǔ)言

頭文件如下

#ifndef _SEQLIST_H_
#define _SEQLIST_H_

// 順序表的動(dòng)態(tài)存儲(chǔ)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int SLDataType;

typedef struct SeqList {
    SLDataType* array;  // 指向動(dòng)態(tài)開(kāi)辟的數(shù)組
    size_t size;       // 有效數(shù)據(jù)個(gè)數(shù)
    size_t capacity;   // 容量空間的大小
}SeqList;

// 基本增刪查改接口
void SeqListInit(SeqList* psl, size_t capacity);
void SeqListDestory(SeqList* psl);

void CheckCapacity(SeqList* psl);
void SeqListPushBack(SeqList* psl, SLDataType x);
void SeqListPopBack(SeqList* psl);
void SeqListPushFront(SeqList* psl, SLDataType x);
void SeqListPopFront(SeqList* psl);

int SeqListFind(SeqList* psl, SLDataType x);
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
void SeqListErase(SeqList* psl, size_t pos);
void SeqListRemove(SeqList* psl, SLDataType x);
void SeqListModify(SeqList* psl, size_t pos, SLDataType x);
void SeqListPrint(SeqList* psl);

void SeqListBubbleSort(SeqList* psl);
int SeqListBinaryFind(SeqList* psl, SLDataType x);
void SeqListRemoveAll(SeqList* psl, SLDataType x);

#endif /*_SEQLIST_H_*/

以下是具體功能實(shí)現(xiàn)

void SeqListInit(SeqList* psl, size_t capacity)//初始化
{
    psl->array = (SLDataType*)calloc(capacity, sizeof(SLDataType));
    psl->capacity = capacity;
    psl->size = 0;
}
void SeqListDestory(SeqList* psl)
{
    if (psl->array)
    {
        free(psl->array);
        psl->array = NULL;
        psl->capacity = 0;
        psl->size = 0;
    }
}

void CheckCapacity(SeqList* psl)//檢查和動(dòng)態(tài)開(kāi)辟
{
    if (psl->size >= psl->capacity)
    {
        psl->capacity *= 2;
        psl->array = (SLDataType*)realloc(psl->array, sizeof(SLDataType)*psl->capacity);
    }
}
void SeqListPushBack(SeqList* psl, SLDataType x)//尾插
{
    CheckCapacity(psl);
    psl->array[psl->size] = x;
    psl->size++;
}
void SeqListPopBack(SeqList* psl)//尾刪
{
    psl->size--;
}
void SeqListPushFront(SeqList* psl, SLDataType x)//首插
{
    int i;
    CheckCapacity(psl);
    for (i = psl->size - 1; i >= 0; i--)
    {
        psl->array[i+1] = psl->array[i];

    }
    psl->array[0] = x;
    psl->size++;
}
void SeqListPopFront(SeqList* psl)//首刪
{
    size_t i;
    for (i = 0; i < psl->size-1; i++)
    {
        psl->array[i] = psl->array[i + 1];
    }
    psl->size--;
}

int SeqListFind(SeqList* psl, SLDataType x)//查詢(xún)
{
    size_t i;
    for (i = 0; i < psl->size - 1; i++)
    {
        if (psl->array[i] == x)
        {
            return i;
        }
    }
    return -1;
}
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)//插入
{
    size_t i;
    for (i=psl->size-1; i>=pos; i--)
    {
        psl->array[i+1] = psl->array[i];
    }
    psl->array[pos] = x;
    psl->size++;
}
void SeqListErase(SeqList* psl, size_t pos)//刪除指定下標(biāo)
{
    size_t i;
    for (i = pos; i < psl->size - 1; i++)
    {
        psl->array[i] = psl->array[i + 1];
    }
    psl->size--;
}
void SeqListRemove(SeqList* psl, SLDataType x)//篩選刪除
{
    SeqListErase(psl, SeqListFind(psl, x));
}
void SeqListModify(SeqList* psl, size_t pos, SLDataType x)
{
    psl->array[pos] = x;
}
void SeqListPrint(SeqList* psl)
{
    size_t i;
    for (i = 0; i <=psl->size - 1; i++)
    {
        if (psl->array == NULL)
        {
            printf("NULL");
            return;
        }
        printf("%d->", psl->array[i]);
    }
    putchar('\n');
}

void SeqListBubbleSort(SeqList* psl)
{
    size_t i, j;
    SLDataType tmp;
    for (i = 0; i < psl->size - 1; i++)
    {
        for (j = 0; j < psl->size - 1; j++)
        {
            if (psl->array[j] > psl->array[j + 1])
            {
                tmp = psl->array[j];
                psl->array[j] = psl->array[j + 1];
                psl->array[j + 1] = tmp;
            }
        }
    }
}
int SeqListBinaryFind(SeqList* psl, SLDataType x)
{
    int left = 0, right = psl->size;
    int mid = (left + right) / 2;
    while (left <= right)
    {
        if (x < psl->array[mid])
        {
            right = mid-1;
        }
        if (x>psl->array[mid])
        {
            left = mid+1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}
void SeqListRemoveAll(SeqList* psl, SLDataType x)
{
    size_t i, gap = 0;
    for (i = 0; i < psl->size; i++)
    {
        if (psl->array[i] == x)
        {
            gap++;
        }
        else
        {
            psl->array[i - gap] = psl->array[i];
        }
    }
    psl->size -= gap;
}
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI