数据结构 顺序表的简单排列

初始化

打印顺序表

尾插一个元素

只删除第一个指定元素

删除所有指定元素

判断顺序表是否为空  为空返回1 不为空返回0 

冒泡排序

#pragma once
#include 
#define SeqListMax 1000
#define TITLE printf("\n==============%s==============\n",__FUNCTION__);


typedef char SeqType;
typedef struct SeqList{
    SeqType data[SeqListMax];
    size_t size;
}SeqList;


void SeqListInit(SeqList* seq);  //初始化
void SeqListPrint(SeqList *seq);//打印顺序表
void SeqListPushBack(SeqList* seq, SeqType value); //尾插一个元素
void SeqListRemove(SeqList* seq, SeqType value); //只删除第一个指定元素
void SeqListRemoveAll(SeqList* seq, SeqType value);//删除所有指定元素
void SeqListBubbleSort(SeqList* seq); //冒泡排序
size_t SeqListSize(SeqList* seq); //判定顺序表是否为空
//void SeqListBubbleSortEx(SeqList* seq);//选择排序
#include "seqlist.h"



void SeqListInit(SeqList* seq)
{
    if(seq==NULL)
    return;
    seq->size=0;
}

void SeqListPrint(SeqList* seq)
{
    if(seq==NULL)
    return;
    size_t i = 0;
    for(;isize;i++)
    {
        printf("[%c] ",seq->data[i]);
    }
    printf("\n");
}

void SeqListPushBack(SeqList* seq, SeqType value)
{
    if(seq==NULL)
    return;
    if(seq->size>=SeqListMax)
    {
        printf("顺序表已满!\n");
        return;
    }
    seq->data[seq->size]=value;
    seq->size++;
}

void SeqListRemove(SeqList* seq, SeqType value)
{
    if(seq==NULL)
    return;
    if(seq->size==0)
    //顺序表为空
    return;
    int i = 0;
    for(;isize;i++)
    {
        if(seq->data[i]==value)
        break;
    }
    for(;isize;i++)
    {
        seq->data[i]=seq->data[i+1];
    }
}

void SeqListRemoveAll(SeqList* seq, SeqType value)
{
    if(seq==NULL)
    return;
    if(seq->size==0)
    //空顺序表
    return;
    int i = 0;
    int flag = 0;
    for(;isize;i++)
    {
        if(seq->data[i]==value)
        flag++;
    }
    for(i=0;isize==0)
    return 1;
    else
    return 0;
}

void swap(SeqType* a,SeqType* b)
{
    SeqType x;
    x = *a;
    *a = *b;
    *b = x;
}

void SeqListBubbleSort(SeqList* seq)
{
    if(seq==NULL)
    return;
    int i = 0;
    for(;isize;i++)
    {
        int j = 0;
        for(;jsize-1-i;j++)
        {
            if(seq->data[j]>seq->data[j+1])
            swap(&seq->data[j],&seq->data[j+1]);
        }
    }
}

//             以下是测试代码             //
///

void TestSeqListInit()
{
    TITLE;
    SeqList seq;
    SeqListInit(&seq);
}

void TestSeqListPushBack()
{
    TITLE;
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushBack(&seq,'a');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'c');
    SeqListPushBack(&seq,'d');
    SeqListPrint(&seq);
}

void TestSeqListRemove()
{
    TITLE;
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushBack(&seq,'a');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'c');
    SeqListPushBack(&seq,'d');
    SeqListRemove(&seq,'b');
    SeqListPrint(&seq);
}

void TestSeqListRemoveAll()
{
    TITLE;
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushBack(&seq,'a');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'c');
    SeqListPushBack(&seq,'d');
    SeqListRemoveAll(&seq,'b');
    SeqListPrint(&seq);
}

void TestSeqListSize()
{
    TITLE;
    int i = 0;
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushBack(&seq,'a');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'c');
    SeqListPushBack(&seq,'d');
    i = SeqListSize(&seq);
    printf("[%d]\n",i);
    SeqListInit(&seq);
    i = SeqListSize(&seq);
    printf("[%d]\n",i);
}

TestSeqListBubbleSort()
{
    TITLE;
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushBack(&seq,'d');
    SeqListPushBack(&seq,'b');
    SeqListPushBack(&seq,'c');
    SeqListPushBack(&seq,'a');
    SeqListPrint(&seq);
    SeqListBubbleSort(&seq);
    SeqListPrint(&seq);
}

int main()
{
    TestSeqListInit();
    TestSeqListPushBack();
    TestSeqListRemove();
    TestSeqListRemoveAll();
    TestSeqListSize();
    TestSeqListBubbleSort();
    return 0;
}




你可能感兴趣的:(数据结构)