#pragma once
#include<stdio.h>

#include<assert.h>
#include<string.h>
#define MAX_SIZE 5
typedef int DataType;

typedef struct SeqList
{
DataType array[MAX_SIZE ];
size_t size;
}SeqList;

void PrintSeqList(SeqList * pSeq);
void InitSeqList(SeqList * pSeq);
void PushBack(SeqList * pSeq, DataType x);
void PopBack(SeqList * pSeq);
void PushFront(SeqList * pSeq, DataType x);
void Popfront(SeqList * pSeq);
void Insert(SeqList *pSeq, rsize_t pos, DataType x);
int Find(SeqList *pSeq, DataType x);
int Remove(SeqList * pSeq, DataType x);
void RemoveAll(SeqList * pSeq, DataType x);

void printSeqList(SeqList * pSeq);


void InitSeqList(SeqList * pSeq)
{

memset( pSeq->array, 0, sizeof (DataType)* MAX_SIZE);
pSeq->size = 0;
}

void PushBack(SeqList * pSeq, DataType x )
{
assert(pSeq );
if (pSeq ->size >= MAX_SIZE)
{
printf( "SeqList is full");
return;
}
pSeq->array[pSeq ->size++] = x;
}
void PopBack(SeqList * pSeq)
{
assert(pSeq );
if (pSeq ->size <= 0)
{
printf( "SeqList is Empty");
return;
}
pSeq->array[--pSeq ->size ] = 0;
}
void PrintSeqList(SeqList * pSeq)
{
int i = 0;
assert(pSeq );
for (; i < pSeq ->size; ++i)
{
printf( "%d ", pSeq ->array[i]);
}
printf( "\n");
}


void PushFront(SeqList * pSeq, DataType x )
{
int begin = pSeq ->size - 1;
assert(pSeq );
if (pSeq ->size >= MAX_SIZE)
{
printf( "SeqList is full\n");
return ;
}
for (; begin >= 0; --begin)
{
pSeq->array[begin + 1] = pSeq ->array[begin];
}
pSeq->array[0] = x ;
++ pSeq->size;
}
void Popfront(SeqList * pSeq)
{
int begin = 1;
assert(pSeq );
if (pSeq ->size <= 0)
{
printf( "SeqList is Empty");
return;
}

for (; begin < pSeq ->size; ++begin)
{
pSeq->array[begin - 1] = pSeq ->array[begin];

}
-- pSeq->size;
}


void Insert(SeqList *pSeq, rsize_t pos , DataType x)
{
int begin = pSeq ->size - 1;
assert(pSeq );
assert(pos <= pSeq->size);
if (pSeq ->size >= MAX_SIZE)
{
printf( "SeqList is full\n");
return;
}
for (; begin >= pos ; --begin)
{
pSeq->array[begin + 1] = pSeq ->array[begin];
}
pSeq->array[pos ] = x;
++ pSeq->size;
}


int Find(SeqList *pSeq, DataType x )
{
int i = 0;
assert(x );
for (; i < pSeq ->size; i++)
{
if (pSeq ->array[i] == x)
{
return i;
}
return -1;
}
}


void Erase(SeqList * pSeq, size_t pos )
{
assert(pSeq );
if (pSeq ->size <= 0)
{
printf( "SeqList is Empty\n");
return;
}
assert(pos < pSeq->size);
int begin = pos + 1;
for (; begin < pSeq ->size; ++begin)
{
pSeq->array[begin - 1] = pSeq ->array[begin];
}
--begin;
}


int Remove(SeqList * pSeq, DataType x )
{
int pos;
assert(pSeq );
pos = Find( pSeq, x );
if (pos != -1)
{
Erase( pSeq, pos);
}
return pos;
}


void RemoveAll(SeqList * pSeq, DataType x )
{
int pos;
assert(pSeq );
pos = Find( pSeq, x );
while (pos != -1)
{
Erase( pSeq, pos);
pos = Find( pSeq, x );
}
}


int BinarySearch(SeqList * pSeq, DataType x )
{
assert(pSeq );

int left = 0;
int right = pSeq ->size;
while (left < right)
{
int mid = (right + left) / 2;
if (x >pSeq->array[mid])
{
left = mid + 1;
}
else if (x < pSeq->array[mid])
{
right = mid - 1;
}
else
return mid;
}
}