顺序表静态与动态实现及基本的操作(增删查改等)

//seqlist.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef _SEQLIST_
#define _SEQLIST_
#define MAX_SIZE 8
typedef  int Datatype;
typedef struct seqlist//静态顺序表的声明
{
    Datatype array[MAX_SIZE];
    unsigned int size;//记录当前顺序表的有效长度

}seqlist;
void Initseqlist(seqlist* s);
void Printseqlist(seqlist* s);
void Pushback(seqlist* s, Datatype a);
void Popback(seqlist* s);
void Pushfront(seqlist* s, Datatype a);
void Popfront(seqlist* s);
void Insert(seqlist* s, unsigned int pos, Datatype a);
void Erase(seqlist* s, unsigned int pos);
int find(seqlist* s, Datatype a);
void Remove(seqlist* s, Datatype a);
void RemoveAll(seqlist* s, Datatype a);
void Bubblesort(seqlist* s);
void Selectesort(seqlist* s);
void Insertsort(seqlist* s);
int Binarysearch(seqlist* s,Datatype a);
#endif
//seqlist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Seqlist.h"
#include 
#include 
#include 
void Initseqlist(seqlist* s)//初始化顺序表
{
    assert(s);
    memset(s, 0, MAX_SIZE*sizeof(Datatype));
    s->size = 0;
}
void Printseqlist(seqlist* s)//打印顺序表
{
    assert(s);
    int i = 0;
    for (i = 0; i < (int)(s->size); i++)
    {
        printf("%d ", s->array[i]);
    }
    printf("\n");
}

void Pushback(seqlist* s, Datatype a)//尾插
{
    assert(s);
    if (s->size >= MAX_SIZE)
    {
        printf("the seqlist is full\n");
        return;
    }
    s->array[s->size] = a;
    s->size++;
}
void Popback(seqlist* s)//尾删
{
    assert(s);
    if (s->size == 0)
    {
        printf("the seqlist is empty\n");
        return;
    }
    s->size--;
}
void Pushfront(seqlist* s, Datatype a)
{
    assert(s);
    if (s->size >= MAX_SIZE)
    {
        printf("the seqlist is full\n");
        return;
    }
    int end = s->size - 1;
    while (end>=0)
    {
        s->array[end + 1] = s->array[end];
        end--;
    }
    s->array[0] = a;
    s->size++;
}
void Popfront(seqlist* s)
{
    assert(s);
    if (s->size == 0)
    {
        printf("list is empty!\n");
        return;
    }
    else
    {
        int i = 0;
        for (i = 0; i < (int)s->size-1; i++)
        {
            s->array[i] = s->array[i+1];
        }
        s->size--;
    }
}
void Insert(seqlist* s, unsigned int pos, Datatype a)//在指定位置插入数据
{
    assert(s);
    if (s->size >= MAX_SIZE)
    {
        printf("list is full\n");
        return;
    }
    unsigned int i = 0;
    for (i = (int)s->size-1; i>=pos-1; i--)
    {
        s->array[i + 1] = s->array[i];
    }
    s->array[pos-1] = a;
    s->size++;
}
int find(seqlist* s, Datatype a)
{
    assert(s);
    unsigned int i = 0;
    for (i = 0; i < s->size; i++)
    {
        if (s->array[i] == a)
        {
            return i;
        }
    }
    return -1;
}
void Erase(seqlist* s, unsigned int pos)
{
    assert(s);
    assert(pos < s->size);
    if (s->size== 0)
    {
        printf("list is empty!\n");
        return;
    }
    unsigned int i = 0;
    for (i = pos-1; i < s->size-1; i++)
    {
        s->array[i] = s->array[i + 1];
    }
    s->size--;
}
void Remove(seqlist* s, Datatype a)
{
    assert(s);
    int pos = find(s, a);
    if (s->size == 0)
    {
        printf("list is empty!\n");
        return;
    }
    if ( pos == -1)
    {
        printf("not exist the data\n");
        return;

    }
    int i = 0;
    for (i = pos ; i < (int)s->size - 1; i++)
    {
        s->array[i] = s->array[i + 1];
    }
    s->size--;
}
void RemoveAll(seqlist* s, Datatype a)
{
    assert(s);
    unsigned i = 0;
    unsigned count = 0;
    for (i = 0; i < s->size; i++)
    {
        if (s->array[i] == a)
        {
            count++;
        }
        else
        {
            s->array[i - count] = s->array[i];

        }
    }
    s->size -= count;
}
void Bubblesort(seqlist* s)
{
    assert(s);
    unsigned int i = 0;
    int flag = 0;
    for (i = 0; i < s->size-1; i++)
    {
        unsigned int j = 0;
        flag = 0;
        for (j = 0; j < s->size - i - 1; j++)
        {
            if (s->array[j]>s->array[j + 1])
            {
                Datatype temp = s->array[j];
                s->array[j] = s->array[j + 1];
                s->array[j + 1] = temp;
                flag = 1;
            }

        }
        if (!flag)
        {
            return;
        }
    }

}
void Selectesort(seqlist* s)
{
    assert(s);
    int min_index = 0;
    int max_index = 0;
    int i = 0;
    int start = 0;
    int end = (int)s->size - 1;
    while (start < end)
    {
        min_index = start;
        max_index = end;
        for (i = start; i<=end; i++)
        {
            if (s->array[i]array[min_index])
            {
                min_index = i;
            }
            if (s->array[i]>s->array[max_index])
            {
                max_index = i;
            }
        }
        Datatype temp1 = s->array[start];
        s->array[start] = s->array [min_index];
        s->array[min_index] = temp1;
        if (max_index == start)
        {
            max_index = min_index;
        }
        Datatype temp2 = s->array[end];
        s->array[end] = s->array[max_index];
        s->array[max_index] = temp2;
        end--;
        start++;

    }
}
void Insertsort(seqlist* s)
{
    assert(s);
    unsigned int i = 0;
    unsigned int end = 0;
    for (i = 0; i < s->size-1; i++)
    {
         end = i;
        Datatype temp = s->array[end + 1];
        while (end >= 0 && s->array[end]>temp)
        {
            s->array[end + 1] = s->array[end];
            end--;
        }
        s->array[end+1] = temp;
    }
}
int Binarysearch(seqlist* s,Datatype a)
{
    assert(s);
    int left = 0;
    int right = (int)s->size-1;
    int mid = 0;
    while (left <= right)
    {
        mid = left + ((right - left) >> 1);
        if (s->array[mid] > a)
        {
            right = mid - 1;
        }
        else if (s->array[mid] < a)
        {
            left = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}
//test.c
#include "Seqlist.h"
#include 
#include 
//void test1()//pushback,popback
//{
//  seqlist seq;
//  Initseqlist(&seq);
//  Pushback(&seq, 1);
//  Pushback(&seq, 2);
//  Pushback(&seq, 3);
//  Pushback(&seq, 4);
//  Pushback(&seq, 5);
//  Pushback(&seq, 6);
//  Printseqlist(&seq);
//  Popback(&seq);
//  Popback(&seq);
//  Popback(&seq);
//  Popback(&seq);
//  Popback(&seq);
//  Printseqlist(&seq);
//  Popback(&seq);
//  Printseqlist(&seq);
//}
void test2()//pushfront popfront
{
    seqlist seq;
    Initseqlist(&seq);
    Pushfront(&seq, 1);
    Pushfront(&seq, 2);
    Pushfront(&seq, 3);
    Pushfront(&seq, 4);
    Pushfront(&seq, 5);
    Pushfront(&seq, 6);
    Printseqlist(&seq);
    Popfront(&seq);
    Popfront(&seq);
    Popfront(&seq);
    Popfront(&seq);
    Popfront(&seq);
    Popfront(&seq);
    Printseqlist(&seq);
}
void test3()
{
    seqlist seq;
    Initseqlist(&seq);
    Pushfront(&seq, 3);
    Pushfront(&seq, 2);
    Pushfront(&seq, 3);
    Pushfront(&seq, 5);
    Pushfront(&seq, 5);
    Pushfront(&seq, 3);

    Printseqlist(&seq);
    //Insert(&seq, 3, 0);
    //Printseqlist(&seq);
    //Erase(&seq, 2);
    //Printseqlist(&seq);
    //Remove(&seq, 0);
    //Remove(&seq, 1);
    //Remove(&seq, 6);
    RemoveAll(&seq, 3);
    Printseqlist(&seq);
}
void test4()//sort
{
    seqlist seq;
    Initseqlist(&seq);
    Pushback(&seq, 1);
    Pushback(&seq, 2);
    Pushback(&seq, 4);
    Pushback(&seq, 4);
    Pushback(&seq, 7);
    Pushback(&seq, 8);
    Bubblesort(&seq);
    //Selectesort(&seq);
    //Insertsort(&seq);
    Printseqlist(&seq);

    int pos = Binarysearch(&seq, 7);
    if (pos == -1)
    {
        printf("not exists\n");
    }
    else
    {
        printf("%d\n", pos);
    }


}
int main()
{
    //test1();
    //test2();
    //test3();
    test4();
    system("pause");
    return 0;
}
//实现顺序表的动态增长
Dseqlist.h
#pragma once
#include 
#include 
#include 
#include 
typedef int Datatype;
typedef struct Dseqlist
{
    Datatype *array;
    size_t size;//有效长度
    size_t capacity;//顺序表的当前容量
}Dseqlist;
void InitDseqlist(Dseqlist* seq);
void Checkcapacity(Dseqlist* seq);
void Printseqlist(Dseqlist* seq);//打印顺序表
void Pushback(Dseqlist* seq, Datatype a);//尾插
void Pushfront(Dseqlist* seq, Datatype a);
void Destory(Dseqlist* seq);
//Dseqlist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Dseqlist.h"
void InitDseqlist(Dseqlist* seq)
{
    assert(seq);
    seq->array = (Datatype*)malloc(sizeof(Datatype)* 3);
    assert(seq->array);
    seq->size = 0;
    seq->capacity = 3;
//刚开始初始化容量为3
}
void Checkcapacity(Dseqlist* seq)
//检查顺序表是否溢出,并扩容
{
    if (seq->size >= seq->capacity)
    {
        seq->array = (Datatype*)realloc(seq->array,sizeof(Datatype)* seq->capacity*2);
        assert(seq->array);
        seq->capacity *= 2;
    }
}
void Printseqlist(Dseqlist* seq)//打印顺序表
{
    assert(seq);
    int i = 0;
    for (i = 0; i < (int)(seq->size); i++)
    {
        printf("%d ", seq->array[i]);
    }
    printf("\n");
}
void Pushback(Dseqlist* seq, Datatype a)//尾插
{
    assert(seq);
    Checkcapacity(seq);
    seq->array[seq->size] = a;
    seq->size++;
}
void Pushfront(Dseqlist* seq, Datatype a)
{
    assert(seq);
    Checkcapacity(seq);
    int end = seq->size - 1;
    while (end >= 0)
    {
        seq->array[end + 1] = seq->array[end];
        end--;
    }
    seq->array[0] = a;
    seq->size++;
}
void Destory(Dseqlist* seq)
{
    assert(seq);
    if (seq->array)
    {
        free(seq->array);
    }
    seq->array = NULL;
    seq->size = 0;
    seq->capacity = 0;
}

你可能感兴趣的:(c语言)