您的位置:首页 > 其它

顺序表的增删改查及冒泡、选择排序、二分查找

2018-02-07 23:27 459 查看
实现的功能:

1、初始化顺序表:

void SeqInit(SeqList* pSeq)
{
pSeq->_size = 0;
pSeq->_capicity = MAX;
pSeq->_a = (DataType*)malloc(sizeof(DataType)*pSeq->_capicity);

memset(pSeq->_a, 0, sizeof(DataType)*pSeq->_capicity);
}


2、打印顺序表:

void SeqPrint(SeqList* pSeq)
{
size_t i=0;
for(i=0;i<pSeq->_size;i++)
{
printf("%d ",pSeq->_a[i]);
}
}


3、判满加扩容

SeqList* SeqCheckFull(SeqList* pSeq)
{
assert(pSeq);
if(pSeq->_size==pSeq->_capicity)
{
DataType *tmp=(DataType*)realloc(pSeq->_a,sizeof(DataType)*pSeq->_capicity*2);
if(NULL==tmp)
{
printf("扩容失败\n");
return NULL;
}
pSeq->_capicity=pSeq->_capicity*2;
pSeq->_a=tmp;

}
return pSeq;

}


4、销毁

void SeqDestory(SeqList* pSeq)
{
free(pSeq->_a);
pSeq->_size=0;

}


5、任意位置插入:

void SeqInsert(SeqList* pSeq, size_t pos, DataType x)//任意插
{
assert(pSeq);
pSeq = SeqCheckFull(pSeq);
if (pSeq != NULL)
{
if (0 == pSeq->_size)
{
pSeq->_a[pSeq->_size++] = x;

}
else
{
int end = pSeq->_size - 1;
while (end >= (int)pos)
{
pSeq->_a[end + 1] = pSeq->_a[end];
end--;
}
pSeq->_a[pos] = x;

++pSeq->_size;
}
}
}


6、任意位置删除

void SeqErase(SeqList* pSeq, size_t pos)//任意删除
{
size_t begin=pos;
assert(pSeq);
assert(pos<pSeq->_size);
if(pSeq->_size==0)
{
printf("此链表为空\n");
return;
}
while((int)begin<pSeq->_size)
{
pSeq->_a[begin]=pSeq->_a[begin+1];
begin++;
}
pSeq->_size--;

}


7、修改

void SeqAt(SeqList* pSeq, size_t pos, DataType x)//替换
{
assert(pSeq);
assert(pos<pSeq->_size);
pSeq->_a[pos]=x;
}
void swap(DataType *x,DataType *y)
{
*x^=*y;
*y^=*x;
*x^=*y;
}


8、查找

int SeqFind(SeqList* pSeq, DataType x)//查找
{
size_t i;
assert(pSeq);
for(i=0;i<pSeq->_a[i];i++)
{
if(x==pSeq->_a[i])
{
return i;
}
}
return -1;

}


9、冒泡

void BubbleSort(SeqList* pSeq)//冒泡排序
{
size_t i=0;
size_t j=0;

for(i=0;i<pSeq->_size-1;i++)
{
int flag=0;
for(j=0;j<pSeq->_size-1-i;j++)
{
if((pSeq->_a[j])>(pSeq->_a[j+1]))
swap((&pSeq->_a[j]),(&pSeq->_a[j+1]));
flag=1;
}
if(0==flag)
{
return;
}
}

}


10、选择

void SelectSort(SeqList* pSeq)//选择排序
{

size_t begin=0;
size_t end=pSeq->_size-1;

assert(pSeq);
while(begin<=end)
{
size_t min=begin;
size_t max=begin;
size_t i=begin;
for(i=begin;i<=end;i++)
{
if(pSeq->_a[i]<pSeq->_a[min])
min=i;

if(pSeq->_a[i]>pSeq->_a[max])
max=i;
}
swap(&(pSeq->_a[begin]),&(pSeq->_a[min]));
if(begin==max)
max=min;
swap(&(pSeq->_a[end]),&(pSeq->_a[max]));
begin++;
end--;
}

}


11、二分

int BinarySearch(SeqList* pSeq, DataType x)
{
int begin=0;
int end=pSeq->_size-1;
while(begin<=end)
{
int  mid=begin+((end-begin)>>1);
if(pSeq->_a[mid]<x)
{
begin=mid+1;
}
else if(x<pSeq->_a[mid])
{
end=mid-1;
}
else
return mid;
}
return -1;

}


整体代码:

SeqList.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

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

#define MAX 10

typedef int DataType; //类型重定义

typedef struct SeqList
{
DataType* _a;
size_t _size;
size_t _capicity;
}SeqList;

void SeqPrint(SeqList* pSeq);  //打印
void SeqInit(SeqList* pSeq);   //初始化
void SeqDestory(SeqList* pSeq); //释放
SeqList* SeqCheckFull(SeqList* pSeq);//判满+扩容

void SeqPushBack(SeqList* pSeq, DataType x); //尾插
void SeqPopBack(SeqList* pSeq);   //尾删
void SeqPushFront(SeqList* pSeq, DataType x); //头插
void SeqPopFront(SeqList* pSeq);  //头删

void SeqInsert(SeqList* pSeq, size_t pos, DataType x);//指定位置插入
void SeqErase(SeqList* pSeq, size_t pos);  //指定位置删除

int SeqFind(SeqList* pSeq, DataType x);   //查找指定元素
void SeqAt(SeqList* pSeq, size_t pos, DataType x); //替换指定位置元素

void BubbleSort(SeqList* pSeq); //冒泡排序
void SelectSort(SeqList* pSeq); //选择排序
int BinarySearch(SeqList* pSeq, DataType x); //二分查找

#endif


SeqList.c

#include"SeqList.h"
SeqList* SeqCheckFull(SeqList* pSeq) { assert(pSeq); if(pSeq->_size==pSeq->_capicity) { DataType *tmp=(DataType*)realloc(pSeq->_a,sizeof(DataType)*pSeq->_capicity*2); if(NULL==tmp) { printf("扩容失败\n"); return NULL; } pSeq->_capicity=pSeq->_capicity*2; pSeq->_a=tmp; } return pSeq; }
void SeqDestory(SeqList* pSeq) { free(pSeq->_a); pSeq->_size=0; }
void SeqInit(SeqList* pSeq) { pSeq->_size = 0; pSeq->_capicity = MAX; pSeq->_a = (DataType*)malloc(sizeof(DataType)*pSeq->_capicity); memset(pSeq->_a, 0, sizeof(DataType)*pSeq->_capicity); }
void SeqPrint(SeqList* pSeq) { size_t i=0; for(i=0;i<pSeq->_size;i++) { printf("%d ",pSeq->_a[i]); } }
void SeqPushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
SeqInsert(pSeq, pSeq->_size, x);

}
void SeqPopBack(SeqList* pSeq)
{

SeqErase(pSeq,pSeq->_size-1);
/*if(pSeq->_size==0)
{
printf("此链表为空\n");
return;
}
else
{
pSeq->_size--;
}*/

}
void SeqPushFront(SeqList* pSeq, DataType x)
{
assert(pSeq);
SeqInsert(pSeq,0,x);

}
void SeqPopFront(SeqList* pSeq)
{
assert(pSeq);

/*size_t begin=1;
assert(pSeq);

if(pSeq->_size==0)
{
printf("此链表为空\n");
return;
}
else
{
pSeq->_a[begin-1]=pSeq->_a[begin];
begin++;
}
pSeq->_size--;*/
SeqErase(pSeq,0);
}
void SeqInsert(SeqList* pSeq, size_t pos, DataType x)//任意插 { assert(pSeq); pSeq = SeqCheckFull(pSeq); if (pSeq != NULL) { if (0 == pSeq->_size) { pSeq->_a[pSeq->_size++] = x; } else { int end = pSeq->_size - 1; while (end >= (int)pos) { pSeq->_a[end + 1] = pSeq->_a[end]; end--; } pSeq->_a[pos] = x; ++pSeq->_size; } } }

void SeqErase(SeqList* pSeq, size_t pos)//任意删除 { size_t begin=pos; assert(pSeq); assert(pos<pSeq->_size); if(pSeq->_size==0) { printf("此链表为空\n"); return; } while((int)begin<pSeq->_size) { pSeq->_a[begin]=pSeq->_a[begin+1]; begin++; } pSeq->_size--; }
int SeqFind(SeqList* pSeq, DataType x)//查找 { size_t i; assert(pSeq); for(i=0;i<pSeq->_a[i];i++) { if(x==pSeq->_a[i]) { return i; } } return -1; }
void SeqAt(SeqList* pSeq, size_t pos, DataType x)//替换 { assert(pSeq); assert(pos<pSeq->_size); pSeq->_a[pos]=x; } void swap(DataType *x,DataType *y) { *x^=*y; *y^=*x; *x^=*y; }
void BubbleSort(SeqList* pSeq)//冒泡排序 { size_t i=0; size_t j=0; for(i=0;i<pSeq->_size-1;i++) { int flag=0; for(j=0;j<pSeq->_size-1-i;j++) { if((pSeq->_a[j])>(pSeq->_a[j+1])) swap((&pSeq->_a[j]),(&pSeq->_a[j+1])); flag=1; } if(0==flag) { return; } } }
void SelectSort(SeqList* pSeq)//选择排序 { size_t begin=0; size_t end=pSeq->_size-1; assert(pSeq); while(begin<=end) { size_t min=begin; size_t max=begin; size_t i=begin; for(i=begin;i<=end;i++) { if(pSeq->_a[i]<pSeq->_a[min]) min=i; if(pSeq->_a[i]>pSeq->_a[max]) max=i; } swap(&(pSeq->_a[begin]),&(pSeq->_a[min])); if(begin==max) max=min; swap(&(pSeq->_a[end]),&(pSeq->_a[max])); begin++; end--; } }
int BinarySearch(SeqList* pSeq, DataType x) { int begin=0; int end=pSeq->_size-1; while(begin<=end) { int mid=begin+((end-begin)>>1); if(pSeq->_a[mid]<x) { begin=mid+1; } else if(x<pSeq->_a[mid]) { end=mid-1; } else return mid; } return -1; }


test.c(测试部分)//此处列出部分测试函数

#include"SeqList.h"
SeqList L;
void test1()
{
SeqInit(&L);
SeqPushBack(&L,1);
SeqPushBack(&L,2);
SeqPushBack(&L,3);
SeqPopBack(&L);
SeqPopBack(&L);

SeqPrint(&L);

}
void test3()
{
SeqInit(&L);
SeqPushFront(&L,1);
SeqPushFront(&L,2);
SeqPopFront(&L);

SeqPrint(&L);
printf("\n");
}
void test2()
{
SeqInit(&L);
SeqInsert(&L,0,1);
SeqInsert(&L,0,1);
SeqInsert(&L,1,2);
SeqInsert(&L,2,20);
SeqPrint(&L);
printf("\n");
printf("%d",SeqFind(&L,20));
printf("\n");

SeqAt(&L,1,4);
SeqPrint(&L);
/*SeqErase(&L,0);

SeqPrint(&L);
SeqErase(&L,1);
printf("\n");
SeqPrint(&L);
printf("\n");*/
}
void test4()//冒泡
{
SeqInit(&L);
SeqPushBack(&L,3);
SeqPushBack(&L,12);
SeqPushBack(&L,4);
SeqPushBack(&L,65);
SeqPushBack(&L,32);
SeqPushBack(&L,1);
SeqPushBack(&L,9);
SeqPrint(&L);
printf("\n");
BubbleSort(&L);

SeqPrint(&L);
}
void test5()//二分法
{
int ret=0;
SeqInit(&L);
SeqPushBack(&L,1);
SeqPushBack(&L,2);
SeqPushBack(&L,3);
SeqPushBack(&L,4);
SeqPushBack(&L,5);
SeqPushBack(&L,6);
SeqPushBack(&L,7);
SeqPrint(&L);
ret= BinarySearch(&L,7);
printf("\n");

if (-1 == ret)
{
printf("没找到\n");
}
else
{
printf("下标是%d\n", ret);
}

}
void test6()//选择排序法
{

SeqInit(&L);
SeqPushBack(&L,4);
SeqPushBack(&L,2);
SeqPushBack(&L,1);
SeqPushBack(&L,5);

SeqPrint(&L);
printf("\n");
SelectSort(&L);
SeqPrint(&L);

}
int main()
{

test6();

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: