c语言线性表脚本,c语言实现线性表

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

#define MAX_SIZE 5

typedef int DataType;

typedef struct Seqlist

{

DataType arr[MAX_SIZE];

size_t size;

}Seqlist;

void PrintSeqList(Seqlist *pSep);

void InitSeqList(Seqlist *pSep);

void PushBack(Seqlist *pSep);

void PopBack(Seqlist *pSep);

void PushFront(Seqlist *pSep);

void PopFront(Seqlist *pSep);

void Insert(Seqlist *pSep, size_t pos);

int Find(Seqlist *pSep, size_t pos,DataType x);

void Erase(Seqlist *pSep, size_t pos);

int Remove(Seqlist *pSep, DataType x);

void RemovAll(Seqlist *pSep, DataType x);

void BubbleSort(Seqlist *pSep);

void SelectSort(Seqlist *pSep);//一次选出最大最小的数放在两段

int BinarySearch(Seqlist *pSep, DataType x);

void PrintSeqList(Seqlist *pSep)

{

assert(pSep);

if (pSep->size <= 0)

{

printf("没有数据可以打印!\n");

return ;

}

int i = 0;

for (i = 0; i size; i++)

{

printf("%d ", pSep->arr[i]);

}

printf("\n");

}

void InitSeqList(Seqlist *pSep)

{

assert(pSep);

memset(pSep->arr, 0, sizeof(DataType)*MAX_SIZE);

pSep->size=0;

}

void PushBack(Seqlist *pSep)

{

assert(pSep);

if (pSep->size >(MAX_SIZE-1))

{

printf("数据已满!\n");

return;

}

else

{

scanf("%d", &pSep->arr[pSep->size]);

pSep->size++;

}

}

void PopBack(Seqlist *pSep)

{

assert(pSep);

if (pSep->size <=0)

{

printf("没有数据!");

return;

}

pSep->arr[pSep->size] = 0;

pSep->size--;

}

void PushFront(Seqlist *pSep)

{

assert(pSep);

int i = 0;

if (pSep->size >(MAX_SIZE-1))

{

printf("数据已满!\n");

return;

}

for (i = pSep->size; i >0; i--)

{

pSep->arr[i] = pSep->arr[i-1];

}

scanf("%d", &pSep->arr[0]);

pSep->size++;

}

void PopFront(Seqlist *pSep)

{

assert(pSep);

if (pSep->size <=0)

{

printf("没有数据!");

return;

}

else

{

int i = 0;

for (i = 1; i size; i++)

{

pSep->arr[i - 1] = pSep->arr[i];

}

pSep->size--;

}

}

void BubbleSort(Seqlist *pSep)

{

assert(pSep);

}

void Insert(Seqlist *pSep, size_t pos)

{

assert(pSep);

assert(pos<=pSep->size);

int i = 0;

if (pSep->size >(MAX_SIZE - 1))

{

printf("数据已满!\n");

return;

}

else

{

for (i = (int)pSep->size; i >= pos ; i--)

{

pSep->arr[i] = pSep->arr[i - 1];

}

scanf("%d", &pSep->arr[pos-1]);

pSep->size++;

}

}

int Find(Seqlist *pSep, size_t pos,DataType x)

{

assert(pSep);

int i = 0;

if (pos > pSep->size && pos 

{

printf("输入位置不对!\n");

return 0;

}

else

{

for (i = pos; i size; i++)

{

if (pSep->arr[pos] == x)

{

printf("已找到%d\n", pSep->arr[pos]);

return i;

}

}

printf("没有这个数!\n");

}

return -1;

}

void Erase(Seqlist *pSep, size_t pos)

{

assert(pSep);

int i = 0;

if (pSep->size+1 <= pos)

{

printf("输入位置不对!");

return;

}

else

{

for (i = pos; i <= pSep->size-1; i++)

{

pSep->arr[i-1] = pSep->arr[i];

}

}

pSep->size--;

}

int Remove(Seqlist *pSep, DataType x)

{

assert(pSep);

int i = 0;

int j = 0;

if (pSep->size <= 0)

{

printf("没有数据!");

return;

}

else

{

for (i = 0; i <= pSep->size; i++)

{

if (pSep->arr[i] == x)

{

for (j = i; j <= pSep->size - 1; j++)

{

pSep->arr[j - 1] = pSep->arr[j];

}

pSep->size--;

return;

}

}

}

return 0;

}

void RemovAll(Seqlist *pSep, DataType x)

{

assert(pSep);

int i = 0;

int j = 0;

//int count = 0;

if (pSep->size <= 0)

{

printf("没有数据!");

return;

}

else

{

for (i = 0; i size; i++)

{

if (pSep->arr[i] == x)

{

for (j = i; j size; j++)

{

pSep->arr[j] = pSep->arr[j+1];

}

pSep->size--;

i--;

}

}

}

}

void SelectSort(Seqlist *pSep)//一次选出最大最小的数放在两段

{

assert(pSep);

int Min = 0;

int Max = 0;

int i = 0;

int j = 0;

int flag = pSep->size;

if (pSep->size <= 0)

{

printf("没有数据!");

return;

}

else

{

for (i = 0; i

{

Min = pSep->arr[i];

Max = pSep->arr[i];

for (j = i; j

{

if (pSep->arr[j]>Max)

{

Max = pSep->arr[j];

}

if (pSep->arr[j] 

{

Min = pSep->arr[j];

}

}

pSep->arr[i] = Max;

pSep->arr[pSep->size - i-1] = Min;

flag--;

}

}

}

int BinarySearch(Seqlist *pSep, DataType x)

{

assert(pSep);

int left = 0;

int right = pSep->size-1;

while (left <=right)

{

int mid =left+(right-left)/2;

if (x > pSep->arr[mid])

{

left = mid + 1;

}

else if (x arr[mid])

{

right = mid -1;

}

else

{

printf("%d", pSep->arr[mid]);

return 0;

}

}

return -1;

}

int main()

{

Seqlist Test;

InitSeqList(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushFront(&Test);

//PushFront(&Test);

//PushFront(&Test);

//PushFront(&Test);

//PushFront(&Test);

//PopFront(&Test);

//PopFront(&Test);

//PopFront(&Test);

//PopFront(&Test);

//PopFront(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//Find(&Test, 2, 3);

//PrintSeqList(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//Erase(&Test, 1);

//PrintSeqList(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//Remove(&Test, 2);

//PrintSeqList(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//PushBack(&Test);

//RemovAll(&Test, 2);

//PrintSeqList(&Test);

//

PushBack(&Test);

PushBack(&Test);

PushBack(&Test);

PushBack(&Test);

PushBack(&Test);

BinarySearch(&Test,3);

PrintSeqList(&Test);

system("pause");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值