c语言实现简单顺序表

头文件

#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#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;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值