c语言实现简单顺序表

头文件

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#ifndef SEQLIST_H__
#define SEQLIST_H__
#define MAX 100
typedef int DataType;
typedef struct SeqList
{
    DataType arr[MAX];
    int count;
}SeqList,*pList;

void InItSeqList(pList p);//初始化
void PushBack(pList p,DataType data);//尾插
void PushFront(pList p, DataType data);//头插
void PopBack(pList p);//尾删
void PopFront(pList p);//头删
void Remove(pList p,DataType data);//删除指定元素
void RemoveAll(pList p,DataType data);//删除指定所有元素
void BubbleSort(pList p);//排序
int Search(pList p,DataType data);//查找
void Show(pList p);//全部显示
int BinarySearch(pList p,DataType d);//二分查找
#endif // !SEQLIST_H__

测试文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{
    printf("*******1.PushBack-尾插                   2.PushFront-头插*******\n");
    printf("*******3.PopBack-尾删                    4.PopFront-头删********\n");
    printf("*******5.Search-查找                     6.Remove-删除指定元素**\n");
    printf("*******7.RemoveAll-删除指定所有元素      8.BubbleSort-排序******\n");
    printf("*******9.Show-显示顺序表               10.BinarySearch-二分查找*\n");
    printf("*******  ****************                0.Exit*****************\n");
}
int main()
{
    SeqList mylist;
    int input = 0;
    int data = 0;
    InItSeqList(&mylist);
    do
    {
        menu();
        printf("请选择需要的操作:\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("请输入要插入的元素:");
            scanf("%d", &data);
            PushBack(&mylist,data);
            break;
        case 2:
            printf("请输入要插入的元素:");
            scanf("%d", &data);
            PushFront(&mylist, data);
            break;
        case 3:
            PopBack(&mylist);//尾删
            break;
        case 4:
            PopFront(&mylist);//头删
            break;
        case 5:
        {
                  int Ret = 0;
                  printf("请输入要查找的元素:");
                  scanf("%d", &data);
                  Ret = Search(&mylist, data);
                  if (Ret == -1)
                  {
                      printf("查找的元素不存在");
                  }
                  else
                  {
                      printf("查找的元素下标为%d\n", Ret);
                  }
        }
            break;
        case 6:
            printf("请输入要删除的元素:");
            scanf("%d", &data);
            Remove(&mylist,data);
            break;
        case 7:
            printf("请输入要删除的元素:");
            scanf("%d", &data);
            RemoveAll(&mylist,data);
            break;
        case 8:
        BubbleSort(&mylist);
            break;
        case 9:
            Show(&mylist);
            break;
        case 10:
            printf("请输入要查找的元素:");
            scanf("%d", &data);
            BinarySearch(&mylist,data);
        defult:
            break;
        }
    } while (input);
    system("pause");
    return 0;
}

主要函数的实现

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void InItSeqList(pList p)//初始化顺序表
{
    p->count = 0;
    memset(p->arr, 0, sizeof(DataType)*MAX);
}
void PushBack(pList p,DataType data)//尾插
{
    assert(p != NULL);
    if (MAX == p->count)
    {
        printf("顺序表已满\n");
        return;
    }
    else
    {
        p->arr[p->count] = data;
        p->count++;
    }
}
void PopBack(pList p)//尾删
{
    if (p != NULL)
    {
        p->count--;
    }
    else
    {
        return;
    }
}

void PushFront(pList p, DataType data)//头插
{
    int i = 0;
    assert(p != NULL);
    if (MAX == p->count)
    {
        printf("顺序表已满\n");
        return;
    }
    else
    {
        for (i = p->count; i >0; i--)
        {
            p->arr[i] = p->arr[i - 1];
        }
        p->arr[0] = data;
        p->count++;
    }
}
void PopFront(pList p)//头删
{
    int i = 0;
    assert(p != NULL);
    if (p->count == 0)
    {
        printf("顺序表为空\n");
    }
    else
    {
        for (i = 0; i < p->count-1; i++)
        {
            p->arr[i] = p->arr[i + 1];
            /*p->count--;*/
        }
        p->count--;
    }
}
void Show(pList p)//显示
{
    int i = 0;
    assert(p != NULL);
    for (i = 0; i < p->count; i++)
    {
        printf("%d\n", p->arr[i]);
    }
}
int Search(pList p,DataType d)//查找
{
    int i = 0;
    assert(p != NULL);
    for (i = 0; i < p->count; i++)
    {
        if (p->arr[i] == d)
        {
            return i;
        }
        else
        {
            return -1;
        }
    }
}
void Remove(pList p,DataType d)//删除指定元素
{
    int i = 0;
    int pos = 0;
    assert(p != NULL);
    if (pos = Search(p, d) != -1)
    {
        for (i = pos; i < p->count - 1; i++)
        {
            p->arr[i] = p->arr[i + 1];
        }
        p->count--;
    }
    else
    {
        printf("找不到需要删除的元素\n");
            return;
    }
}
void RemoveAll(pList p,DataType d)//删除指定所有元素
{
    int i = 0;
    int pos = 0;
    assert(p != NULL);
    while ((pos = Search(p, d)) != -1)
    {
        for (i = pos; i < p->count - 1; i++)
        {
            p->arr[i] = p->arr[i + 1];
        }
        p->count--;
    }
}
void BubbleSort(pList p)//排序
{
    int i = 0;
    int j = 0;
    assert(p != NULL); 
    for (i = 0; i < p->count - 1; i++)
    {
        for (j = 0; j < p->count - 1 - i; j++)
        {
            if (p->arr[j]>p->arr[j+1])
            {
                DataType tmp = p->arr[j];
                p->arr[j] = p->arr[j + 1];
                p->arr[j + 1] = tmp;
            }
        }
    }
}
int BinarySearch(pList p,DataType d)
{
    int left = 0;
    int right = p->count - 1;
    while (left <= right)
    {
        int mid = (left - (left - right)) >> 1;
        if (p->arr[mid] > d)
        {
            right = mid - 1;
        }
        else if (p->arr[mid] < d)
        {
            left = mid + 1;
        }
        else
        {
            return mid;
        }
    }
}

你可能感兴趣的:(c语言实现简单顺序表)