静态顺序表的各种操作

SeqList.h文件
#define _CRT_SECURE_NO_WARNINGS 1
#define MAXSIZE 10
#include <stdlib.h>
#include <memory.h>
#include <assert.h>
#include <stdio.h>
typedef int DataType;
typedef struct SeqList
{
int arr[MAXSIZE];
int size;
}SeqList,*PSeqList;
void InitSeqList(PSeqList pSeqList);       //初始化顺序表
void PrintSeqList(PSeqList pSeqList);     //打印顺序表
void PushBack(PSeqList pSeqList,DataType data);     //尾插
void PopBack(PSeqList pSeqList);           //尾删
void PushFront(PSeqList pSeqList,DataType data);      //头插
void PopFront(PSeqList pSeqList);          //头删  
void Insert(PSeqList pSeqList,int pos,DataType data);        //在任意位置插入data
void Erase(PSeqList pSeqList,int pos);           //删除任意位置的数
int Find(PSeqList pSeqList,DataType data);          //查找data返回其下标
void Remove(PSeqList pSeqList,DataType data);       //移除data
void RemoveAll(PSeqList pSeqList,DataType data);     //移除所有的data
void BubbleSort(PSeqList pSeqList);             //将顺序表进行冒泡排序
void SelectSort(PSeqList pSeqList);              //将顺序表进行选择排序
int BinarySearch_xh(PSeqList pSeqList, DataType data);       //利用二分法查找data,返回下标(循环法)
int BinarySearch_dg(PSeqList pSeqList, DataType data,int left,int right);         //  //利用二分法查找data,返回下标(递归法)
void InitSeqList(PSeqList pSeqList)
{
assert(pSeqList);
memset(pSeqList->arr,0,(MAXSIZE * sizeof(DataType)));
pSeqList->size = 0;
}
void PrintSeqList(PSeqList pSeqList)
{
int i = 0;
assert(pSeqList);
if(pSeqList->size == 0)
{
printf("该顺序表为空");
return;
}
for(;i<pSeqList->size;i++)
{
printf("%2d",pSeqList->arr[i]);
}
}
void PushBack(PSeqList pSeqList,DataType data)
{
assert(pSeqList);
if(pSeqList->size>=MAXSIZE)
{
printf("该顺序表已满");
return;
}
pSeqList->arr[pSeqList->size] = data;
pSeqList->size++;
}
void PopBack(PSeqList pSeqList)
{
assert(pSeqList);
if(pSeqList->size==0)
{
printf("该顺序表为空");
return;
}
pSeqList->size--;
}
void PushFront(PSeqList pSeqList,DataType data)
{
int i=0;
assert(pSeqList);
if(pSeqList->size>=MAXSIZE)
{
printf("该顺序表已满");
return;
}
for(i=pSeqList->size;i>0;i--)
{
pSeqList->arr[i]=pSeqList->arr[i-1];
}
pSeqList->arr[0]=data;
pSeqList->size++;
}
void PopFront(PSeqList pSeqList)
{
int i=1;
assert(pSeqList);
if(pSeqList->size==0)
{
printf("该顺序表为空");
}
for(;i<pSeqList->size;i++)
{
pSeqList->arr[i-1]=pSeqList->arr[i];
}
pSeqList->size--;
}
void Insert(PSeqList pSeqList,int pos,DataType data)
{
int i=0;
assert(pSeqList);
if(pSeqList->size>=MAXSIZE)
{
printf("该顺序表已满");
return;
}
if(pos<0 || pos>pSeqList->size)
{
printf("插入位置不合法");
return;
}
for(i=pSeqList->size;i>=pos;i--)
{
pSeqList->arr[i]=pSeqList->arr[i-1];
}
pSeqList->arr[pos]=data;
pSeqList->size++;
}
void Erase(PSeqList pSeqList,int pos)
{
int i=0;
assert(pSeqList);
if(pSeqList->size==0)
{
printf("该顺序表为空");
return;
}
if(pos<0 || pos>pSeqList->size-1)
{
printf("删除位置不合法");
return;
}
for(i=pos;i<pSeqList->size;i++)
{
pSeqList->arr[i]=pSeqList->arr[i+1];
}
pSeqList->size--;
}
int Find(PSeqList pSeqList,DataType data)
{
int i=0;
assert(pSeqList);
if(pSeqList->size==0)
{
printf("该顺序表为空");
return -1;
}
for(;i<pSeqList->size;i++)
{
if(pSeqList->arr[i]==data)
{
printf("%2d\n",i);
}
}
return -1;
}
void Remove(PSeqList pSeqList,DataType data)
{
int i=0;
assert(pSeqList);
    if(pSeqList->size==0)
{
printf("该顺序表为空");
return;
}
for(;i<pSeqList->size;i++)
{
if(pSeqList->arr[i]==data)
{
for(i;i<pSeqList->size-1;i++)
{
pSeqList->arr[i]=pSeqList->arr[i+1];
}
pSeqList->size--;
}
}
}
void RemoveAll(PSeqList pSeqList,DataType data)
{
int i=0;
int count=0;
assert(pSeqList);
    if(pSeqList->size==0)
{
printf("该顺序表为空");
return;
}
for(;i<pSeqList->size;i++)
{
if(pSeqList->arr[i]==data)
{
count++;
}
if(pSeqList->arr[i]!=data)
{
pSeqList->arr[i-count]=pSeqList->arr[i];
}
}
pSeqList->size-=count;
}
void BubbleSort(PSeqList pSeqList)
{
int i=0;
int j=0;
int flag=0;
assert(pSeqList);
for(j=0;j<pSeqList->size-1;j++)
{
flag=1;
for(i=0;i<pSeqList->size-1-j;i++)
{
if(pSeqList->arr[i]>pSeqList->arr[i+1])
{
DataType temp=pSeqList->arr[i];
pSeqList->arr[i]=pSeqList->arr[i+1];
pSeqList->arr[i+1]=temp;
flag=0;
}
}
if(flag==1)
{
break;
}
}
}
void SelectSort(PSeqList pSeqList)
{
int i=0;
int j=0;
int k=0;
assert(pSeqList);
for(i=0;i<pSeqList->size-1;i++)
{
k=i;
for(j=i+1;j<pSeqList->size;j++)
{
if(pSeqList->arr[j]>pSeqList->arr[k])
{
k=j;
}
}
if(k!=i)
{
DataType temp=pSeqList->arr[i];
pSeqList->arr[i]=pSeqList->arr[k];
pSeqList->arr[k]=temp;
}
}
}
int BinarySearch_xh(PSeqList pSeqList,DataType data)
{
int left=0;
int right=pSeqList->size-1;
int mid=0;
while(left<=right)
{
mid=left-(left-right)/2;
if(data>pSeqList->arr[mid])
{
left=mid+1;
}
else if(data<pSeqList->arr[mid])
{
right=mid-1;
}
else
{
printf("%2d",mid);
return 0;
}
}
printf("没找到");
return -1;
}
int BinarySearch_dg(PSeqList pSeqList,int data,int left,DataType right)
{
int mid=0;
if(left<=right)
{
mid=left-(left-right)/2;
if(data>pSeqList->arr[mid])
{
BinarySearch_dg(pSeqList,data,mid+1,right);
}
else if(data<pSeqList->arr[mid])
{
BinarySearch_dg(pSeqList,data,left,mid-1);
}
else
{
printf("%2d",mid);
return 0;
}
}
printf("没找到");
return -1;
}

SeqList.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void fun()
{
SeqList seqlist;
InitSeqList(&seqlist);
PushBack(&seqlist,1);
PushBack(&seqlist,2);
PushBack(&seqlist,3);
PrintSeqList(&seqlist);
printf("\n");
PushFront(&seqlist,3);
PushFront(&seqlist,2);
PushFront(&seqlist,1);
PrintSeqList(&seqlist);
printf("\n");
PopBack(&seqlist);
PopBack(&seqlist);
PrintSeqList(&seqlist);
printf("\n");
PopFront(&seqlist);
PrintSeqList(&seqlist);
printf("\n");
Insert(&seqlist,0,5);
PrintSeqList(&seqlist);
printf("\n");
PushFront(&seqlist,3);
PushFront(&seqlist,2);
PushFront(&seqlist,1);
Erase(&seqlist,1);
PrintSeqList(&seqlist);
printf("\n");
Find(&seqlist,3);
Remove(&seqlist,5);
PrintSeqList(&seqlist);
printf("\n");
RemoveAll(&seqlist,3);
PrintSeqList(&seqlist);
printf("\n");
Insert(&seqlist,0,5);
Insert(&seqlist,0,3);
Insert(&seqlist,0,8);
Insert(&seqlist,0,4);
PrintSeqList(&seqlist);
printf("\n");
BubbleSort(&seqlist);
PopFront(&seqlist);
PrintSeqList(&seqlist);
/*printf("\n");
SelectSort(&seqlist);
PrintSeqList(&seqlist);*/
printf("\n");
BinarySearch_xh(&seqlist,8);
printf("\n");
BinarySearch_dg(&seqlist,1,0,5);
}
int main()
{
fun();
printf("\n");
system("pause");
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值