顺序表

seqList.h
#define _CRT_SECURE_NO_WARNINGS 1  
#ifndef _SEQLIST_H__  
#define _SEQLIST_H__   

#include<stdio.h>  
#include<stdlib.h>  
#include<assert.h>  
#include<string.h>  

#define MAX 10  
#define DataType int  

typedef struct Seqlist  
{  
    DataType data[MAX];//数据  
    int sz;//存放数据的个数  
}*PSeqList, Seqlist;  

//初始化 
void InitSeqList(PSeqList pSeq); 
//尾部插入 
void PushBack(PSeqList pSeq, DataType data); 
//尾部删除 
void PopBack(PSeqList pSeq); 
//头部插入 
void PushFront(PSeqList pSeq, DataType data); 
//头部删除 
void PopFront(PSeqList pSeq); 
//查找指定元素 
int Find(PSeqList pSeq, DataType data); 
//指定位置插入 
void Insert(PSeqList pSeq, int pos, DataType data); 
//删除指定位置元素 
void Erase(PSeqList pSeq, int pos); 
//删除指定元素 
void Remove(PSeqList pSeq, DataType data); 
//删除所有的指定元素 
void RemoveAll(PSeqList pSeq, DataType data); 
//返回顺序表的大小 
int Size(PSeqList pSeq); 
//判断顺序表是否为空 
int Empty(PSeqList pSeq); 
//冒泡排序 
void BubbleSort(PSeqList pSeq); 
//选择排序 
void SelectSort(PSeqList pSeq); 
//选择排序的优化 
void SelectSortOP(PSeqList pSeq); 
//二分查找 
int BinarySearch(PSeqList pSeq, DataType data); 
//二分查找递归写法 
int BinarySearch_R(PSeqList pSeq, int left, int right, DataType d); 

//打印 
void PrintSeqList(PSeqList pSeq); 

#endif//_SEQLIST_H__ 
sedList.c
#define _CRT_SECURE_NO_WARNINGS 1  
#include"seqlist.h"

void InitSeqList(PSeqList pSeq)
{
    assert(pSeq != NULL);
    memset(pSeq->data,0,sizeof(DataType)*MAX);
    pSeq->sz = 0;

}

void PrintSeqList(PSeqList pSeq)
{
    int i = 0;
    assert(pSeq != NULL);
    if(pSeq->sz ==0)
    {
        printf("该顺序表没有元素\n");
        return;
    }
    for(i=0; i<pSeq->sz;i++)
    {
        printf("%d ",pSeq->data[i]);
    }
        printf("\n");

}

void PushBack(PSeqList pSeq, DataType data)
{
   assert(pSeq != NULL);
   if(pSeq->sz == MAX)
   {
      printf("该顺序表已满\n");
      return;

   }
     pSeq->data[pSeq->sz] = data;
     pSeq->sz++;

}
void PopBack(PSeqList pSeq)
{
    assert(pSeq != NULL);
    if(pSeq->sz == 0)
    {
       printf("该顺序表为空顺序表\n");
       return;
    }
       pSeq->sz--; 

}
void PushFront(PSeqList pSeq, DataType data)
{
   int i = 0;
   assert(pSeq != NULL);
   if(pSeq->sz == MAX)
   {
      printf("该顺序表已满\n");
      return;
   }
   for(i=pSeq->sz; i>0; i--)
   {
      pSeq->data[i] = pSeq->data[i-1];

   }
      pSeq->data[0] = data;
      pSeq->sz++;

}
void PopFront(PSeqList pSeq)
{
     int i = 0;
     assert(pSeq != NULL);
     if(pSeq->sz == 0)
     {
       printf("该顺序表为空顺序表\n");
       return;
     }
     for(i=0; i<pSeq->sz-1;i++)
     {
       pSeq->data[i] = pSeq->data[i+1]; 
     }
       pSeq->sz--;

}
int Find(PSeqList pSeq, DataType data)
{
    int i = 0;
    assert( pSeq != NULL);
    if(pSeq->sz == 0)
    {
      printf("该顺序表为空顺序表\n");
      return -1;
    }
    while( i < pSeq->sz)
    {
      if(data == pSeq->data[i] )
      return i;
      i++;
    }
      return -1;

}
void Insert(PSeqList pSeq, int pos, DataType data)
{
    int i = 0;
    assert(pSeq != NULL);
    if(pSeq->sz == MAX)
    {
      printf("该顺序表已满\n");
      return;
    }
    for(i=pSeq->sz; i>pos; i-- )
    {
      pSeq->data[i] = pSeq->data[i-1];
    }
      pSeq->data[pos] = data;
      pSeq->sz++;

}
void Erase(PSeqList pSeq, int pos)
{
     int i = 0;
     assert(pSeq != NULL);
     if(pSeq->sz == 0)
     {
       printf("该顺序表为空顺序表\n");
       return;
     }
     for(i=pos; i<pSeq->sz-1;i++)
     {
       pSeq->data[i] = pSeq->data[i+1]; 
     }
       pSeq->sz--;

}
void Remove(PSeqList pSeq, DataType data)
{
     int i = 0;
     int pos = 0;
     assert(pSeq != NULL);
     if(pSeq->sz == 0)
     {
       printf("该顺序表为空顺序表\n");
       return;
     }
     while(pos < pSeq->sz)
     {
       if(data == pSeq->data[pos])
       break;
       pos++;
     }
     for(i=pos;i<pSeq->sz-1;i++)
     {
       pSeq->data[i] = pSeq->data[i+1];
     }
       pSeq->sz--;
}
void RemoveAll(PSeqList pSeq, DataType data)
{
     int i = 0;
     int pos = 0;
     assert(pSeq != NULL);
     if(pSeq->sz == 0)
     {
       printf("该顺序表为空顺序表\n");
       return;
     }
     while(pos < pSeq->sz)
     {
       if(data == pSeq->data[pos])
       {
       for(i=pos;i<pSeq->sz-1;i++)
       {
         pSeq->data[i] = pSeq->data[i+1];
       }
          pSeq->sz--; 
       }
          pos++;
     }

}
int Size(PSeqList pSeq)
{ 
    return pSeq->sz;
}
int Empty(PSeqList pSeq)
{
   return pSeq->sz == 0 ? 0: 1;
}
void Swap(int *p, int *q)
{
   int tmp = 0;
   tmp = *p;
   *p = *q;
   *q = tmp;

}
void BubbleSort(PSeqList pSeq)
{
    int i = 0;
    int j = 0;
    assert(pSeq != NULL);
    for(i=0; i<pSeq->sz-1; i++)
    {
        for(j=0; j<pSeq->sz-1-i; j++)
        {

            if(pSeq->data[j] > pSeq->data[j+1])
            {
                Swap(&pSeq->data[j],&pSeq->data[j+1]);
            }
        }

    }

}
void SelectSort(PSeqList pSeq)
{
   int i = 0;
   int j = 0;
   assert(pSeq != NULL);
   for(i=0; i<pSeq->sz-1; i++)
   {      
       int max = i;
       for(j=i+1; j<pSeq->sz; j++)
       {      
           if(pSeq->data[max]<pSeq->data[j])
           {                       
              max = j;      
           }

       }
          if(max != i)
          {

              Swap(&pSeq->data[max],&pSeq->data[i]);    
          }  
   }

}
void SelectSortOP(PSeqList pSeq)
{
   int i = 0;
   int start = 0;
   int end = pSeq->sz;
   assert(pSeq != NULL);
   while(i < pSeq->sz/2)  
   {
       int max = i;
       int min = i;
       for(start=i+1; start<end; start++)
       {      
           if(pSeq->data[max] < pSeq->data[start])
           {                       
               max = start;     
           }
           else if(pSeq->data[min] > pSeq->data[start])
               min = start;      
       }

       Swap(&pSeq->data[min],&pSeq->data[i]);
       if(max == i)
       max = min;
       Swap(&pSeq->data[max],&pSeq->data[--end]); 
       i++; 
   }
}
int BinarySearch(PSeqList pSeq, DataType data)
{
      int left = 0;
      int right = pSeq->sz-1;
      assert(pSeq != NULL);
      while(left<=right)
      {
      int mid = left+(right-left)/2;
      if(pSeq->data[mid] == data)
      {
            return mid;
      }
      else if(pSeq->data[mid] <  data)
      {
            left = mid+1;
      }
      else 
            right = mid-1;
      }   
      return -1;
}
int BinarySearch_R(PSeqList pSeq, int left, int right, DataType d)
{
    assert(pSeq != NULL);
    while(left <= right)
    {
        int mid = left + (right-left)/2;
        if(pSeq->data[mid] == d)
           return mid;
        else if(pSeq->data[mid] > d)    
           BinarySearch_R(pSeq,left,right = mid-1,d);
        else
           BinarySearch_R(pSeq,left = mid+1,right,d);       
    }
    return -1;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1  
#include"seqlist.h"
void test()
{
    Seqlist s;
    int ret = 0;
    InitSeqList(&s);//初始化
    printf("尾插\n");
    PushBack(&s,1);
    PushBack(&s,2);
    PushBack(&s,3);
    PushBack(&s,4);
    PushBack(&s,5);
    PrintSeqList(&s); 

    printf("尾删\n");
    PopBack(&s);
    PrintSeqList(&s); 

    printf("头插\n");
    PushFront(&s, 5);
    PushFront(&s, 8);
    PushFront(&s, 7);
    PushFront(&s, 8);
    PushFront(&s, 9);
    PushFront(&s, 10);
    PrintSeqList(&s);

    printf("头删\n");
    PopFront(&s);
    PrintSeqList(&s);

    printf("查找指定元素\n");
    ret = Find(&s, 6);
    printf("%d\n",ret);

    printf("在指定位置插入元素\n");
    Insert(&s, 5, 12);
    PrintSeqList(&s);

    printf("删除指定位置元素\n");
    Erase(&s, 5);
    PrintSeqList(&s);

    printf("删除指定元素\n");
    Remove(&s, 5);
    PrintSeqList(&s);

    printf("删除所有指定元素\n");
    RemoveAll(&s,8);
    PrintSeqList(&s);

    printf("返回顺序表的大小\n");
    ret = Size(&s);
    printf("%d\n",ret);

    printf("判断顺序表是否为空\n");
    ret = Empty(&s);
    if(ret == 1)
    {
      printf("顺序表不为空\n");
    }
    if(ret == 0)
    {
      printf("顺序表为空\n");
    }

    printf("冒泡排序\n");
    BubbleSort(&s);
    PrintSeqList(&s);

    printf("选择排序\n");
    SelectSort(&s);
    PrintSeqList(&s);

    printf("选择排序的优化\n");
    SelectSortOP(&s);
    PrintSeqList(&s);

    printf("二分查找\n");
    ret = BinarySearch(&s,9);
    printf("%d\n",ret);

    printf("递归二分查找\n");
    ret = BinarySearch_R(&s,0,s.sz-1,7 );
    printf("%d\n",ret);
}
int main()
{

    test();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值