顺序表是用一段地址连续的存储单元依次存储数据元素的线性结构。顺序表可分为静态存储和动态存储,静态顺序表比较简单,数据空间固定,而动态顺序表可以动态增容,便于存放大量数据,现主要把动态的基本实现一下~此处的排序简单实现了一下,后面会整理出各种排序~~

#define MAX_SIZE 100
#define INIT_SIZE 3
typedef int DataType;
//顺序表的静态存储
typedef struct SeqList_s
{
DataType array[MAX_SIZE]; //数据段
size_t size; //数据的个数
}SeqList_s;
//顺序表的动态存储
typedef struct SeqList_d
{
DataType* array; //数据块指针
size_t size; //有效数据的个数
size_t capacity; //容量
}SeqList_d;
//检查容量,增容
void CheckCapacity(SeqList_d *pSeq)
{
assert(pSeq);
DataType* temp;
if (pSeq->size >= pSeq->capacity)
{
pSeq->capacity *= 2; //容量增至2倍
temp = (DataType*)realloc(pSeq->array, pSeq->capacity*sizeof(DataType));
if (NULL == temp)
{
return;//如果没有增容成功,返回
}
pSeq->array = temp;
}
else
{
return;
}
}
//初始化
int InitSeqList_d(SeqList_d* pSeq)
{
assert(pSeq);
pSeq->array = (DataType*)malloc(sizeof(DataType)*INIT_SIZE);
if (NULL == pSeq)
{
return 0; //未成功
}
pSeq->size = 0;
pSeq->capacity = INIT_SIZE;
return 1;
}
//查找
size_t Find(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
size_t pos = pSeq->size; //用pos记录x所在位置,若大于等于size,则说明未找到
for (size_t i = 0; i < pSeq->size; i++)
{
if (*(pSeq->array + i) == x)
{
pos = i;
break;
}
}
if (pos >= pSeq->size)
{
printf("未找到\n");
}
return pos;
}
//尾插
void PushBack(SeqList_d* pSeq, DataType x)
{
assert(pSeq != NULL);
CheckCapacity(pSeq);
pSeq->array[pSeq->size++] = x;
}
//插入
void Insert(SeqList_d* pSeq, size_t pos, DataType x)
{
assert(pSeq);
CheckCapacity(pSeq);
if (pos > pSeq->size)
{
printf("插入位置有误!\n");
return;
}
	DataType* temp = pSeq->array + pSeq->size - 1;
while (temp >= (pSeq->array + pos))
{
*(temp + 1) = temp;
temp--;
}
	////也可以这样移动
//for (size_t i = pSeq->size - 1; i >= pos; i--)
//{
// pSeq->array[i+1] = pSeq->array[i];
//}
	*(pSeq->array + pos) = x;
pSeq->size++;
}
//删除pos位置的数据
void Erase(SeqList_d* pSeq, size_t pos)
{
assert(pSeq);
if (pos < 0 || pos >= pSeq->size)
{
printf("要删除的位置有误!\n");
return;
}
for (size_t i = pos; i < pSeq->size; i++)
{
*(pSeq->array + i) = *(pSeq->array + i + 1);
}
pSeq->size--;
}
//删除找到的第一个x
void Remove(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
size_t pos = Find(pSeq, x);
if (pos < 0 || pos >= pSeq->size)
{
printf("没有x这个数据!\n");
return;
}
else
{
for (size_t i = pos; i < pSeq->size; i++)
{
*(pSeq->array + i) = *(pSeq->array + i + 1);
}
pSeq->size--;
}
}
//删除所有的x(有优化)
void RemoveAll(SeqList_d* pSeq, DataType x)
{
	assert(pSeq);
size_t first = 0, second = 0, count = 0;
while (second < pSeq->size)
{
if (*(pSeq->array + second) == x)
{
count++;
}
else
{
*(pSeq->array + first) = *(pSeq->array + second);
first++;
}
second++;
}
pSeq->size -= count;
}

用second指针遍历顺序表,用first指针记录删除后的顺序表。

当second指针不是指向所要删除的数据3时,second指向的数据赋给first指向的数据,first指针和second指针同时向后指;

当second指针指向所要删除的数据3时,first指针不动,count计数加1,second指针继续向后指。

这样就把数据删除了~~~这种方法比找到一个就挪动一次顺序表要高效一些。。。

//排序(从小到大)
void Sort(SeqList_d* pSeq)
{
assert(pSeq);
if (pSeq->size <= 0)
{
return;
}
DataType temp;
bool flag = true;
for (size_t i = 0; (i < pSeq->size - 1) && flag; i++) //进行size-1趟排序
{
flag = false;
for (size_t j = 0; j < pSeq->size - i - 1; j++)
{
if (*(pSeq->array + j) > *(pSeq->array + j + 1))
{
temp = *(pSeq->array + j);
*(pSeq->array + j) = *(pSeq->array + j + 1);
*(pSeq->array + j + 1) = temp;
flag = true;
}
}
}
}
//二分查找(非递归)
int BinarySearch(SeqList_d* pSeq, DataType x)
{
assert(pSeq);
int left = 0, right = pSeq->size - 1;
int mid;
while (left <= right)
{
mid = left + (right - left) / 2; //这样算mid可以避免溢出
if (*(pSeq->array + mid) < x) //x在后边部分
{
left = mid + 1;
}
else if (*(pSeq->array + mid) > x) //x在前半部分
{
right = mid - 1;
}
else
{
return mid;
break;
}
}
return -1;
}
//二分查找(递归)
int _BinarySearch_R(SeqList_d* pSeq, int left, int right, DataType x)
{
assert(pSeq);
if (left <= right)
{
int mid = left + (right - left) / 2;
if (pSeq->array[mid] < x)
{
return _BinarySearch_R(pSeq, mid + 1, right, x);
}
else if (pSeq->array[mid] > x)
{
return _BinarySearch_R(pSeq, left, mid - 1, x);
}
else
{
return mid;
}
}
return -1;
}
int Binary_R(SeqList_d* pSeq, DataType x)
{
return _BinarySearch_R(pSeq, 0, pSeq->size - 1, x);
}

C语言实现的顺序表的更多相关文章

  1. 数据结构C语言版--动态顺序表的基本功能实现(二)

    /* * 若各个方法结构体变量参数为: &L(即地址符加变量)则结构体变量访问结构成员变量时使用"." * 若为:*L(即取地址符加变量)则结构体变量访问结构体成员变量使用 ...

  2. C语言学习笔记-顺序表

    #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "coni ...

  3. C语言版本:顺序表的实现

    seqlist.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include<cstdio> #include<malloc.h> ...

  4. c语言描述的顺序表实现

    //顺序表的实现:(分配一段连续地址给顺序表,像数组一样去操作) #include<stdio.h> #include<stdlib.h> #define OK 1 #defi ...

  5. C语言项目实现顺序表

    #include <stdio.h> #include <stdlib.h> #include "test_顺序表声明.h" /* run this pro ...

  6. 数据结构C语言版--静态顺序表的基本功能实现(一)

    /* * 功能:创建一个线性表,并输出 * 静态分配内存 */ #include<stdio.h> //stdio.h是C的标准I/O库 //#include<iostream> ...

  7. 【数据结构】之顺序表(Java语言描述)

    之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...

  8. 【数据结构】之顺序表(C语言描述)

    顺序表是线性表的一种,它将元素存储在一段连续的内存空间中,表中的任意元素都可以通过下标快速的获取到,因此,顺序表适合查询操作频繁的场景,而不适合增删操作频繁的场景. 下面是使用 C语言 编写的顺序表的 ...

  9. C语言实现顺序表

    C语言实现顺序表代码 文件SeqList.cpp #pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqLi ...

随机推荐

  1. 自定义HttpHandler配置iis8.0

    配置环境,window8 ,iis8.0,.net Framework4.0,托管管道模式“经典” 步骤 (1)进入iis管理器,选中要设置的网站-->功能视图-->MIME类型--> ...

  2. [React] React Fundamentals: First Component

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. oc中的类学习笔记1

    1.oc中的类和构造方法 NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init]; NSAutoreleasePool是一个类,alloc ...

  4. 自定的TableView

    一.自定的TableView 有的时候,我们需要vc视图中添加一个表视图,此时在ViewController中使用TableViewController是不可行的这就,因此就要使用自定义的TableV ...

  5. 微信公众号支付(三):页面调用微信支付JS并完成支付

    一.调用微信的JS文件 1.首先要绑定[JS接口安全域名],“公众号设置”的“功能设置”中 2.引入JS文件 备注:支持使用 AMD/CMD 标准模块加载方法加载 <script type=&q ...

  6. datetimepicker 初始化只显示年

    $("#overdue2").datetimepicker({ format: 'yyyy', autoclose: true, startView:4, minView:4, t ...

  7. 20160501--struts2入门3

    一.自定义拦截器 要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口: public class PermissionInterc ...

  8. 本机Font字体

    void getFontList() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Str ...

  9. ns2出现Client: Handoff Attempt的情况解决

    找到mac/mac-802_11.cc,这是系统本身一个bug,对于adhoc网络无需进行切换尝试. > if (*rcount == 3 && handoff == 0) {& ...

  10. 使用NPOI操作Excel

    案例:用NPOI动态生成一个Excel表,然后弹出对话框让用户下载,文件名是"用户列表.xls" 先去相关网站下载 NPOI DLL文件,再引用   application/x-e ...