静态顺序表的实现

什么是顺序表?
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
下面我们来实现一下静态顺序表
SeqList.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include <stdio.h>
#include <assert.h>
#include <string.h>

#define MAX 10

typedef int DataType;

typedef struct SeqList
{
    DataType data[MAX];
    int sz;
}SeqList, *pSeqList;  //pSeqList是SeqList*的重命名

void InitSeqList(pSeqList ps);             //初始化
void PushBack(pSeqList ps, DataType d);    //在尾部插入元素
void PopBack(pSeqList ps);                 //在尾部删除元素
void Display(const pSeqList ps);           //打印
void PushFront(pSeqList ps, DataType d);   //在头部插入一个元素
void PopFront(pSeqList ps);                //在头部删除一个元素
int Find(pSeqList ps, DataType d);               //查找
void Insert(pSeqList ps, DataType d, int pos);  //在指定位置插入元素
void Remove(pSeqList ps, DataType d);           //删除指定的某个元素
void RemoveAll(pSeqList ps, DataType d);        //删除所有指定的某个元素
void Reverse(pSeqList ps);                      //逆序
void Sort(pSeqList ps);                         //排序
int BinarySearch(pSeqList ps, DataType d);      //二分查找
#endif //__SEQLIST_H__
SeqList.c
#include "SeqList.h"

void InitSeqList(pSeqList ps)//初始化顺序表
{
    ps->sz = 0;
    memset(ps->data, 0, sizeof(ps->data));
}

void PushBack(pSeqList ps, DataType d)
{
    assert(ps != NULL);
    if(ps->sz == MAX)
    {
        return;
    }
    ps->data[ps->sz] = d;
    ps->sz++;
}

void PopBack(pSeqList ps)
{
    assert(ps != NULL);
    if(ps->sz == 0)
    {
        return;
    }
    ps->sz--;
}

void PushFront(pSeqList ps, DataType d)
{
    int i = 0;
    assert(ps != NULL);
    if(ps->sz < MAX)
    {
        //memmove(ps->data+1,ps->data,sizeof(DataType)*(ps->sz));
        for(i=ps->sz;i>0;i--)
        {
            ps->data[i] = ps->data[i-1];
        }
        ps->data[0] = d;
        ps->sz++;
    }
}

void PopFront(pSeqList ps)
{
    int i = 0;
    assert(ps != NULL);
    if(ps->sz == 0)
    {
        return;
    }
    for(i=0;i<ps->sz-1;i++)
    {
        ps->data[i] = ps->data[i+1];
    }
    ps->sz--;
}

int Find(pSeqList ps, DataType d)//遍历查找
{
    int i = 0;
    assert(ps != NULL);
    for(i=0; i<ps->sz; i++)
    {
        if(d == ps->data[i])
            return i;
    }
    return -1;
}

void Insert(pSeqList ps, DataType d, int pos)//在指定位置插入元素
{
    int i = 0;
    assert(ps != NULL);
    if(ps->sz == MAX)
    {
        return;
    }
    for(i=ps->sz;i>pos;i--)
    {
        ps->data[i] = ps->data[i-1];
    }
    ps->data[pos] = d;
    ps->sz++;
}

void Remove(pSeqList ps, DataType d) //删除指定元素
{
    int pos = 0;        
    assert(ps != NULL);
    if(ps->sz == 0);
    {
        return;
    }
    pos = Find(ps, d);
    if(pos != -1)
    {
        memmove(ps->data+pos, ps->data+pos+1, (ps->sz-pos-1)*sizeof(DataType));
        ps->sz--;
    }
}

void RemoveAll(pSeqList ps, DataType d)
{
    int pos = 0;      
    assert(ps != NULL);
    while(pos = Find(ps, d) != -1)
    {
        Remove(ps, d);
    }
}

void Reverse(pSeqList ps)   //逆序
{
    DataType *left = ps->data;
    DataType *right = ps->data + ps->sz - 1;
    assert(ps != NULL);
    while(left<right)
    {
        DataType tmp = *left;
        *left = *right;
        *right = tmp;
        *left++;
        *right--;
    }
}

void Sort(pSeqList ps)  //排序(冒泡法)
{
    int i = 0;
    int j = 0;
    assert(ps != NULL);
    for(i=0; i<ps->sz-1; i++)
    {
        for(j=0; j<ps->sz-1-i; j++)
        {
            if(ps->data[j] > ps->data[j+1])
            {
                DataType tmp = ps->data[j];
                ps->data[j] = ps->data[j+1];
                ps->data[j+1] = tmp;
            }

        }
    }
}

int BinarySearch(pSeqList ps, DataType d)  //二分查找(找到返回数字的下标,找不到返回-1)
{
    int left = 0;
    int right = ps->sz;
    int mid = left+((right - left)>>1);
    while(left <= right)
    {
        if(ps->data[mid]<d)
        {
            left = mid + 1;
        }
        else if(ps->data[mid]>d)
        {
            right = mid - 1;
        }
        else if(d == ps->data[mid])
        {
            return mid;
        }
    }
    return -1;
}

void Display(const pSeqList ps)
{
    int i = 0;
    assert(ps != NULL);
    for(i=0;i<ps->sz;i++)
    {
        printf("%d ",ps->data[i]);
    }
    printf("\n");
}

测试
test.c

#include "SeqList.h"

void test1()
{
    SeqList my_list;
    InitSeqList(&my_list);
    PushBack(&my_list, 1);
    PushBack(&my_list, 2);
    PushBack(&my_list, 3);
    PushBack(&my_list, 4);
    Display(&my_list);
    PopBack(&my_list);
    Display(&my_list);

}

void test2()
{
    SeqList my_list;
    InitSeqList(&my_list);
    PushFront(&my_list, 1);
    PushFront(&my_list, 2);
    PushFront(&my_list, 3);
    PushFront(&my_list, 4);
    Display(&my_list);
    PopFront(&my_list);
    Display(&my_list);
    PopFront(&my_list);
    Display(&my_list);
}

void test3()
{
    int pos = 0;
    SeqList my_list;
    InitSeqList(&my_list);
    PushFront(&my_list, 1);
    PushFront(&my_list, 2);
    PushFront(&my_list, 3);
    PushFront(&my_list, 4);

    pos = Find(&my_list, 1);
    if(pos == -1)
    {
        printf("要查找的元素不存在\n");
    }
    else
    {
        Insert(&my_list, 6, pos);
    }
    Display(&my_list);
}

void test4()
{
    int pos = 0;
    SeqList my_list;
    InitSeqList(&my_list);
    PushFront(&my_list, 1);
    PushFront(&my_list, 2);
    PushFront(&my_list, 3);
    PushFront(&my_list, 4);

    pos = Find(&my_list, 6);
    if(pos == -1)
    {
        printf("要查找的元素不存在\n");
    }
    else
    {
        Remove(&my_list,pos);
    }
    Display(&my_list);
}

void test5()
{
    int pos = 0;
    SeqList my_list;
    InitSeqList(&my_list);
    PushFront(&my_list, 3);
    PushFront(&my_list, 2);
    PushFront(&my_list, 3);
    PushFront(&my_list, 4);
    PushFront(&my_list, 3);
    PushFront(&my_list, 1);
    pos = Find(&my_list, 3);
    if(pos == -1)
    {
        printf("要查找的元素不存在\n");
    }
    else
    {
        RemoveAll(&my_list,pos);
    }
    Display(&my_list);
}

void test()
{
    int pos = 0;
    SeqList my_list;
    InitSeqList(&my_list);
    PushFront(&my_list, 3);
    PushFront(&my_list, 2);
    PushFront(&my_list, 3);
    PushFront(&my_list, 4);
    PushFront(&my_list, 3);
    PushFront(&my_list, 1);
    Display(&my_list);
    Reverse(&my_list);
    Display(&my_list);
    Sort(&my_list);
    Display(&my_list);
    pos = BinarySearch(&my_list, 4);
    printf("%d\n",pos);
}

int main()
{
    test();
    return 0;
}

静态顺序表的实现也许不太完美,希望大家能给我多多提意见,我会努力改进。
谢谢阅读!

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

博主推荐

换一批

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