顺序表的实现

.h文件

 

#pragma once

#include

#include

#include

 

#define MAX_SIZE 1000//预定义顺序表为1000

typedef int DataType;//控制以后数据类型

typedef struct SeqList//定义一个结构体

{

DataType array[MAX_SIZE];//定义一个数组

size_t size;//标识有效数据的个数

}SeqList;

int pos;

void pushBack(SeqList* pSeq,DataType x);//在当前顺序表的尾部插入一个数据

void popBack(SeqList* pSeq);

 

void pushFront(SeqList* pSeq,DataType x);

void popFront(SeqList* pSeq);

 

void InitSeqList( SeqList* pSeq);

void printSeqList(SeqList* pSeq);

 

void InsertSeqList(SeqList* pSeq,int pos , DataType x);

int Find(SeqList* pSeq,DataType x);//寻找某个数据

int Erase(SeqList* pSeq,size_t pos);//删除某个位置的数据

int Remove(SeqList* pSeq,DataType x);//找到某个数据并且删除

int RemoveAll(SeqList* pSeq,DataType x);//删除顺序表内出现的重复数据

int BinarySearch(SeqList* pSeq,DataType x);//二分查找

void ReverseList(SeqList* pSeq);//翻转顺序表

 

void pushBack(SeqListpSeq,DataType x)//在当前顺序表的尾部插入一个数据

{   //1检查参数

//2检查顺序表是否已满

//3完成功能

assert(pSeq);

if(pSeq->size==MAX_SIZE)

{

printf("顺序表以满\n");

return;

}

pSeq->array[pSeq->size]=x;

pSeq->size++;

 

}

void popBack(SeqListpSeq)

{

assert(pSeq);

if(pSeq->size<=0)

{

printf("顺序表为空\n");

return;

}

   pSeq->size--;

}

void InitSeqList( SeqListpSeq)

{

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

pSeq->size=0;//初始化局部变量,并不印象外边的东西所以必须通过传址调用,通过指针改变外边的东西

}

void printSeqList(SeqListpSeq)

{

    int i=0;

assert(pSeq);

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

{

printf("%d->",pSeq->array[i]);

}

printf("NULL\n");

}

void pushFront(SeqListpSeq,DataType x)

{

int i=0;

assert(pSeq);

if(pSeq->size==MAX_SIZE)

{

printf("List is Full\n");

return;

}

else

{

for(i=pSeq->size;i>=0;i--)//一定要注意边界条件刚开始的时候i>=0,否则当数组下标为0的时候会插入失败

{

pSeq->array[i+1]=pSeq->array[i];

pSeq->array[i]=x;

}

  pSeq->size++;//前置++和后置++都可以

}

}

void popFront(SeqListpSeq)

{

int i=0;

assert(pSeq);

if(pSeq->size<=0)

{

printf("List is empty");

return;

}

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

{

pSeq->array[i]=pSeq->array[i+1];

}

--pSeq->size;

}

void InsertSeqList(SeqListpSeq,size_t pos , DataType x)

//size_t 无符号×××不用检查POS是否大于零

{

int i=0;

assert(pSeq);

assert(pos<pSeq->size);

if(pSeq->size>=MAX_SIZE)

{

printf("List is Full\n");

return;

}

for(i=pSeq->size;i>=pos;i--)

{

pSeq->array[i+1]=pSeq->array[i];

}

pSeq->array[pos]=x;

++pSeq->size;

}

 

 

int Find(SeqListpSeq,DataType x)//寻找某个数据

{

int i=0;

   assert(pSeq);

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

   {

   if(pSeq->array[i]==x)

       return i;

   }

   return -1;

}

int Erase(SeqListpSeq,size_t pos)//删除某个位置的数据

{  

int i=pos+1;

   assert(pSeq);

   assert(pos<=pSeq->size);

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

   {

   pSeq->array[i-1]=pSeq->array[i];

   }

   --pSeq->size;

}

int Remove(SeqListpSeq,DataType x)//找到某个数据并且删除

{

   int pos;

   assert(pSeq);

   pos=Find(pSeq,x);

   if(pos!=-1)

   {

   Erase(pSeq,pos);

   }

 

}

int RemoveAll(SeqListpSeq,DataType x)//删除顺序表内出现的重复数据

{

int pos;

   assert(pSeq);

   pos=Find(pSeq,x);

   while(pos!=-1)

   {

   Erase(pSeq,pos);

   pos=Find(pSeq,x);

   }

 

}

 

int BinarySearch(SeqListpSeq,DataType x)//二分查找

{

int left=0;

int right=pSeq->size;

assert(pSeq);

while(left

{

int mid=(left+right)/2;

if(x>pSeq->array[mid])

{

left=mid+1;

}

else if (x<pSeq->array[mid])

{

right=mid-1;

}

else

{

return mid;

    }

}

 

return -1;

}

 

void ReverseList(SeqListpSeq)//翻转顺序表

{

int start=0;

int end=pSeq->size-1;

while(start

{

DataType tmp=pSeq->array[start];

pSeq->array[start]=pSeq->array[end];

pSeq->array[end]=tmp;

start++;

end--;

}

}

.c

#define _CRT_SECURE_NO_WARNINGS 1

#include

#include"SeqList.h"

 

//InitSeqLis/pushBack/ popBack

void Test1( )

{

 

SeqList seq;//定义一个顺序表

InitSeqList(&seq);//初始化顺序表

 

 pushBack(&seq,1);//尾插法插入数据

 pushBack(&seq,2);

 pushBack(&seq,3);

 pushBack(&seq,4);

 pushBack(&seq,5);

 pushBack(&seq,6);

 printSeqList(&seq);//打印数据

  popBack(&seq);

  popBack(&seq);

  popBack(&seq);

  popBack(&seq);

  popBack(&seq);

 

 printSeqList(&seq);//打印数据

}

void Test2()

{

  SeqList seq;//定义一个顺序表

  InitSeqList(&seq);//初始化顺序表

  pushFront(&seq,1);

  pushFront(&seq,2);

  pushFront(&seq,3);

  pushFront(&seq,4);

  pushFront(&seq,5);

  printSeqList(&seq);

 

  popFront(&seq);

  popFront(&seq);

  popFront(&seq);

  popFront(&seq);

  popFront(&seq);

  printSeqList(&seq);

}

 

int Test3()

{

  SeqList seq;//定义一个顺序表

  InitSeqList(&seq);//初始化顺序表

  pushFront(&seq,1);

  pushFront(&seq,2);

  pushFront(&seq,2);

  pushFront(&seq,4);

  pushFront(&seq,5);

  printSeqList(&seq);

  ReverseList(&seq);

  printSeqList(&seq);

  

}

int main()

{

 

Test3();

system("pause");

return 0;

}