码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构—顺序表的实现

时间:2016-05-27 11:33:18      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

线性表的顺序存储又称为顺序表。它是用一组地址连续的存储单元,依次存储线性表中的数据元素,从而使得逻辑上相邻的两个元素在物理位置上也相邻。第1个元素存储在线性表的起始位置,第i个元素的存储位置后面紧接着存储的是第i+1个元素。因此,顺序表的特点是表中元素的逻辑顺序与其物理顺序相同。

所以在这里我们实现一下顺序表!

seqlist.h

#define _CRT_SECURE_NO_WARNINGS 1


#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#define MAX_SIZE 100 //顺序表最大大小

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

typedef int datatype;
typedef struct seqlist
{
    datatype data[MAX_SIZE];
    int sz;
}Seqlist,*pSeqlist;


static enum seq
{
    EXIT,
    INIT,
    PUSHBACK,
    POPBACK,
    PUSHFRONT,
    POPFRONT,
    INSERT,
    REMOVE,
    REMOVEALL,
    SORT,
    BINARYSEARCH,
    PRINTFSEQLIST

};

void PrintSeqlist(pSeqlist pSeq);
void InitSqlist(pSeqlist pSeq);
void PushBack(pSeqlist pSeq, datatype x);
void PopBack(pSeqlist pSeq);
void PushFront(pSeqlist pSeq,datatype x);
void PopFront(pSeqlist pSeq);
void Insert(pSeqlist pSeq,int pos,datatype x);
void Remove(pSeqlist pSeq,datatype x);
void RemoveAll(pSeqlist pSeq,datatype x);
void Sort(pSeqlist pSeq);
int BinarySearch(pSeqlist pSeq,datatype x);

void meau();

void Exit_seq();
#endif //!__SEQLIST_H__

seqlist.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"seqlist.h"


void meau()
{
    printf("$$$$$$$$$$$$$$   SEQLIST  $$$$$$$$$$$$\n");
	printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    printf("$$$  1.init       2.push_back      $$$\n");
    printf("$$$  3.pop_back   4.push_front     $$$\n");
    printf("$$$  5.pop_front  6.insert         $$$\n");
    printf("$$$  7.remove     8.removeall      $$$\n");
    printf("$$$  9.sort       10.binary_search $$$\n");
    printf("$$$  11.print     12.EXIT          $$$\n");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");

}
//打印顺序表
void PrintSeqlist(pSeqlist pSeq)
{
    int i = 0;
    for (i = 0; i < pSeq->sz; i++)
    {
        printf("%3d", pSeq->data[i]);
    }
    printf("\n");
}
//初始化
void InitSqlist(pSeqlist pSeq)
{
    pSeq->sz = 0;
    memset(pSeq->data, 0, sizeof(datatype)*MAX_SIZE);
}
//尾插
void PushBack(pSeqlist pSeq, datatype x)
{
    if ((pSeq->sz) >= MAX_SIZE)
    {
        printf("顺序表已经满了。\n");
        return;
    }
    pSeq->data[pSeq->sz] = x;
    pSeq->sz++;
}
//尾出
void PopBack(pSeqlist pSeq)
{
    if (pSeq->sz <= 0)
    {
        printf("顺序表已空\n");
        return;
    }
    pSeq->sz--;
}
//头插
void PushFront(pSeqlist pSeq, datatype x)
{
    if ((pSeq->sz) >= MAX_SIZE)
    {
        printf("顺序表已经满了。\n");
        return;
    }

    for (int i = 0; i < pSeq->sz; i++)
    {
        pSeq->data[i + 1] = pSeq->data[i];
    }
    pSeq->data[0] = x;
    pSeq->sz++;
}
//头出
void PopFront(pSeqlist pSeq)
{
    if (pSeq->sz <= 0)
    {
        printf("顺序表已空\n");
        return;
    }
    for (int i = 0; i < pSeq->sz; i++)
    {
        pSeq->data[i] = pSeq->data[i + 1];
    }
    pSeq->sz--;
}
//插入
void Insert(pSeqlist pSeq, int pos, datatype x)
{
    if ((pSeq->sz) >= MAX_SIZE)
    {
        printf("顺序表已经满了。\n");
        return;
    }
    for (int i = pos; i < pSeq->sz; i++)
    {
        pSeq->data[i + 1] = pSeq->data[i];
    }
    pSeq->data[pos] = x;
    pSeq->sz++;
}
//删除第一个元素为x的元素
void Remove(pSeqlist pSeq, datatype x)
{
    if (pSeq->sz <= 0)
    {
        printf("顺序表已空,无法删除\n");
        return;
    }
    for (int i = 0; i < pSeq->sz; i++)
    {
        if (pSeq->data[i] == x)
        {
            for (int j = i; j < pSeq->sz - i; j++)
            {
                pSeq->data[j] = pSeq->data[j + 1];
            }
            pSeq->sz--;
            return;
        }
    }

}
//删除所有元素为x的元素
void RemoveAll(pSeqlist pSeq, datatype x)
{
    if (pSeq->sz <= 0)
    {
        printf("顺序表已空,无法删除\n");
        return;
    }
    for (int i = 0; i < pSeq->sz; i++)
    {
        if (pSeq->data[i] == x)
        {
            for (int j = i; j < pSeq->sz ; j++)
            {
                pSeq->data[j] = pSeq->data[j + 1];
            }
            pSeq->sz--;
        }
    }
}
//排序
void Sort(pSeqlist pSeq)
{

    char ch = 0;
    int i = 0;
    int j = 0;
    if (pSeq->sz <= 0)
    {
        printf("顺序表已空,无法排序\n");
        return;
    }
    printf("请选择排序类型(>:从大到小),(<:从小到大):\n");
    fflush(stdin);
    scanf("%c", &ch);

    switch (ch)
    {
    case‘>‘:
        for (i = 0; i < pSeq->sz-1; i++)
        {
            for (j = 0; j < pSeq->sz - i - 1; j++)
            {
                if (pSeq->data[j] < pSeq->data[j + 1])
                {
                    int tmp = pSeq->data[j];
                    pSeq->data[j] = pSeq->data[j + 1];
                    pSeq->data[j + 1] = tmp;
                }
            }
        }
        break;
    case‘<‘:
        for (i = 0; i < pSeq->sz - 1; i++)
        {
            for (j = i; j < pSeq->sz - i - 1; j++)
            {
                if (pSeq->data[j] > pSeq->data[j + 1])
                {
                    int tmp = pSeq->data[j];
                    pSeq->data[j] = pSeq->data[j + 1];
                    pSeq->data[j + 1] = tmp;
                }
            }
        }
        break;
    default:
        printf("所给出排序类型有问题!\n");
        break;
    }
}
//二分查找
int BinarySearch(pSeqlist pSeq, datatype x)
{
    int left = 0;
    int right = pSeq->sz - 1;
    int mid = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (pSeq->data[mid] < x)
        {
            left = mid + 1;
        }
        else if (pSeq->data[mid]>x)
        {
            right = mid - 1;
        }
        else
            return mid;
    }
    return -1;

}


test.c

#define _CRT_SECURE_NO_WARNINGS 1


#include"seqlist.h"
void Test()
{
    Seqlist seq;
    int input = 1;
    int x = 0;
    int pos = 0;
    int search = 0;
    InitSqlist(&seq);
    while (input)
    {
        meau();
        printf("请选择:");
        scanf("%d", &input);
        fflush(stdin);
        switch (input)
        {
        case INIT:
            InitSqlist(&seq);
            break;
        case PUSHBACK:
            fflush(stdin);
            printf("请输入你所要尾部push的元素:\n");
            scanf("%d", &x);
            PushBack(&seq, x);
            break;
        case POPBACK:
            PopBack(&seq);
            break;
        case PUSHFRONT:
            fflush(stdin);
            printf("请输入你所要头部push的元素:\n");
            scanf("%d", &x);
            PushFront(&seq, x);
            break;
        case POPFRONT:
            PopFront(&seq);
            break;
        case INSERT:
            fflush(stdin);
            printf("请输入你所要插入的元素:\n");
            scanf("%d", &x);
            fflush(stdin);
            printf("请输入你所要插入的位置:\n");
            scanf("%d", &pos);
            Insert(&seq, pos, x);
            break;
        case REMOVE:
            fflush(stdin);
            printf("请输入要删除的元素\n");
            scanf("%d", &x);
            Remove(&seq, x);
            break;
        case REMOVEALL:
            fflush(stdin);
            printf("请输入要删除的元素\n");
            scanf("%d", &x);
            RemoveAll(&seq, x);
            break;
        case SORT:
            Sort(&seq);
            break;
        case BINARYSEARCH:
            fflush(stdin);
            printf("请输入要二分查找的元素\n");
            scanf("%d", &x);
            search = BinarySearch(&seq, x);
            printf("%d所在的位置是:%d", x, search);
            break;
        case EXIT:
            InitSqlist(&seq);
            input = 0;
            break;
        case PRINTFSEQLIST:
            PrintSeqlist(&seq);
            break;
        default:
            break;
        }
    }

}


int main()
{
    Test();


    system("pause");
    return 0;
}

数据结构—顺序表的实现

标签:

原文地址:http://blog.csdn.net/qq_26768741/article/details/51511410

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!