利用C语言实现动态顺序表


1. 数据类型及动态分配顺序存储结构


#define INIT_SIZE 10                           //当前默认有效值
#define DEFAULT_INC 10                    //动态分配默认增长个数

typedef int DataType;

typedef struct SeqList
{
     DataType* data;                            //指向空间的指针
     int size;                                          //当前有效的个数
     int capacity;                                   //容量
}SeqList,*pSeqList;


2. 动态顺序表的常用操作


(1) 顺序表的初始化

void InitSeqList(pSeqList pSeq)//初始化顺序表
{
     assert(pSeq);
     pSeq->data = (DataType *)malloc(INIT_SIZE*sizeof(DataType));//
     memset(pSeq->data,0,INIT_SIZE*sizeof(DataType));
     pSeq->capacity = INIT_SIZE;
     pSeq->size = 0;
}


(2) 在顺序表中尾插,头插,在特定位置插入时,需要判断当前顺序表的容量是否够用,若不够,则需要开辟新的空间

void CheckCapacity(pSeqList pSeq)//判断容量
{
     if(pSeq->size == pSeq->capacity)
     {
          pSeq->data = (DataType*)realloc(pSeq->data,pSeq->capacity + DEFAULT_INC);
          pSeq->capacity = pSeq->capacity + DEFAULT_INC;
     }
}


(3) 动态顺序表的常用操作

//------------------------------- 尾插 -------------------------------------

void PushBack(pSeqList pSeq, DataType x)             
{
     assert(pSeq);
     CheckCapacity(pSeq);
     pSeq->data[pSeq->size] = x;
     pSeq->size++;
}

//-------------------------------- 尾删 ------------------------------------

void PopBack(pSeqList pSeq)                                    
{
     assert(pSeq);
     if(pSeq->size == 0)
     {
          printf("顺序表为空");
     }
     pSeq->size--;
}

//----- 头插,从尾部向前,每次后移一位,空出第一位插入,size++ -----

void PushFront(pSeqList pSeq, DataType x)                

     int i = 0;
     assert(pSeq);
     CheckCapacity(pSeq);
     for(i=pSeq->size-1; i>=0;i--)
     {
          pSeq->data[i+1] = pSeq->data[i];
     }
     pSeq->data[0] = x;
     pSeq->size++;
}

//------------- 头删,从前超后,每一次前移一位,size--  ----------------

void PopFront(pSeqList pSeq)                                     
{
     int i = 0;
     assert(pSeq);
     if(pSeq->size == 0)
     {
          printf("顺序表为空!");
     }
     for(i = 0;i<pSeq->size-1;i++)
     {
          pSeq->data[i] = pSeq->data[i+1];
     }
     pSeq->size--;
}

//-- 插入,从后移到指向pos的那一位,开始后移,将x赋给指向pos的结点 ---

void Insert(pSeqList pSeq, int pos, DataType x)
{
     int i = 0;
     assert(pSeq);
     assert((pos>=0)&&(pos<pSeq->size-1));
     for(i=pSeq->size-1;i>=pos;i--)
     {
          pSeq->data[i+1] = pSeq->data[i];
     }
     pSeq->data[pos] = x;
     pSeq->size++;
}

//------------------------- 删除指定元素 ---------------------------------------

void Remove(pSeqList pSeq,DataType x)          
{
     int pos = Find(pSeq,x);
     int i = 0;
     assert(pSeq);
     for(i=pos;i<pSeq->size-1;i++)
     {
          pSeq->data[i] = pSeq->data[i+1];
     }
     pSeq->size--;
}

//-------------------------  删除所有相同节点 ----------------------------------

void RemoveAll(pSeqList pSeq, DataType x)      
{
     int pos = Find(pSeq,x);
     assert(pSeq);
     while(pos != -1)
     {
          int i = 0;
          for(i=pos ;i<pSeq->size-1;i++)
          {
               pSeq->data[i] = pSeq->data[i+1];
          }
          pSeq->size--;
          pos = Find(pSeq ,x);
     }
}

//---------------------------------  查找  -------------------------------------

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

//---------------------------------- 逆序 --------------------------------------

void ReverseList(pSeqList pSeq)                
{
     int start = 0;
     int tail = pSeq->size-1;
     assert(pSeq);
     while(start < tail)
     {
          DataType tmp = pSeq->data[start];
          pSeq->data[start] = pSeq->data[tail];
          pSeq->data[tail] = tmp;
          start++;
          tail--;
     }
}

//----------------------------------  排序  ------------------------------------

void SortList(pSeqList pSeq)                   
{
     int i = 0;
     int j = 0;
     assert(pSeq);
     for(i = 0;i<pSeq->size-1;i++)
     {
          for(j = 0;j<pSeq->size-1-i;j++)
          {
               if(pSeq->data[j] > pSeq->data[j+1])
               {
                    DataType tmp = pSeq->data[j];
                    pSeq->data[j] = pSeq->data[j+1];
                    pSeq->data[j+1] = tmp;
               }
          }
     }
}

// --------------------------------  折半查找  ---------------------------------

int BinarySearch(pSeqList pSeq,DataType x)     
{
     int left = 0;
     int right = pSeq->size-1;
     assert(pSeq);
     while(left <= right)
     {
          int mid = left-((left - right)>>1);
          if(pSeq->data[mid] == x)
          {
               return mid;
          }
          else if(x > pSeq->data[mid])
          {
               left = mid+1;
          }
          else
          {
               right = mid-1;
          }
     }
}